Skip to content

Commit 910795c

Browse files
authored
Merge pull request #13409 from pCosta99/issue/13395
[Fix #13395] False positive for `Lint/UselessAssignment` when assigning in branch and block
2 parents 0d8d9ed + af07c1c commit 910795c

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#13395](https://github.com/rubocop/rubocop/issues/13395): Fix a false positive for `Lint/UselessAssignment` when assigning in branch and block. ([@pCosta99][])

lib/rubocop/cop/variable_force/variable.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class VariableForce
66
# A Variable represents existence of a local variable.
77
# This holds a variable declaration node and some states of the variable.
88
class Variable
9+
extend NodePattern::Macros
10+
911
VARIABLE_DECLARATION_TYPES = (VARIABLE_ASSIGNMENT_TYPES + ARGUMENT_DECLARATION_TYPES).freeze
1012

1113
attr_reader :name, :declaration_node, :scope, :assignments, :references, :captured_by_block
@@ -31,11 +33,21 @@ def initialize(name, declaration_node, scope)
3133
def assign(node)
3234
assignment = Assignment.new(node, self)
3335

34-
@assignments.last&.reassigned! unless captured_by_block?
36+
mark_last_as_reassigned!(assignment)
3537

3638
@assignments << assignment
3739
end
3840

41+
def mark_last_as_reassigned!(assignment)
42+
return if captured_by_block?
43+
return if candidate_condition?(assignment.node.parent)
44+
45+
@assignments.last&.reassigned!
46+
end
47+
48+
# @!method candidate_condition?(node)
49+
def_node_matcher :candidate_condition?, '[{if case case_match when}]'
50+
3951
def referenced?
4052
!@references.empty?
4153
end

spec/rubocop/cop/lint/useless_assignment_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,51 @@ def some_method
551551
end
552552
end
553553

554+
context 'when assigning in branch' do
555+
it 'accepts' do
556+
expect_no_offenses(<<~RUBY)
557+
def some_method
558+
changed = false
559+
560+
if Random.rand > 1
561+
changed = true
562+
end
563+
564+
[].each do
565+
changed = true
566+
end
567+
568+
puts changed
569+
end
570+
RUBY
571+
end
572+
end
573+
574+
context 'when assigning in case' do
575+
it 'accepts' do
576+
expect_no_offenses(<<~RUBY)
577+
def some_method
578+
changed = false
579+
580+
case Random.rand
581+
when 0.5
582+
changed = true
583+
when 1..20
584+
changed = false
585+
when 21..70
586+
changed = true
587+
end
588+
589+
[].each do
590+
changed = true
591+
end
592+
593+
puts changed
594+
end
595+
RUBY
596+
end
597+
end
598+
554599
context "when a variable is reassigned in loop body but won't " \
555600
'be referenced either next iteration or loop condition' do
556601
it 'registers an offense' do

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