Skip to content

Commit

Permalink
[Fix #7515] Fix a false negative for Style/RedundantParentheses
Browse files Browse the repository at this point in the history
Fixes #7515.

This PR fixes a false negative for `Style/RedundantParentheses` when
calling a method with safe navigation operator.
  • Loading branch information
koic committed Nov 22, 2019
1 parent aa09fd6 commit 98ff307
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/redundant_splat_expansion.rb
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/style/redundant_parentheses.rb
Expand Up @@ -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) ...) ...)'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/redundant_sort.rb
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions spec/rubocop/cop/style/redundant_parentheses_spec.rb
Expand Up @@ -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'
Expand Down

0 comments on commit 98ff307

Please sign in to comment.