Skip to content

Commit 5912850

Browse files
authored
Merge pull request #10609 from nobuyo/fix-autocorrect-for-style-redundant-condition-with-parentheses
[Fix #10607] Fix autocorrect for `Style/RedundantCondition` with parenthesized method call
2 parents 0866f79 + ea7f73f commit 5912850

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#10607](https://github.com/rubocop/rubocop/issues/10607): Fix autocorrect for `Style/RedundantCondition` when there are parenthesized method calls in each branch. ([@nobuyo][])

lib/rubocop/cop/style/redundant_condition.rb

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ def on_if(node)
4444
message = message(node)
4545

4646
add_offense(range_of_offense(node), message: message) do |corrector|
47-
if node.ternary?
47+
if node.ternary? && !branches_have_method?(node)
4848
correct_ternary(corrector, node)
49-
elsif node.modifier_form? || !node.else_branch
49+
elsif redudant_condition?(node)
5050
corrector.replace(node, node.if_branch.source)
5151
else
5252
corrected = make_ternary_form(node)
@@ -59,7 +59,7 @@ def on_if(node)
5959
private
6060

6161
def message(node)
62-
if node.modifier_form? || !node.else_branch
62+
if redudant_condition?(node)
6363
REDUNDANT_CONDITION
6464
else
6565
MSG
@@ -68,6 +68,7 @@ def message(node)
6868

6969
def range_of_offense(node)
7070
return node.loc.expression unless node.ternary?
71+
return node.loc.expression if node.ternary? && branches_have_method?(node)
7172

7273
range_between(node.loc.question.begin_pos, node.loc.colon.end_pos)
7374
end
@@ -81,6 +82,10 @@ def offense?(node)
8182
(node.ternary? || !else_branch.instance_of?(AST::Node) || else_branch.single_line?)
8283
end
8384

85+
def redudant_condition?(node)
86+
node.modifier_form? || !node.else_branch
87+
end
88+
8489
def use_if_branch?(else_branch)
8590
else_branch&.if_type?
8691
end
@@ -89,6 +94,10 @@ def use_hash_key_assignment?(else_branch)
8994
else_branch&.send_type? && else_branch&.method?(:[]=)
9095
end
9196

97+
def use_hash_key_access?(node)
98+
node.send_type? && node.method?(:[])
99+
end
100+
92101
def synonymous_condition_and_branch?(node)
93102
condition, if_branch, _else_branch = *node
94103
# e.g.
@@ -113,7 +122,8 @@ def synonymous_condition_and_branch?(node)
113122
# else
114123
# test.value = another_value?
115124
# end
116-
branches_have_method?(node) && condition == if_branch.first_argument
125+
branches_have_method?(node) && condition == if_branch.first_argument &&
126+
!use_hash_key_access?(if_branch)
117127
end
118128

119129
def branches_have_assignment?(node)
@@ -140,6 +150,14 @@ def branches_have_method?(node)
140150
if_branch.method?(else_branch.method_name)
141151
end
142152

153+
def if_source(if_branch)
154+
if branches_have_method?(if_branch.parent) && if_branch.parenthesized?
155+
if_branch.source.delete_suffix(')')
156+
else
157+
if_branch.source
158+
end
159+
end
160+
143161
def else_source(else_branch)
144162
if branches_have_method?(else_branch.parent)
145163
else_source_if_has_method(else_branch)
@@ -176,7 +194,8 @@ def else_source_if_has_assignment(else_branch)
176194

177195
def make_ternary_form(node)
178196
_condition, if_branch, else_branch = *node
179-
ternary_form = [if_branch.source, else_source(else_branch)].join(' || ')
197+
ternary_form = [if_source(if_branch), else_source(else_branch)].join(' || ')
198+
ternary_form += ')' if branches_have_method?(node) && if_branch.parenthesized?
180199

181200
if node.parent&.send_type?
182201
"(#{ternary_form})"

spec/rubocop/cop/style/redundant_condition_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,21 @@ def test
322322
RUBY
323323
end
324324

325+
it 'registers an offense and corrects when the branches contains parenthesized method call' do
326+
expect_offense(<<~RUBY)
327+
if foo
328+
^^^^^^ Use double pipes `||` instead.
329+
bar(foo)
330+
else
331+
bar(1..2)
332+
end
333+
RUBY
334+
335+
expect_correction(<<~RUBY)
336+
bar(foo || (1..2))
337+
RUBY
338+
end
339+
325340
it 'does not register offenses when using `nil?` and the branches contains assignment' do
326341
expect_no_offenses(<<~RUBY)
327342
if foo.nil?
@@ -351,6 +366,16 @@ def test
351366
end
352367
RUBY
353368
end
369+
370+
it 'does not register offenses when the branches contains hash key access' do
371+
expect_no_offenses(<<~RUBY)
372+
if foo
373+
bar[foo]
374+
else
375+
bar[1]
376+
end
377+
RUBY
378+
end
354379
end
355380
end
356381

@@ -426,6 +451,28 @@ def test
426451
RUBY
427452
end
428453

454+
it 'registers an offense and corrects with ternary expression and the branches contains parenthesized method call' do
455+
expect_offense(<<~RUBY)
456+
foo ? bar(foo) : bar(quux)
457+
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use double pipes `||` instead.
458+
RUBY
459+
460+
expect_correction(<<~RUBY)
461+
bar(foo || quux)
462+
RUBY
463+
end
464+
465+
it 'registers an offense and corrects with ternary expression and the branches contains chained parenthesized method call' do
466+
expect_offense(<<~RUBY)
467+
foo ? foo(foo).bar(foo) : foo(foo).bar(quux)
468+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use double pipes `||` instead.
469+
RUBY
470+
471+
expect_correction(<<~RUBY)
472+
foo(foo).bar(foo || quux)
473+
RUBY
474+
end
475+
429476
it 'registers an offense and corrects when the else branch contains `rescue`' do
430477
expect_offense(<<~RUBY)
431478
if a

0 commit comments

Comments
 (0)