Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-correction fixes both Style/InverseMethods and Rails/NegateInclude in a line #1045

Closed
umbrella-h opened this issue Jul 13, 2023 · 0 comments · Fixed by #1070
Closed
Labels
bug Something isn't working

Comments

@umbrella-h
Copy link

umbrella-h commented Jul 13, 2023

Expected behavior

When Style/InverseMethods and Rails/NegateInclude are caught in a line of code, the auto-correction should fix one of the two.

Actual behavior

When Style/InverseMethods and Rails/NegateInclude are caught in a line of code, the auto-correction fixes both and leads to a logically opposite behavior.

Reproduce the Problem

class TestAutoCorrect
  def perform
    %w[art science biography].select { |item| !%w[travel art].include?(item) }

    # rubocop -A
    # %w[art science biography].reject { |item| %w[travel art].exclude?(item) }
  end
end

Before auto-corrected:

2.6.0 :001 > TestAutoCorrect.new.perform
 => ["science", "biography"]

After auto-corrected:

2.6.0 :001 > TestAutoCorrect.new.perform
 => ["art"]

rubocop messages:

$ rubocop test_auto_correct.rb
Inspecting 1 file
C

Offenses:

test_auto_correct.rb:3:5: C: [Correctable] Style/InverseMethods: Use reject instead of inverting select.
    %w[art science biography].select { |item| !%w[travel art].include?(item) }
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
test_auto_correct.rb:3:47: C: [Correctable] Rails/NegateInclude: Use .exclude? and remove the negation part.
    %w[art science biography].select { |item| !%w[travel art].include?(item) }
                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
test_auto_correct.rb:3:48: C: Performance/CollectionLiteralInLoop: Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.
    %w[art science biography].select { |item| !%w[travel art].include?(item) }
                                               ^^^^^^^^^^^^^^

1 file inspected, 3 offenses detected, 2 offenses auto-correctable
$ rubocop -A test_auto_correct.rb
Inspecting 1 file
C

Offenses:

test_auto_correct.rb:3:5: C: [Corrected] Style/InverseMethods: Use reject instead of inverting select.
    %w[art science biography].select { |item| !%w[travel art].include?(item) }
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
test_auto_correct.rb:3:47: C: [Corrected] Rails/NegateInclude: Use .exclude? and remove the negation part.
    %w[art science biography].select { |item| !%w[travel art].include?(item) }
                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected, 2 offenses corrected

Although aware that rubocop -A includes unsafe corrections, we still find it necessary to point this out as an example for unsafe corrections. (Or if there is any chance of getting fixed. I will also take a stab at this later in my free time).

Rubocop Version

$ bundle exec rubocop -V
1.23.0 (using Parser 3.0.2.0, rubocop-ast 1.13.0, running on ruby 2.6.0 x86_64-linux)
  - rubocop-performance 1.12.0
  - rubocop-rails 2.12.4
  - rubocop-rspec 2.6.0
@koic koic added the bug Something isn't working label Aug 16, 2023
koic added a commit to koic/rubocop-rails that referenced this issue Aug 16, 2023
…ude`

Fixes rubocop#1045.

This PR fixes an incorrect autocorrect for `Rails/NegateInclude`
when using `Style/InverseMethods`'s autocorrection together.
koic added a commit to koic/rubocop-rails that referenced this issue Aug 16, 2023
…ude`

Fixes rubocop#1045.

This PR fixes an incorrect autocorrect for `Rails/NegateInclude`
when using `Style/InverseMethods`'s autocorrection together.
koic added a commit that referenced this issue Aug 20, 2023
…ails_negate_include

[Fix #1045] Fix an incorrect autocorrect for `Rails/NegateInclude`
koic added a commit to koic/rubocop-rails that referenced this issue Aug 22, 2023
…ude`

Fixes rubocop#1045.

This PR fixes an incorrect autocorrect for `Rails/NegateInclude`
when using `Style/InverseMethods`'s autocorrection together.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants