From 98ff307edd33f26134df013493b4c214c988fff3 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 20 Nov 2019 09:56:43 +0900 Subject: [PATCH] [Fix #7515] Fix a false negative for `Style/RedundantParentheses` Fixes #7515. This PR fixes a false negative for `Style/RedundantParentheses` when calling a method with safe navigation operator. --- CHANGELOG.md | 1 + lib/rubocop/cop/lint/redundant_splat_expansion.rb | 2 +- lib/rubocop/cop/style/redundant_parentheses.rb | 6 +++--- lib/rubocop/cop/style/redundant_sort.rb | 2 +- spec/rubocop/cop/style/redundant_parentheses_spec.rb | 1 + 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8dd55060d07..cdd6ec81910 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * [#7493](https://github.com/rubocop-hq/rubocop/issues/7493): Fix `Style/RedundantReturn` to inspect conditional constructs that are preceded by other statements. ([@buehmann][]) * [#7509](https://github.com/rubocop-hq/rubocop/issues/7509): Fix `Layout/SpaceInsideArrayLiteralBrackets` to correct empty lines. ([@ayacai115][]) * [#7517](https://github.com/rubocop-hq/rubocop/issues/7517): `Style/SpaceAroundKeyword` allows `::` after `super`. ([@ozydingo][]) +* [#7515](https://github.com/rubocop-hq/rubocop/issues/7515): Fix a false negative for `Style/RedundantParentheses` when calling a method with safe navigation operator. ([@koic][]) ### Changes diff --git a/lib/rubocop/cop/lint/redundant_splat_expansion.rb b/lib/rubocop/cop/lint/redundant_splat_expansion.rb index 4f52137c6f0..050637d221f 100644 --- a/lib/rubocop/cop/lint/redundant_splat_expansion.rb +++ b/lib/rubocop/cop/lint/redundant_splat_expansion.rb @@ -146,7 +146,7 @@ def redundant_brackets?(node) grandparent = node.parent.parent parent.when_type? || parent.send_type? || part_of_an_array?(node) || - (grandparent&.resbody_type?) + grandparent&.resbody_type? end def remove_brackets(array) diff --git a/lib/rubocop/cop/style/redundant_parentheses.rb b/lib/rubocop/cop/style/redundant_parentheses.rb index 1d1c1a38e61..64abfd7c442 100644 --- a/lib/rubocop/cop/style/redundant_parentheses.rb +++ b/lib/rubocop/cop/style/redundant_parentheses.rb @@ -19,7 +19,7 @@ class RedundantParentheses < Cop def_node_matcher :square_brackets?, '(send {(send _recv _msg) str array hash} :[] ...)' def_node_matcher :range_end?, '^^{irange erange}' - def_node_matcher :method_node_and_args, '$(send _recv _msg $...)' + def_node_matcher :method_node_and_args, '$(call _recv _msg $...)' def_node_matcher :rescue?, '{^resbody ^^resbody}' def_node_matcher :arg_in_call_with_block?, '^^(block (send _ _ equal?(%0) ...) ...)' @@ -102,7 +102,7 @@ def check(begin_node) return offense(begin_node, 'a variable') if node.variable? return offense(begin_node, 'a constant') if node.const_type? - check_send(begin_node, node) if node.send_type? + check_send(begin_node, node) if node.call_type? end def check_send(begin_node, node) @@ -195,7 +195,7 @@ def keyword_with_redundant_parentheses?(node) end def method_call_with_redundant_parentheses?(node) - return false unless node.send_type? + return false unless node.call_type? return false if node.prefix_not? return false if range_end?(node) diff --git a/lib/rubocop/cop/style/redundant_sort.rb b/lib/rubocop/cop/style/redundant_sort.rb index bdbe02ef7f9..4621bb53da8 100644 --- a/lib/rubocop/cop/style/redundant_sort.rb +++ b/lib/rubocop/cop/style/redundant_sort.rb @@ -127,7 +127,7 @@ def suggestion(sorter, accessor, arg) end def base(accessor, arg) - if accessor == :first || (arg&.zero?) + if accessor == :first || arg&.zero? 'min' elsif accessor == :last || arg == -1 'max' diff --git a/spec/rubocop/cop/style/redundant_parentheses_spec.rb b/spec/rubocop/cop/style/redundant_parentheses_spec.rb index 75bb48cd0fc..cdc1ba3c69f 100644 --- a/spec/rubocop/cop/style/redundant_parentheses_spec.rb +++ b/spec/rubocop/cop/style/redundant_parentheses_spec.rb @@ -88,6 +88,7 @@ it_behaves_like 'redundant', '(x)', 'x', 'a method call' it_behaves_like 'redundant', '(x(1, 2))', 'x(1, 2)', 'a method call' it_behaves_like 'redundant', '("x".to_sym)', '"x".to_sym', 'a method call' + it_behaves_like 'redundant', '("x"&.to_sym)', '"x"&.to_sym', 'a method call' it_behaves_like 'redundant', '(x[:y])', 'x[:y]', 'a method call' it_behaves_like 'redundant', '("foo"[0])', '"foo"[0]', 'a method call' it_behaves_like 'redundant', '(["foo"][0])', '["foo"][0]', 'a method call'