From 695710e3a28e68fa9688934a6d20aea9232de73a Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 28 Nov 2020 17:13:22 +0900 Subject: [PATCH] Fix a false positive for `Style/MethodCallWithoutArgsParentheses` This PR fixes the following false positive for `Style/MethodCallWithoutArgsParentheses` when assigning to a default argument with the same name. ```console % cat example.rb def foo(test = test()) end % ruby -c example.rb Syntax OK % rubocop --only Style/MethodCallWithoutArgsParentheses -a example.rb Inspecting 1 file C Offenses: example.rb:1:20: C: [Corrected] Style/MethodCallWithoutArgsParentheses: Do not use parentheses for method calls with no arguments. def foo(test = test()) ^^ 1 file inspected, 1 offense detected, 1 offense corrected % cat example.rb def foo(test = test) end % ruby -c example.rb example.rb:1: circular argument reference - test ``` It prevents `example.rb:1: circular argument reference - test`. --- ...tive_for_method_call_without_args_parentheses.md | 1 + .../style/method_call_without_args_parentheses.rb | 13 +++++++++++-- .../method_call_without_args_parentheses_spec.rb | 7 +++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_false_positive_for_method_call_without_args_parentheses.md diff --git a/changelog/fix_false_positive_for_method_call_without_args_parentheses.md b/changelog/fix_false_positive_for_method_call_without_args_parentheses.md new file mode 100644 index 00000000000..5e3d265ab15 --- /dev/null +++ b/changelog/fix_false_positive_for_method_call_without_args_parentheses.md @@ -0,0 +1 @@ +* [#9113](https://github.com/rubocop-hq/rubocop/pull/9113): Fix a false positive for `Style/MethodCallWithoutArgsParentheses` when assigning to a default argument with the same name. ([@koic][]) diff --git a/lib/rubocop/cop/style/method_call_without_args_parentheses.rb b/lib/rubocop/cop/style/method_call_without_args_parentheses.rb index f27500c34b1..fb1e0351484 100644 --- a/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +++ b/lib/rubocop/cop/style/method_call_without_args_parentheses.rb @@ -21,21 +21,30 @@ class MethodCallWithoutArgsParentheses < Base def on_send(node) return unless !node.arguments? && node.parenthesized? return if ineligible_node?(node) + return if default_argument?(node) return if ignored_method?(node.method_name) return if same_name_assignment?(node) + register_offense(node) + end + + private + + def register_offense(node) add_offense(offense_range(node)) do |corrector| corrector.remove(node.loc.begin) corrector.remove(node.loc.end) end end - private - def ineligible_node?(node) node.camel_case_method? || node.implicit_call? || node.prefix_not? end + def default_argument?(node) + node.parent&.optarg_type? + end + def same_name_assignment?(node) any_assignment?(node) do |asgn_node| next variable_in_mass_assignment?(node.method_name, asgn_node) if asgn_node.masgn_type? diff --git a/spec/rubocop/cop/style/method_call_without_args_parentheses_spec.rb b/spec/rubocop/cop/style/method_call_without_args_parentheses_spec.rb index 345a4e1e9ab..6646d55a41a 100644 --- a/spec/rubocop/cop/style/method_call_without_args_parentheses_spec.rb +++ b/spec/rubocop/cop/style/method_call_without_args_parentheses_spec.rb @@ -56,6 +56,13 @@ expect_no_offenses('test = test()') end + it 'accepts parens in default argument assignment' do + expect_no_offenses(<<~RUBY) + def foo(test = test()) + end + RUBY + end + it 'accepts parens in shorthand assignment' do expect_no_offenses('test ||= test()') end