Skip to content

Commit

Permalink
Add partial autocorrect support for Lint/LiteralAsCondition
Browse files Browse the repository at this point in the history
  • Loading branch information
Zopolis4 committed Aug 14, 2024
1 parent ef0acfd commit f8c74bf
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#13117](https://github.com/rubocop/rubocop/issues/13117): Add partial autocorrect support to `Lint/LiteralAsCondition` cop to check for redundant conditions. ([@zopolis4][])
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2010,6 +2010,7 @@ Lint/LambdaWithoutLiteralBlock:
Lint/LiteralAsCondition:
Description: 'Checks of literals used in conditions.'
Enabled: true
AutoCorrect: contextual
VersionAdded: '0.51'

Lint/LiteralAssignmentInCondition:
Expand Down
35 changes: 33 additions & 2 deletions lib/rubocop/cop/lint/literal_as_condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module Lint
# end
class LiteralAsCondition < Base
include RangeHelp
extend AutoCorrector

MSG = 'Literal `%<literal>s` appeared as a condition.'

Expand Down Expand Up @@ -98,13 +99,43 @@ def message(node)

def check_for_literal(node)
cond = condition(node)
if cond.literal?
add_offense(cond)
if cond.truthy_literal?
autocorrect_redundant_truthy_condition(node, cond)
elsif cond.falsey_literal?
autocorrect_redundant_falsey_condition(node, cond)
else
check_node(cond)
end
end

def autocorrect_redundant_truthy_condition(node, cond)
if node.if_type?
add_offense(cond) do |corrector|
corrector.replace(node, node.if_branch.source)
end
elsif node.while_type? || node.while_post_type?
add_offense(cond) do |corrector|
corrector.replace(cond, 'true')
end
else
add_offense(cond)
end
end

def autocorrect_redundant_falsey_condition(node, cond)
if node.if_type? && node.unless?
add_offense(cond) do |corrector|
corrector.replace(node, node.if_branch.source)
end
elsif node.until_type? || node.until_post_type?
add_offense(cond) do |corrector|
corrector.replace(cond, 'false')
end
else
add_offense(cond)
end
end

def basic_literal?(node)
if node.array_type?
primitive_array?(node)
Expand Down
4 changes: 2 additions & 2 deletions spec/rubocop/cli/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1059,9 +1059,9 @@ def on_send(node)
expect($stdout.string)
.to eq(<<~RESULT)
== example.rb ==
W: 1: 4: Lint/LiteralAsCondition: Literal 0 appeared as a condition.
W: 1: 4: [Correctable] Lint/LiteralAsCondition: Literal 0 appeared as a condition.
1 file inspected, 1 offense detected
1 file inspected, 1 offense detected, 1 offense autocorrectable
RESULT
end
end
Expand Down
32 changes: 32 additions & 0 deletions spec/rubocop/cop/lint/literal_as_condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
top
end
RUBY

expect_correction(<<~RUBY)
top
RUBY
end

it "registers an offense for literal #{lit} in while" do
Expand All @@ -18,6 +22,12 @@
top
end
RUBY

expect_correction(<<~RUBY)
while true
top
end
RUBY
end

it "registers an offense for literal #{lit} in post-loop while" do
Expand Down Expand Up @@ -54,6 +64,8 @@
when x then top
end
RUBY

expect_no_corrections
end

it "registers an offense for literal #{lit} in a when " \
Expand All @@ -64,6 +76,8 @@
^{lit} Literal `#{lit}` appeared as a condition.
end
RUBY

expect_no_corrections
end

it "accepts literal #{lit} in a when of a case with something after case keyword" do
Expand All @@ -90,6 +104,8 @@
in CONST then top
end
RUBY

expect_no_corrections
end

it "accepts literal #{lit} in a when of a case match" do
Expand All @@ -108,6 +124,8 @@
top
end
RUBY

expect_no_corrections
end

it "registers an offense for literal #{lit} in complex cond" do
Expand All @@ -117,6 +135,8 @@
top
end
RUBY

expect_no_corrections
end

it "registers an offense for literal #{lit} in !" do
Expand All @@ -126,6 +146,8 @@
top
end
RUBY

expect_no_corrections
end

it "registers an offense for literal #{lit} in complex !" do
Expand All @@ -135,6 +157,8 @@
top
end
RUBY

expect_no_corrections
end

it "accepts literal #{lit} if it's not an and/or operand" do
Expand All @@ -158,13 +182,17 @@
!%{lit}
^{lit} Literal `#{lit}` appeared as a condition.
RUBY

expect_no_corrections
end

it "registers an offense for `not #{lit}`" do
expect_offense(<<~RUBY, lit: lit)
not(%{lit})
^{lit} Literal `#{lit}` appeared as a condition.
RUBY

expect_no_corrections
end
end

Expand All @@ -191,6 +219,8 @@
when [1, 2, 5] then top
end
RUBY

expect_no_corrections
end

it 'accepts dstr literal in case' do
Expand Down Expand Up @@ -225,6 +255,8 @@
in [1, 2, 5] then top
end
RUBY

expect_no_corrections
end

it 'accepts an offense for case match with a match var' do
Expand Down

0 comments on commit f8c74bf

Please sign in to comment.