Skip to content

Commit

Permalink
Avoid annotating var objects in unused local detection
Browse files Browse the repository at this point in the history
Ideally various detection modules are independent and don't mutate arguments,
just report warnings.
  • Loading branch information
mpeterv committed Feb 2, 2018
1 parent 27920e9 commit 43cebe7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
26 changes: 14 additions & 12 deletions src/luacheck/detect_uninit_access.lua
Expand Up @@ -5,24 +5,26 @@ local function detect_uninit_access_in_line(chstate, line)

if item_var_map then
for var, accessing_nodes in pairs(item_var_map) do
-- `var.empty` is set during general local variable reporting if the variable is never set.
-- In this case, reporting all its accesses as uninitialized is redundant.
-- If there are no values at all reaching this access, not even the empty one,
-- this item (or a closure containing it) is not reachable from variable definition.
-- It will be reported as unreachable code, no need to report uninitalized accesses in it.
if not var.empty and item.used_values[var] then
local all_possible_values_empty = true
if item.used_values[var] then
-- If this variable is has only one, empty value then it's already reported as never set,
-- no need to report each access.
if not (#var.values == 1 and var.values[1].empty) then
local all_possible_values_empty = true

for _, possible_value in ipairs(item.used_values[var]) do
if not possible_value.empty then
all_possible_values_empty = false
break
for _, possible_value in ipairs(item.used_values[var]) do
if not possible_value.empty then
all_possible_values_empty = false
break
end
end
end

if all_possible_values_empty then
for _, accessing_node in ipairs(accessing_nodes) do
chstate:warn_uninit(accessing_node, action_key == "mutations")
if all_possible_values_empty then
for _, accessing_node in ipairs(accessing_nodes) do
chstate:warn_uninit(accessing_node, action_key == "mutations")
end
end
end
end
Expand Down
1 change: 0 additions & 1 deletion src/luacheck/detect_unused_locals.lua
Expand Up @@ -46,7 +46,6 @@ local function check_var(chstate, var)
chstate:warn_unused_variable(var.values[1], nil, nil, var.values[1].empty)
end
elseif var.values[1].empty then
var.empty = true
chstate:warn_unset(var)
end
elseif not var.accessed and not var.mutated then
Expand Down

0 comments on commit 43cebe7

Please sign in to comment.