Skip to content

Commit

Permalink
Fix false positive by Lint/UselessAssignment.
Browse files Browse the repository at this point in the history
This code would wrongly produce a UselessAssignment on "myvar=0"
```
def useless_assignment_bug
  myvar = 0

  if cond
    myvar = nil               # 'myvar = 1' makes the bug go away
    bar if baz                # 'bar' kills the bug
  end

  myvar
end
```

because on_if did not return `node` and therefore this cop managed to
confuse another cop.
  • Loading branch information
mvidner committed Dec 11, 2015
1 parent 06e73e8 commit b2e63cb
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/rubocop/yast/track_variable_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def on_if(node)

# clean slate
scope.clear

node
end

# def on_unless
Expand All @@ -91,39 +93,46 @@ def on_case(node)

# clean slate
scope.clear

node
end

def on_lvasgn(node)
super
name, value = * node
return if value.nil? # and-asgn, or-asgn, resbody do this
scope[name].nice = nice(value)
node
end

def on_and_asgn(node)
super
var, value = *node
bool_op_asgn(var, value, :and)
node
end

def on_or_asgn(node)
super
var, value = *node
bool_op_asgn(var, value, :or)
node
end

def on_block(_node)
def on_block(node)
# ignore body, clean slate
scope.clear
node
end
alias_method :on_for, :on_block

def on_while(_node)
def on_while(node)
# ignore both condition and body,
# with a simplistic scope we cannot handle them

# clean slate
scope.clear
node
end
alias_method :on_until, :on_while

Expand All @@ -143,8 +152,10 @@ def on_rescue(node)
process(r)
end
# end
node
rescue TooComplexToTranslateError
warn "begin-rescue is too complex to translate due to a retry"
node
end

def on_resbody(node)
Expand All @@ -161,11 +172,12 @@ def on_resbody(node)
super
end

def on_ensure(_node)
def on_ensure(node)
# (:ensure, guarded-code, ensuring-code)
# guarded-code may be a :rescue or not

scope.clear
node
end

def on_retry(_node)
Expand Down

0 comments on commit b2e63cb

Please sign in to comment.