Skip to content

Commit 2873bb8

Browse files
authored
Merge pull request #13938 from koic/fix_false_positive_for_style_redundant_condition
Fix false positives for `Style/RedundantCondition`
2 parents 323cb71 + 9884def commit 2873bb8

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#13938](https://github.com/rubocop/rubocop/pull/13938): Fix false positives for `Style/RedundantCondition` when a variable or a constant is used. ([@koic][])

lib/rubocop/cop/style/redundant_condition.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,8 @@ def if_branch_is_true_type_and_else_is_not?(node)
182182
return false unless node.ternary? || node.if?
183183

184184
cond = node.condition
185-
if cond.call_type? && (!cond.predicate_method? || allowed_method?(cond.method_name))
186-
return false
187-
end
185+
return false unless cond.call_type?
186+
return false if !cond.predicate_method? || allowed_method?(cond.method_name)
188187

189188
node.if_branch&.true_type? && node.else_branch && !node.else_branch.true_type?
190189
end

spec/rubocop/cop/style/redundant_condition_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,58 @@ def do_something(foo, ...)
571571
end
572572

573573
context 'when `true` as the true branch' do
574+
it 'does not register an offense when true is used as the true branch and the condition is a local variable' do
575+
expect_no_offenses(<<~RUBY)
576+
variable = do_something
577+
578+
if variable
579+
true
580+
else
581+
a
582+
end
583+
RUBY
584+
end
585+
586+
it 'does not register an offense when true is used as the true branch and the condition is an instance variable' do
587+
expect_no_offenses(<<~RUBY)
588+
if @variable
589+
true
590+
else
591+
a
592+
end
593+
RUBY
594+
end
595+
596+
it 'does not register an offense when true is used as the true branch and the condition is a class variable' do
597+
expect_no_offenses(<<~RUBY)
598+
if @@variable
599+
true
600+
else
601+
a
602+
end
603+
RUBY
604+
end
605+
606+
it 'does not register an offense when true is used as the true branch and the condition is a global variable' do
607+
expect_no_offenses(<<~RUBY)
608+
if $variable
609+
true
610+
else
611+
a
612+
end
613+
RUBY
614+
end
615+
616+
it 'does not register an offense when true is used as the true branch and the condition is a constant' do
617+
expect_no_offenses(<<~RUBY)
618+
if CONST
619+
true
620+
else
621+
a
622+
end
623+
RUBY
624+
end
625+
574626
it 'does not register an offense when true is used as the true branch and the condition is not a predicate method' do
575627
expect_no_offenses(<<~RUBY)
576628
if a[:key]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy