From cd3db1247c060e183b34012797641d2d9ed99443 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 20 Apr 2021 00:30:29 +0900 Subject: [PATCH] Fix an incorrect auto-correct for `Style/MethodCallWithArgsParentheses` 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 ``` --- ...style_method_call_with_args_parentheses.md | 1 + .../method_call_with_args_parentheses.rb | 2 +- lib/rubocop/cop/style/rescue_modifier.rb | 4 ++++ spec/rubocop/cli/autocorrect_spec.rb | 22 +++++++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_incorrect_autocorrect_for_style_method_call_with_args_parentheses.md diff --git a/changelog/fix_incorrect_autocorrect_for_style_method_call_with_args_parentheses.md b/changelog/fix_incorrect_autocorrect_for_style_method_call_with_args_parentheses.md new file mode 100644 index 00000000000..386c74e3799 --- /dev/null +++ b/changelog/fix_incorrect_autocorrect_for_style_method_call_with_args_parentheses.md @@ -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][]) diff --git a/lib/rubocop/cop/style/method_call_with_args_parentheses.rb b/lib/rubocop/cop/style/method_call_with_args_parentheses.rb index b8deed03fbf..bb33f391586 100644 --- a/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +++ b/lib/rubocop/cop/style/method_call_with_args_parentheses.rb @@ -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) diff --git a/lib/rubocop/cop/style/rescue_modifier.rb b/lib/rubocop/cop/style/rescue_modifier.rb index 0b7b97997cf..23285ad0fe7 100644 --- a/lib/rubocop/cop/style/rescue_modifier.rb +++ b/lib/rubocop/cop/style/rescue_modifier.rb @@ -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) diff --git a/spec/rubocop/cli/autocorrect_spec.rb b/spec/rubocop/cli/autocorrect_spec.rb index 0bc2df404ea..2ddb77aa0c7 100644 --- a/spec/rubocop/cli/autocorrect_spec.rb +++ b/spec/rubocop/cli/autocorrect_spec.rb @@ -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)