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 #6956] Prevent auto-correct confliction of Lint/Lambda and Lint/UnusedBlockArgument #6958

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.md
Expand Up @@ -21,6 +21,7 @@
* [#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][])
* [#6935](https://github.com/rubocop-hq/rubocop/issues/6935): `Layout/AccessModifierIndentation` should ignore access modifiers that apply to specific methods. ([@deivid-rodriguez][])
* [#6956](https://github.com/rubocop-hq/rubocop/issues/6956): Prevent auto-correct confliction of `Lint/Lambda` and `Lint/UnusedBlockArgument`. ([@koic][])

### Changes

Expand Down
24 changes: 18 additions & 6 deletions lib/rubocop/cop/correctors/unused_arg_corrector.rb
Expand Up @@ -15,14 +15,26 @@ def correct(processed_source, node)
@processed_source = processed_source

if node.blockarg_type?
correct_for_blockarg_type(node)
else
lambda do |corrector|
range = range_with_surrounding_space(range: node.source_range,
side: :left)
range = range_with_surrounding_comma(range, :left)
corrector.remove(range)
variable_name = if node.optarg_type?
node.node_parts[0]
else
# Extract only a var name without splat (`*`)
node.source.gsub(/\A\*+/, '')
end
corrector.replace(node.loc.name, "_#{variable_name}")
end
else
->(corrector) { corrector.insert_before(node.loc.name, '_') }
end
end

def correct_for_blockarg_type(node)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 I extracted to the method because it was warned by Metrics cops.

lambda do |corrector|
range = range_with_surrounding_space(range: node.source_range,
side: :left)
range = range_with_surrounding_comma(range, :left)
corrector.remove(range)
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cli/cli_autocorrect_spec.rb
Expand Up @@ -467,6 +467,25 @@ def verify_section
expect(IO.read('example.rb')).to eq(corrected)
end

it 'corrects `Lint/Lambda` and `Lint/UnusedBlockArgument` offenses' do
source = <<-'RUBY'.strip_indent
c = -> event do
puts 'Hello world'
end
RUBY
create_file('example.rb', source)
expect(cli.run([
'--auto-correct',
'--only', 'Lint/Lambda,Lint/UnusedBlockArgument'
])).to eq(0)
corrected = <<-'RUBY'.strip_indent
c = lambda do |_event|
puts 'Hello world'
end
RUBY
expect(IO.read('example.rb')).to eq(corrected)
end

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