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 #10754] Fix an incorrect autocorrect for Style/HashExcept #10758

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
@@ -0,0 +1 @@
* [#10754](https://github.com/rubocop/rubocop/issues/10754): Fix an incorrect autocorrect for `Style/HashExcept` when using a non-literal collection receiver for `include?`. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/hash_except.rb
Expand Up @@ -144,7 +144,7 @@ def except_key_source(key)
return key.join(', ')
end

key.source
key.literal? ? key.source : "*#{key.source}"
end

def decorate_source(value)
Expand Down
96 changes: 96 additions & 0 deletions spec/rubocop/cop/style/hash_except_spec.rb
Expand Up @@ -142,6 +142,30 @@
{foo: 1, bar: 2, baz: 3}.except("\#{foo}", 'bar')
RUBY
end

it 'registers and corrects an offense when using `reject` and calling `include?` method with variable' do
expect_offense(<<~RUBY)
array = [:foo, :bar]
{foo: 1, bar: 2, baz: 3}.reject { |k, v| !array.include?(k) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `except(*array)` instead.
RUBY

expect_correction(<<~RUBY)
array = [:foo, :bar]
{foo: 1, bar: 2, baz: 3}.except(*array)
RUBY
end

it 'registers and corrects an offense when using `reject` and calling `include?` method with method call' do
expect_offense(<<~RUBY)
{foo: 1, bar: 2, baz: 3}.reject { |k, v| !array.include?(k) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `except(*array)` instead.
RUBY

expect_correction(<<~RUBY)
{foo: 1, bar: 2, baz: 3}.except(*array)
RUBY
end
end

context 'using `exclude?`' do
Expand Down Expand Up @@ -316,6 +340,30 @@
{foo: 1, bar: 2, baz: 3}.except("\#{foo}", 'bar')
RUBY
end

it 'registers and corrects an offense when using `reject` and calling `key.in?` method with variable' do
expect_offense(<<~RUBY)
array = %i[foo bar]
{foo: 1, bar: 2, baz: 3}.reject { |k, v| k.in?(array) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `except(*array)` instead.
RUBY

expect_correction(<<~RUBY)
array = %i[foo bar]
{foo: 1, bar: 2, baz: 3}.except(*array)
RUBY
end

it 'registers and corrects an offense when using `reject` and calling `key.in?` method with method call' do
expect_offense(<<~RUBY)
{foo: 1, bar: 2, baz: 3}.reject { |k, v| k.in?(array) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `except(*array)` instead.
RUBY

expect_correction(<<~RUBY)
{foo: 1, bar: 2, baz: 3}.except(*array)
RUBY
end
end

context 'using `include?`' do
Expand Down Expand Up @@ -373,6 +421,30 @@
{foo: 1, bar: 2, baz: 3}.except("\#{foo}", 'bar')
RUBY
end

it 'registers and corrects an offense when using `reject` and calling `include?` method with variable' do
expect_offense(<<~RUBY)
array = %i[foo bar]
{foo: 1, bar: 2, baz: 3}.reject { |k, v| !array.include?(k) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `except(*array)` instead.
RUBY

expect_correction(<<~RUBY)
array = %i[foo bar]
{foo: 1, bar: 2, baz: 3}.except(*array)
RUBY
end

it 'registers and corrects an offense when using `reject` and calling `include?` method with method call' do
expect_offense(<<~RUBY)
{foo: 1, bar: 2, baz: 3}.reject { |k, v| !array.include?(k) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `except(*array)` instead.
RUBY

expect_correction(<<~RUBY)
{foo: 1, bar: 2, baz: 3}.except(*array)
RUBY
end
end

context 'using `exclude?`' do
Expand Down Expand Up @@ -430,6 +502,30 @@
{foo: 1, bar: 2, baz: 3}.except("\#{foo}", 'bar')
RUBY
end

it 'registers and corrects an offense when using `reject` and calling `!exclude?` method with variable' do
expect_offense(<<~RUBY)
array = %i[foo bar]
{foo: 1, bar: 2, baz: 3}.reject { |k, v| !array.exclude?(k) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `except(*array)` instead.
RUBY

expect_correction(<<~RUBY)
array = %i[foo bar]
{foo: 1, bar: 2, baz: 3}.except(*array)
RUBY
end

it 'registers and corrects an offense when using `reject` and calling `!exclude?` method with method call' do
expect_offense(<<~RUBY)
{foo: 1, bar: 2, baz: 3}.reject { |k, v| !array.exclude?(k) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `except(*array)` instead.
RUBY

expect_correction(<<~RUBY)
{foo: 1, bar: 2, baz: 3}.except(*array)
RUBY
end
end

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