From e0342b2fbcefc40f52571efa44de4f63da5758dc Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 3 Apr 2021 09:15:29 +0900 Subject: [PATCH] Fix an incorrect auto-correct for `Lint/AmbiguousOperator` 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 ``` --- ...rect_autocorrect_for_ambiguous_operator.md | 1 + lib/rubocop/cop/util.rb | 5 +++- spec/rubocop/cli/autocorrect_spec.rb | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_incorrect_autocorrect_for_ambiguous_operator.md diff --git a/changelog/fix_incorrect_autocorrect_for_ambiguous_operator.md b/changelog/fix_incorrect_autocorrect_for_ambiguous_operator.md new file mode 100644 index 00000000000..f1142eb4fac --- /dev/null +++ b/changelog/fix_incorrect_autocorrect_for_ambiguous_operator.md @@ -0,0 +1 @@ +* [#9671](https://github.com/rubocop/rubocop/pull/9671): Fix an incorrect auto-correct for `Lint/AmbiguousOperator` with `Style/MethodCallWithArgsParentheses`. ([@koic][]) diff --git a/lib/rubocop/cop/util.rb b/lib/rubocop/cop/util.rb index 7dc59c90513..6916c8da532 100644 --- a/lib/rubocop/cop/util.rb +++ b/lib/rubocop/cop/util.rb @@ -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 diff --git a/spec/rubocop/cli/autocorrect_spec.rb b/spec/rubocop/cli/autocorrect_spec.rb index 4b66eb090ee..5aa956e18f0 100644 --- a/spec/rubocop/cli/autocorrect_spec.rb +++ b/spec/rubocop/cli/autocorrect_spec.rb @@ -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