Skip to content

Commit

Permalink
[Fix rubocop#10843] Fix a false positive for Style/HashExcept
Browse files Browse the repository at this point in the history
Fixes rubocop#10843.

This PR fix a false positive for `Style/HashExcept` when using
`reject` and calling `include?` method with symbol array and
second block value.
  • Loading branch information
koic authored and WJWH committed Aug 8, 2022
1 parent da9fd6d commit e345249
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/fix_a_false_positive_for_style_hash_except.md
@@ -0,0 +1 @@
* [#10843](https://github.com/rubocop/rubocop/issues/10843): Fix a false positive for `Style/HashExcept` when using `reject` and calling `include?` method with symbol array and second block value. ([@koic][])
4 changes: 0 additions & 4 deletions lib/rubocop/cop/style/hash_except.rb
Expand Up @@ -159,10 +159,6 @@ def except_key(node)
key_argument = node.argument_list.first.source
body = extract_body_if_nagated(node.body)
lhs, _method_name, rhs = *body

return lhs if body.method?('include?')
return lhs if body.method?('exclude?')
return rhs if body.method?('in?')
return if [lhs, rhs].map(&:source).none?(key_argument)

[lhs, rhs].find { |operand| operand.source != key_argument }
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/style/hash_except_spec.rb
Expand Up @@ -166,6 +166,12 @@
{foo: 1, bar: 2, baz: 3}.except(*array)
RUBY
end

it 'does not register an offense when using `reject` and calling `include?` method with symbol array and second block value' do
expect_no_offenses(<<~RUBY)
{foo: 1, bar: 2, baz: 3}.reject { |k, v| ![1, 2].include?(v) }
RUBY
end
end

context 'using `exclude?`' do
Expand Down Expand Up @@ -364,6 +370,12 @@
{foo: 1, bar: 2, baz: 3}.except(*array)
RUBY
end

it 'does not register an offense when using `reject` and calling `in?` method with symbol array and second block value' do
expect_no_offenses(<<~RUBY)
{foo: 1, bar: 2, baz: 3}.reject { |k, v| v.in?([1, 2]) }
RUBY
end
end

context 'using `include?`' do
Expand Down Expand Up @@ -526,6 +538,12 @@
{foo: 1, bar: 2, baz: 3}.except(*array)
RUBY
end

it 'does not register an offense when using `reject` and calling `exclude?` method with symbol array and second block value' do
expect_no_offenses(<<~RUBY)
{foo: 1, bar: 2, baz: 3}.reject { |k, v| ![1, 2].exclude?(v) }
RUBY
end
end

it 'does not register an offense when using `reject` and other than comparison by string and symbol using `==`' do
Expand Down

0 comments on commit e345249

Please sign in to comment.