diff --git a/changelog/fix_a_false_positive_for_style_method_call_with_args_parentheses.md b/changelog/fix_a_false_positive_for_style_method_call_with_args_parentheses.md new file mode 100644 index 000000000000..fcc7f8e4daae --- /dev/null +++ b/changelog/fix_a_false_positive_for_style_method_call_with_args_parentheses.md @@ -0,0 +1 @@ +* [#13018](https://github.com/rubocop/rubocop/issues/13018): Fix a false positive for `Style/MethodCallWithArgsParentheses` when `EnforcedStyle: omit_parentheses` is set and parenthesized method call is used before constant resolution. ([@koic][]) 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 b48a80b2883a..910af36977a2 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 @@ -18,6 +18,7 @@ def omit_parentheses(node) # rubocop:disable Metrics/PerceivedComplexity return if inside_endless_method_def?(node) return if require_parentheses_for_hash_value_omission?(node) return if syntax_like_method_call?(node) + return if method_call_before_constant_resolution?(node) return if super_call_without_arguments?(node) return if legitimate_call_with_parentheses?(node) return if allowed_camel_case_method_call?(node) @@ -63,6 +64,10 @@ def syntax_like_method_call?(node) node.implicit_call? || node.operator_method? end + def method_call_before_constant_resolution?(node) + node.parent&.const_type? + end + def super_call_without_arguments?(node) node.super_type? && node.arguments.none? end 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 de610bf1b3cc..b3d1401e98cc 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 @@ -703,6 +703,12 @@ class Point < Struct.new :x, :y RUBY end + it 'accepts parenthesized method calls before constant resolution' do + expect_no_offenses(<<~RUBY) + do_something(arg)::CONST + RUBY + end + it 'accepts parens in single-line inheritance' do expect_no_offenses(<<-RUBY) class Point < Struct.new(:x, :y); end