Skip to content

Commit

Permalink
[Fix #6860] Prevent auto-correct conflict of Style/InverseMethods a…
Browse files Browse the repository at this point in the history
…nd `Style/Not`.

Prevent conflict problem like below:

file:
```
x.select { |y| not y.z }
```

auto-correct:
```
$ bundle exec rubocop -a c.rb --only Style/InverseMethods,Style/Not
Inspecting 1 file
C

Offenses:

c.rb:1:1: C: [Corrected] Style/InverseMethods: Use reject instead of inverting select.
x.select { |y| not y.z }
^^^^^^^^^^^^^^^^^^^^^^^^
c.rb:1:1: C: [Corrected] Style/InverseMethods: Use select instead of inverting reject.
x.reject { |y| !y.z }
^^^^^^^^^^^^^^^^^^^^^
c.rb:1:16: C: [Corrected] Style/Not: Use ! instead of not.
x.select { |y| not y.z }
               ^^^

1 file inspected, 3 offenses detected, 3 offenses corrected
```

corrected badly:
```
x.select { |y| y.z }
```
  • Loading branch information
hoshinotsuyoshi authored and bbatsov committed Apr 22, 2019
1 parent 1c0dd05 commit 43755df
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,7 @@
* [#6946](https://github.com/rubocop-hq/rubocop/pull/6946): Allow `Rails/ReflectionClassName` to use string interpolation for `class_name`. ([@r7kamura][])
* [#6778](https://github.com/rubocop-hq/rubocop/issues/6778): Fix a false positive in `Style/HashSyntax` cop when a hash key is an interpolated string and EnforcedStyle is ruby19_no_mixed_keys. ([@tatsuyafw][])
* [#6902](https://github.com/rubocop-hq/rubocop/issues/6902): Fix a bug where `Naming/RescuedExceptionsVariableName` would handle an only first rescue for multiple rescue groups. ([@tatsuyafw][])
* [#6860](https://github.com/rubocop-hq/rubocop/issues/6860): Prevent auto-correct conflict of `Style/InverseMethods` and `Style/Not`. ([@hoshinotsuyoshi][])

### Changes

Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/cop/style/inverse_methods.rb
Expand Up @@ -37,6 +37,10 @@ class InverseMethods < Cop
NEGATED_EQUALITY_METHODS = %i[!= !~].freeze
CAMEL_CASE = /[A-Z]+[a-z]+/.freeze

def self.autocorrect_incompatible_with
[Style::Not]
end

def_node_matcher :inverse_candidate?, <<-PATTERN
{
(send $(send $(...) $_ $...) :!)
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cli/cli_autocorrect_spec.rb
Expand Up @@ -452,6 +452,21 @@ def verify_section
expect(IO.read('example.rb')).to eq(corrected)
end

it 'corrects Style/InverseMethods and Style/Not offenses' do
source = <<-'RUBY'.strip_indent
x.select {|y| not y.z }
RUBY
create_file('example.rb', source)
expect(cli.run([
'--auto-correct',
'--only', 'Style/InverseMethods,Style/Not'
])).to eq(0)
corrected = <<-'RUBY'.strip_indent
x.reject {|y| y.z }
RUBY
expect(IO.read('example.rb')).to eq(corrected)
end

describe 'caching' do
let(:cache) do
instance_double(RuboCop::ResultCache, 'valid?' => true,
Expand Down

0 comments on commit 43755df

Please sign in to comment.