Skip to content

Commit

Permalink
Fix an incorrect auto-correct for Lint/AmbiguousOperator
Browse files Browse the repository at this point in the history
This PR fixes the following incorrect auto-correct for
`Style/MethodCallWithArgsParentheses` with `Lint/AmbiguousOperator`.

```console
% cat example.rb
def foo(&block)
  do_something &block
end

% rubocop --only Style/MethodCallWithArgsParentheses,Lint/AmbiguousOperator -a
Inspecting 4 files
..WC

Offenses:

example.rb:2:3: C: [Corrected] Style/MethodCallWithArgsParentheses: Use
parentheses for method calls with arguments.
  do_something &block
  ^^^^^^^^^^^^^^^^^^^
example.rb:2:16: W: [Corrected] Lint/AmbiguousOperator: Ambiguous block
operator. Parenthesize the method arguments if it's surely a block
operator, or add a whitespace to the right of the & if it should be a
binary AND.
  do_something &block
               ^
ripper/example.rb:6:5: C: [Corrected]
Style/MethodCallWithArgsParentheses: Use parentheses for method calls
with arguments.
    raise message
    ^^^^^^^^^^^^^

4 files inspected, 3 offenses detected, 3 offenses corrected
```

## Before

Syntax error with redundant closing parentheses.

```console
% cat example.rb
def foo(&block)
  do_something(&block))
end

% ruby -c example.rb
example.rb:2: syntax error, unexpected ')', expecting end
  do_something(&block))
```

## After

Valid syntax.

```console
% cat example.rb
def foo(&block)
  do_something(&block)
end
```
  • Loading branch information
koic committed Apr 3, 2021
1 parent 1cd90c7 commit e0342b2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
@@ -0,0 +1 @@
* [#9671](https://github.com/rubocop/rubocop/pull/9671): Fix an incorrect auto-correct for `Lint/AmbiguousOperator` with `Style/MethodCallWithArgsParentheses`. ([@koic][])
5 changes: 4 additions & 1 deletion lib/rubocop/cop/util.rb
Expand Up @@ -38,7 +38,10 @@ def add_parentheses(node, corrector)
elsif node.arguments.empty?
corrector.insert_after(node, '()')
else
corrector.replace(args_begin(node), '(')
args_begin = args_begin(node)

corrector.remove(args_begin)
corrector.insert_before(args_begin, '(')
corrector.insert_after(args_end(node), ')')
end
end
Expand Down
23 changes: 23 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Expand Up @@ -236,6 +236,29 @@ def batch
RUBY
end

it 'corrects `EnforcedStyle: require_parentheses` of `Style/MethodCallWithArgsParentheses` with ' \
'`Lint/AmbiguousOperator`' do
create_file('.rubocop.yml', <<~YAML)
Style/MethodCallWithArgsParentheses:
EnforcedStyle: require_parentheses
YAML
create_file('example.rb', <<~RUBY)
def foo(&block)
do_something &block
end
RUBY
expect(
cli.run(
['--auto-correct', '--only', 'Style/MethodCallWithArgsParentheses,Lint/AmbiguousOperator']
)
).to eq(0)
expect(IO.read('example.rb')).to eq(<<~RUBY)
def foo(&block)
do_something(&block)
end
RUBY
end

it 'corrects `Style/IfUnlessModifier` with `Style/SoleNestedConditional`' do
source = <<~RUBY
def foo
Expand Down

0 comments on commit e0342b2

Please sign in to comment.