Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #10843] Fix a false positive for Style/HashExcept #10844

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fix_a_false_positive_for_style_hash_except.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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