Skip to content

Commit

Permalink
Merge pull request rubocop#12539 from koic/fix_false_positives_for_li…
Browse files Browse the repository at this point in the history
…nt_literal_assignment_in_condition

Fix false positives for `Lint/LiteralAssignmentInCondition`
  • Loading branch information
koic committed Dec 14, 2023
2 parents 7c80c79 + a8fdeb0 commit baa4a97
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
@@ -0,0 +1 @@
* [#12539](https://github.com/rubocop/rubocop/pull/12539): Fix false positives for `Lint/LiteralAssignmentInCondition` when a collection literal contains non-literal elements. ([@koic][])
17 changes: 12 additions & 5 deletions lib/rubocop/cop/lint/literal_assignment_in_condition.rb
Expand Up @@ -41,7 +41,7 @@ def on_if(node)
next unless asgn_node.loc.operator

rhs = asgn_node.to_a.last
next if !forbidden_literal?(rhs) || parallel_assignment_with_splat_operator?(rhs)
next if !all_literals?(rhs) || parallel_assignment_with_splat_operator?(rhs)

range = offense_range(asgn_node, rhs)

Expand All @@ -59,10 +59,17 @@ def traverse_node(node, &block)
node.each_child_node { |child| traverse_node(child, &block) }
end

def forbidden_literal?(node)
return false if node.dstr_type? || node.xstr_type?

node.respond_to?(:literal?) && node.literal?
def all_literals?(node)
case node.type
when :dstr, :xstr
false
when :array
node.values.all? { |value| all_literals?(value) }
when :hash
(node.values + node.keys).all? { |item| all_literals?(item) }
else
node.respond_to?(:literal?) && node.literal?
end
end

def parallel_assignment_with_splat_operator?(node)
Expand Down
47 changes: 46 additions & 1 deletion spec/rubocop/cop/lint/literal_assignment_in_condition_spec.rb
Expand Up @@ -31,14 +31,59 @@
RUBY
end

it 'registers an offense when assigning array literal to local variable in `if` condition' do
it 'registers an offense when assigning empty array literal to local variable in `if` condition' do
expect_offense(<<~RUBY)
if test = []
^^^^ Don't use literal assignment `= []` in conditional, should be `==` or non-literal operand.
end
RUBY
end

it 'registers an offense when assigning array literal with only literal elements to local variable in `if` condition' do
expect_offense(<<~RUBY)
if test = [1, 2, 3]
^^^^^^^^^^^ Don't use literal assignment `= [1, 2, 3]` in conditional, should be `==` or non-literal operand.
end
RUBY
end

it 'does not register an offense when assigning array literal with non-literal elements to local variable in `if` condition' do
expect_no_offenses(<<~RUBY)
if test = [42, x, y]
end
RUBY
end

it 'registers an offense when assigning empty hash literal to local variable in `if` condition' do
expect_offense(<<~RUBY)
if test = {}
^^^^ Don't use literal assignment `= {}` in conditional, should be `==` or non-literal operand.
end
RUBY
end

it 'registers an offense when assigning hash literal with only literal elements to local variable in `if` condition' do
expect_offense(<<~RUBY)
if test = {x: :y}
^^^^^^^^^ Don't use literal assignment `= {x: :y}` in conditional, should be `==` or non-literal operand.
end
RUBY
end

it 'does not register an offense when assigning hash literal with non-literal key to local variable in `if` condition' do
expect_no_offenses(<<~RUBY)
if test = {x => :y}
end
RUBY
end

it 'does not register an offense when assigning hash literal with non-literal value to local variable in `if` condition' do
expect_no_offenses(<<~RUBY)
if test = {x: y}
end
RUBY
end

it 'does not register an offense when assigning non-literal to local variable in `if` condition' do
expect_no_offenses(<<~RUBY)
if test = do_something
Expand Down

0 comments on commit baa4a97

Please sign in to comment.