mpeterv / luacheck

A tool for linting and static analysis of Lua code.
MIT License
1.92k stars 322 forks source link

Unused value of a local is not reported if a closure referring to it is defined in alternative branch #126

Closed mpeterv closed 7 years ago

mpeterv commented 7 years ago

E.g. when checking

local var

if cond then
   var = value
else
   func = function() return var end
end

value assigned to var should be reported as unused (and access of var in func function should be reported as accessing an uninitialized variable), but it's not.

Currently in analyze.lua it's assumed that an access in a closure can resolve to an assignment if the closure and the value can be live at the same CFG node. Instead, it should check that either the value reaches the closure or the closure reaches the value. Can be solved by tweaking the way values and closures are propagated in analyze.lua, no reason to switch to proper dataflow analysis.