Skip to content

Commit 93ee728

Browse files
committed
[Fix #1045] Fix an incorrect autocorrect for Rails/NegateInclude
Fixes #1045. This PR fixes an incorrect autocorrect for `Rails/NegateInclude` when using `Style/InverseMethods`'s autocorrection together.
1 parent 34376e0 commit 93ee728

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1045](https://github.com/rubocop/rubocop-rails/issues/1045): Fix an incorrect autocorrect for `Rails/NegateInclude` when using `Style/InverseMethods`'s autocorrection together. ([@koic][])

lib/rubocop-rails.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717

1818
RuboCop::Cop::Style::HashExcept.minimum_target_ruby_version(2.0)
1919

20+
RuboCop::Cop::Style::InverseMethods.singleton_class.prepend(
21+
Module.new do
22+
def autocorrect_incompatible_with
23+
super.push(RuboCop::Cop::Rails::NegateInclude)
24+
end
25+
end
26+
)
27+
2028
RuboCop::Cop::Style::MethodCallWithArgsParentheses.singleton_class.prepend(
2129
Module.new do
2230
def autocorrect_incompatible_with

spec/rubocop/cli/autocorrect_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@
2525
RUBY
2626
end
2727

28+
it 'corrects `Rails/NegateInclude,` with `Style/InverseMethods`' do
29+
create_file('example.rb', <<~RUBY)
30+
array.select { |item| !do_something.include?(item) }
31+
RUBY
32+
33+
expect(cli.run(['-A', '--only', 'Rails/NegateInclude,Style/InverseMethods'])).to eq(0)
34+
expect(File.read('example.rb')).to eq(<<~RUBY)
35+
array.reject { |item| do_something.include?(item) }
36+
RUBY
37+
end
38+
2839
it 'corrects `Rails/SafeNavigation` with `Style/RedundantSelf`' do
2940
create_file('.rubocop.yml', <<~YAML)
3041
Rails/SafeNavigation:

0 commit comments

Comments
 (0)