Skip to content

Commit

Permalink
[Fix rubocop#12537] Fix false positives for Style/RedundantParentheses
Browse files Browse the repository at this point in the history
Fixes rubocop#12537.

This PR fixes false positives for `Style/RedundantParentheses`
when `AllowInMultilineConditions: true` of `Style/ParenthesesAroundCondition`.
  • Loading branch information
koic committed Dec 15, 2023
1 parent baa4a97 commit 3ce1ca0
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_false_positives_for_redundant_parentheses.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12537](https://github.com/rubocop/rubocop/issues/12537): Fix false positives for `Style/RedundantParentheses` when `AllowInMultilineConditions: true` of `Style/ParenthesesAroundCondition`. ([@koic][])
8 changes: 8 additions & 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.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 All @@ -165,6 +166,13 @@ def find_offense_message(begin_node, node)
# @!method interpolation?(node)
def_node_matcher :interpolation?, '[^begin ^^dstr]'

def allow_in_multiline_conditions?
parentheses_around_condition_config = config.for_cop('Style/ParenthesesAroundCondition')
return false unless parentheses_around_condition_config['Enabled']

!!parentheses_around_condition_config['AllowInMultilineConditions']
end

def check_send(begin_node, node)
return check_unary(begin_node, node) if node.unary_operation?

Expand Down
21 changes: 21 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,27 @@ def batch
RUBY
end

it 'does not correct `AllowInMultilineConditions: true` of `Style/ParenthesesAroundCondition` with `Style/RedundantParentheses`' do
create_file('.rubocop.yml', <<~YAML)
Style/ParenthesesAroundCondition:
AllowInMultilineConditions: true
YAML
source = <<~RUBY
if (foo &&
bar)
end
RUBY
create_file('example.rb', source)
expect(cli.run(['--autocorrect', '--only',
'Style/ParenthesesAroundCondition,' \
'Style/RedundantParentheses'])).to eq(0)
expect(File.read('example.rb')).to eq(<<~RUBY)
if (foo &&
bar)
end
RUBY
end

it 'corrects `EnforcedShorthandSyntax: always` of `Style/HashSyntax` with `Style/RedundantParentheses` when using Ruby 3.1' do
create_file('.rubocop.yml', <<~YAML)
AllCops:
Expand Down
105 changes: 105 additions & 0 deletions spec/rubocop/cop/style/redundant_parentheses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -850,4 +850,109 @@ def foo
it_behaves_like 'allowed parentheses', '1..2', 'a range literal'
it_behaves_like 'allowed parentheses', '1', 'an int literal'
end

context 'when `AllowInMultilineConditions: true` of `Style/ParenthesesAroundCondition`' do
let(:other_cops) do
{
'Style/ParenthesesAroundCondition' => {
'Enabled' => enabled, 'AllowInMultilineConditions' => true
}
}
end

context 'when `Style/ParenthesesAroundCondition` is enabled' do
let(:enabled) { true }

context 'when singe line conditions' do
it_behaves_like 'redundant', '(x && y)', 'x && y', 'a logical expression'
it_behaves_like 'redundant', '(x || y)', 'x || y', 'a logical expression'
it_behaves_like 'redundant', '(x and y)', 'x and y', 'a logical expression'
it_behaves_like 'redundant', '(x or y)', 'x or y', 'a logical expression'
end

context 'when multiline conditions' do
it_behaves_like 'plausible', <<~RUBY
(x &&
y)
RUBY
it_behaves_like 'plausible', <<~RUBY
(x ||
y)
RUBY
it_behaves_like 'plausible', <<~RUBY
(x and
y)
RUBY
it_behaves_like 'plausible', <<~RUBY
(x or
y)
RUBY
end
end

context 'when `Style/ParenthesesAroundCondition` is disabled' do
let(:enabled) { false }

context 'when singe line conditions' do
it_behaves_like 'redundant', '(x && y)', 'x && y', 'a logical expression'
it_behaves_like 'redundant', '(x || y)', 'x || y', 'a logical expression'
it_behaves_like 'redundant', '(x and y)', 'x and y', 'a logical expression'
it_behaves_like 'redundant', '(x or y)', 'x or y', 'a logical expression'
end

context 'when multiline conditions' do
it 'registers an offense when using `&&`' do
expect_offense(<<~RUBY)
(x &&
^^^^^ Don't use parentheses around a logical expression.
y)
RUBY

expect_correction(<<~RUBY)
x &&
y
RUBY
end

it 'registers an offense when using `||`' do
expect_offense(<<~RUBY)
(x ||
^^^^^ Don't use parentheses around a logical expression.
y)
RUBY

expect_correction(<<~RUBY)
x ||
y
RUBY
end

it 'registers an offense when using `and`' do
expect_offense(<<~RUBY)
(x and
^^^^^^ Don't use parentheses around a logical expression.
y)
RUBY

expect_correction(<<~RUBY)
x and
y
RUBY
end

it 'registers an offense when using `or`' do
expect_offense(<<~RUBY)
(x or
^^^^^ Don't use parentheses around a logical expression.
y)
RUBY

expect_correction(<<~RUBY)
x or
y
RUBY
end
end
end
end
end

0 comments on commit 3ce1ca0

Please sign in to comment.