Skip to content

Commit

Permalink
[Fix rubocop#6956] Prevent auto-correct confliction of Lint/Lambda
Browse files Browse the repository at this point in the history
…and `Lint/UnusedBlockArgument`

Fixes rubocop#6956.

This PR prevents auto-correct confliction of `Lint/Lambda` and `Lint/UnusedBlockArgument`.

The following is the reproduction procedure.

```ruby
# example.rb
c = -> event do
  puts 'Hello world'
end
```

```console
% rubocop example.rb -a --only Style/Lambda,Lint/UnusedBlockArgument
```

## Before

A reserved word `lambda` is broken.

```diff
% git diff
diff --git a/example.rb b/example.rb
index 3445c6f..b8e24f4 100644
--- a/example.rb
+++ b/example.rb
@@ -1,3 +1,3 @@
-c = -> event do
+c = lambda_ do |_event|
   puts 'Hello world'
 end
```

## After

It will be a valid code.

```diff
% git diff
diff --git a/example.rb b/example.rb
index 3445c6f..9c22b37 100644
--- a/example.rb
+++ b/example.rb
@@ -1,3 +1,3 @@
-c = -> event do
+c = lambda do |_event|
   puts 'Hello world'
 end
```
  • Loading branch information
koic committed Apr 23, 2019
1 parent cd1d1a7 commit 6859345
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
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)
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

0 comments on commit 6859345

Please sign in to comment.