Skip to content

Commit

Permalink
[Fix rubocop#6969] Fix a false positive with block methods in `Style/…
Browse files Browse the repository at this point in the history
…InverseMethods`
  • Loading branch information
dduugg committed May 10, 2019
1 parent 21d80aa commit 6406543
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [#5782](https://github.com/rubocop-hq/rubocop/issues/5782): Do not autocorrect `Lint/UnifiedInteger` if `TargetRubyVersion < 2.4`. ([@lavoiesl][])
* [#6387](https://github.com/rubocop-hq/rubocop/issues/6387): Prevent `Lint/NumberConversion` from reporting error with `Time`/`DateTime`. ([@tejasbubane][])
* [#6980](https://github.com/rubocop-hq/rubocop/issues/6980): Fix `Style/StringHashKeys` to allow string as keys for hash arguments to gsub methods. ([@tejasbubane][])
* [#6969](https://github.com/rubocop-hq/rubocop/issues/6969): Fix a false positive with block methods in `Style/InverseMethods`. ([@dduugg][])

### Changes

Expand All @@ -33,7 +34,8 @@
* [#6992](https://github.com/rubocop-hq/rubocop/pull/6992): Fix unknown default configuration for `Layout/IndentFirstParameter` cop. ([@drenmi][])
* [#6972](https://github.com/rubocop-hq/rubocop/issues/6972): Fix a false positive for `Style/MixinUsage` when using inside block and `if` condition is after `include`. ([@koic][])
* [#6738](https://github.com/rubocop-hq/rubocop/issues/6738): Prevent auto-correct conflict of `Style/Next` and `Style/SafeNavigation`. ([@hoshinotsuyoshi][])
* [#6847](https://github.com/rubocop-hq/rubocop/pull/6847): Fix `Style/BlockDelimiters` to properly check if the node is chaned when `braces_for_chaining` is set. ([@att14][])
* [#6847](https://github.com/rubocop-hq/rubocop/pull/6847): Fix `Style/BlockDelimiters` to properly check if the node is chained when `braces_for_chaining` is set. ([@att14][])

### Bug fixes

* Replace `Time.zone.current` with `Time.zone.today` on `Rails::Date` cop message. ([@vfonic][])
Expand Down
6 changes: 6 additions & 0 deletions lib/rubocop/cop/style/inverse_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ module Style
# foo == bar
# !!('foo' =~ /^\w+$/)
# !(foo.class < Numeric) # Checking class hierarchy is allowed
# # Blocks with guard clauses are ignored:
# foo.select do |f|
# next if f.zero?
# f != 1
# end
class InverseMethods < Cop
include IgnoredNode
include RangeHelp
Expand Down Expand Up @@ -76,6 +81,7 @@ def on_block(node)
inverse_block?(node) do |_method_call, method, block|
return unless inverse_blocks.key?(method)
return if negated?(node) && negated?(node.parent)
return if node.each_node(:next).any?

# Inverse method offenses inside of the block of an inverse method
# offense, such as `y.reject { |key, _value| !(key =~ /c\d/) }`,
Expand Down
5 changes: 5 additions & 0 deletions manual/cops_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -2641,6 +2641,11 @@ foo != bar
foo == bar
!!('foo' =~ /^\w+$/)
!(foo.class < Numeric) # Checking class hierarchy is allowed
# Blocks with guard clauses are ignored:
foo.select do |f|
next if f.zero?
f != 1
end
```

### Configurable attributes
Expand Down
14 changes: 14 additions & 0 deletions spec/rubocop/cop/style/inverse_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@
expect_no_offenses('!!foo.reject { |e| !e }')
end

it 'allows an inverse method in a block with next' do
expect_no_offenses(<<~RUBY)
class TestClass
def test_method
[1, 2, 3, 4].select do |number|
next if number == 4
number != 2
end
end
end
RUBY
end

context 'auto-correct' do
it 'corrects !.none? with a symbol proc to any?' do
new_source = autocorrect_source('!foo.none?(&:even?)')
Expand Down

0 comments on commit 6406543

Please sign in to comment.