From 3d4ba9c2d0782b352bf807cd9fed66ce58e83641 Mon Sep 17 00:00:00 2001 From: Genadi Samokovarov Date: Wed, 12 Dec 2018 18:08:44 +0200 Subject: [PATCH] False positive for parentheses removal in splatted calls Ahem, this is yet-another-false-positive for the `omit_parentheses` style of the `Style/MethodCallWithArgsParentheses` cop. If you are splatting the result of a call, you may end up with the following: ```ruby app/controllers/foo/bar_controller.rb:4:68: C: Style/MethodCallWithArgsParentheses: Omit parentheses for method calls with arguments. render json: FieldSearch.with(search_params).map(&method(:serialize)) ^^^^^^^^^^^^ ``` --- .../cop/style/method_call_with_args_parentheses.rb | 1 + .../style/method_call_with_args_parentheses_spec.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+) 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 2012a4c831f..580fc0a557f 100644 --- a/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +++ b/lib/rubocop/cop/style/method_call_with_args_parentheses.rb @@ -237,6 +237,7 @@ def call_in_literals?(node) node.parent && (node.parent.pair_type? || node.parent.array_type? || + splat?(node.parent) || ternary_if?(node.parent)) 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 b3ba35a4fcd..0834f53d757 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 @@ -414,6 +414,19 @@ def foo RUBY end + it 'accepts parens in splat calls' do + expect_no_offenses(<<-RUBY) + foo(*bar(args)) + foo(**quux(args)) + RUBY + end + + it 'accepts parens in block passing calls' do + expect_no_offenses(<<-RUBY) + foo(&method(:args)) + RUBY + end + it 'auto-corrects single-line calls' do original = <<-RUBY.strip_indent top.test(1, 2, foo: bar(3))