Closed
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