diff --git a/changelog/fix_fix_false_positive_for.md b/changelog/fix_fix_false_positive_for.md new file mode 100644 index 00000000000..5667778753d --- /dev/null +++ b/changelog/fix_fix_false_positive_for.md @@ -0,0 +1 @@ +* [#9707](https://github.com/rubocop/rubocop/issues/9707): Fix false positive for `Style/MethodCallWithArgsParentheses` with `omit_parentheses` style on an endless `defs` node. ([@dvandersluis][]) diff --git a/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb b/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb index 76edaf437e3..ebe2298276d 100644 --- a/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +++ b/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb @@ -42,7 +42,7 @@ def offense_range(node) def inside_endless_method_def?(node) # parens are required around arguments inside an endless method - node.each_ancestor(:def).any?(&:endless?) && node.arguments.any? + node.each_ancestor(:def, :defs).any?(&:endless?) && node.arguments.any? end def syntax_like_method_call?(node) diff --git a/spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb b/spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb index 1c0464821ae..906a8574735 100644 --- a/spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb +++ b/spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb @@ -23,12 +23,29 @@ def x() = foo() def x() = foo#{trailing_whitespace} RUBY end + + it 'registers an offense for `defs` when there are parens' do + expect_offense(<<~RUBY) + def self.x() = foo() + ^^ Omit parentheses for method calls with arguments. + RUBY + + expect_correction(<<~RUBY) + def self.x() = foo#{trailing_whitespace} + RUBY + end else it 'does not register an offense when there are parens' do expect_no_offenses(<<~RUBY) def x() = foo() RUBY end + + it 'does not register an offense for `defs` when there are parens' do + expect_no_offenses(<<~RUBY) + def self.x() = foo() + RUBY + end end it 'does not register an offense when there are no parens' do @@ -36,6 +53,18 @@ def x() = foo() def x() = foo RUBY end + + it 'does not register an offense when there are arguments' do + expect_no_offenses(<<~RUBY) + def x() = foo(y) + RUBY + end + + it 'does not register an offense for `defs` when there are arguments' do + expect_no_offenses(<<~RUBY) + def self.x() = foo(y) + RUBY + end end end end