Skip to content

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

Closed
@umbrella-h

Description

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions