Skip to content

Commit

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

```console
% cat example.rb
do_something arg rescue nil

% bundle exec rubocop -a --only
Style/MethodCallWithArgsParentheses,Style/RescueModifier
(snip)

Inspecting 1 file
C

Offenses:

example.rb:1:1: C: [Corrected] Style/MethodCallWithArgsParentheses: Use
parentheses for method calls with arguments.
do_something arg rescue nil
^^^^^^^^^^^^^^^^
example.rb:1:1: C: [Corrected] Style/RescueModifier: Avoid using rescue
in its modifier form.
do_something arg rescue nil
^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected, 2 offenses corrected
```

## Before

Auto-corrected to invalid syntax.

```ruby
% cat example.rb
begin
  do_something(arg
rescue
  nil
end)

% ruby -c example.rb
example.rb:3: syntax error, unexpected `rescue', expecting ')'
rescue
example.rb:5: syntax error, unexpected ')', expecting end-of-input
```

## After

Auto-corrected to valid syntax.

```ruby
% cat example.rb
begin
  do_something(arg)
rescue
  nil
end
```
  • Loading branch information
koic authored and bbatsov committed Apr 20, 2021
1 parent bc6f98b commit cd3db12
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
@@ -0,0 +1 @@
* [#9715](https://github.com/rubocop/rubocop/pull/9715): Fix an incorrect auto-correct for `EnforcedStyle: require_parentheses` of `Style/MethodCallWithArgsParentheses` with `Style/RescueModifier`. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/method_call_with_args_parentheses.rb
Expand Up @@ -202,7 +202,7 @@ class MethodCallWithArgsParentheses < Base
extend AutoCorrector

def self.autocorrect_incompatible_with
[Style::NestedParenthesizedCalls]
[Style::NestedParenthesizedCalls, Style::RescueModifier]
end

def on_send(node)
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/cop/style/rescue_modifier.rb
Expand Up @@ -47,6 +47,10 @@ class RescueModifier < Base

MSG = 'Avoid using `rescue` in its modifier form.'

def self.autocorrect_incompatible_with
[Style::MethodCallWithArgsParentheses]
end

def on_resbody(node)
return unless rescue_modifier?(node)

Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Expand Up @@ -214,6 +214,28 @@ def batch
RUBY
end

it 'corrects `EnforcedStyle: require_parentheses` of `Style/MethodCallWithArgsParentheses` with `Style/RescueModifier`' do
create_file('.rubocop.yml', <<~YAML)
Style/MethodCallWithArgsParentheses:
EnforcedStyle: require_parentheses
YAML
source = <<~RUBY
do_something arg rescue nil
RUBY
create_file('example.rb', source)
expect(cli.run([
'--auto-correct-all',
'--only', 'Style/MethodCallWithArgsParentheses,Style/RescueModifier'
])).to eq(0)
expect(File.read('example.rb')).to eq(<<~RUBY)
begin
do_something(arg)
rescue
nil
end
RUBY
end

it 'corrects `EnforcedStyle: require_parentheses` of `Style/MethodCallWithArgsParentheses` with ' \
'`EnforcedStyle: conditionals` of `Style/AndOr`' do
create_file('.rubocop.yml', <<~YAML)
Expand Down

0 comments on commit cd3db12

Please sign in to comment.