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 an incorrect auto-correct for Style/MethodCallWithArgsParentheses #9715

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 @@
* [#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