Skip to content

Commit

Permalink
Merge pull request #12563 from koic/fix_false_positives_for_style_red…
Browse files Browse the repository at this point in the history
…undant_parentheses

[Fix #12556] Fix false positives for `Style/RedundantParentheses`
  • Loading branch information
koic committed Dec 21, 2023
2 parents 2a72b6c + 24fae5e commit 7de40e0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12556](https://github.com/rubocop/rubocop/issues/12556): Fix false positives for `Style/RedundantParentheses` when parentheses are used around a semantic operator in expressions within assignments. ([@koic][])
1 change: 1 addition & 0 deletions lib/rubocop/cop/style/redundant_parentheses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def find_offense_message(begin_node, node)
return if begin_node.chained?

if node.and_type? || node.or_type?
return if node.semantic_operator? && begin_node.parent
return if node.multiline? && allow_in_multiline_conditions?
return if ALLOWED_NODE_TYPES.include?(begin_node.parent&.type)
return if begin_node.parent&.if_type? && begin_node.parent&.ternary?
Expand Down
30 changes: 30 additions & 0 deletions spec/rubocop/cop/style/redundant_parentheses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,36 @@ def x
RUBY
end

it 'registers an offense when the use of parentheses around `&&` expressions in assignment' do
expect_offense(<<~RUBY)
var = (foo && bar)
^^^^^^^^^^^^ Don't use parentheses around a logical expression.
RUBY

expect_correction(<<~RUBY)
var = foo && bar
RUBY
end

it 'registers an offense when the use of parentheses around `||` expressions in assignment' do
expect_offense(<<~RUBY)
var = (foo || bar)
^^^^^^^^^^^^ Don't use parentheses around a logical expression.
RUBY

expect_correction(<<~RUBY)
var = foo || bar
RUBY
end

it 'accepts the use of parentheses around `or` expressions in assignment' do
expect_no_offenses('var = (foo or bar)')
end

it 'accepts the use of parentheses around `and` expressions in assignment' do
expect_no_offenses('var = (foo and bar)')
end

it 'accepts parentheses around a method call with unparenthesized arguments' do
expect_no_offenses('(a 1, 2) && (1 + 1)')
end
Expand Down

0 comments on commit 7de40e0

Please sign in to comment.