Skip to content

Commit

Permalink
Recognize mutation with a long key chain
Browse files Browse the repository at this point in the history
Previously `x.y = 1` was considered a mutation but `x.y.z = 2` was
a simple access. Recursively handle indexes in lhs of assignments
so that such long key chains are recognized as mutations, too.
  • Loading branch information
mpeterv committed Jul 2, 2016
1 parent be2d2f9 commit 002728c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions spec/cli_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ Total: 4 warnings / 0 errors in 1 file
assert.equal([[
Checking spec/samples/bad_code.lua 6 warnings
spec/samples/bad_code.lua:1:1: accessing undefined variable 'package'
spec/samples/bad_code.lua:1:1: mutating non-standard global variable 'package'
spec/samples/bad_code.lua:3:16: unused function 'helper'
spec/samples/bad_code.lua:3:23: unused variable length argument
spec/samples/bad_code.lua:7:10: setting non-standard global variable 'embrace'
Expand Down Expand Up @@ -750,7 +750,7 @@ spec/samples/good_code.lua
return {}
spec/samples/bad_code.lua
(%d+)
local A="113";return {{A,"package",1,1,7},{"211","helper",3,16,21,%[10%]=true},{"212","...",3,23,25},{"111","embrace",7,10,16,%[12%]=true},{"412","opt",8,10,12,7,18},{A,"hepler",9,11,16}}
return {{"112","package",1,1,7},{"211","helper",3,16,21,%[10%]=true},{"212","...",3,23,25},{"111","embrace",7,10,16,%[12%]=true},{"412","opt",8,10,12,7,18},{"113","hepler",9,11,16}}
spec/samples/python_code.lua
(%d+)
return {{"011",%[3%]=1,%[4%]=6,%[5%]=15,%[23%]="expected '=' near '__future__'"}}
Expand Down
2 changes: 1 addition & 1 deletion src/luacheck/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local cache = {}
-- third is check result in lua table format.
-- String fields are compressed into array indexes.

cache.format_version = 5
cache.format_version = 6

local fields = {
"code", "name", "line", "column", "end_column", "prev_line", "prev_column", "secondary",
Expand Down
23 changes: 14 additions & 9 deletions src/luacheck/linearize.lua
Original file line number Diff line number Diff line change
Expand Up @@ -456,15 +456,7 @@ function LinState:emit_stmt_Set(node)
end
else
assert(expr.tag == "Index")

if expr[1].tag == "Id" and not self:resolve_var(expr[1][1]) then
-- Warn about mutated global.
self:check_var(expr[1], "mutate")
else
self:scan_expr(item, expr[1])
end

self:scan_expr(item, expr[2])
self:scan_lhs_index(item, expr)
end
end

Expand Down Expand Up @@ -529,6 +521,19 @@ function LinState:scan_expr_Dots(item, node)
self:mark_access(item, node)
end

function LinState:scan_lhs_index(item, node)
if node[1].tag == "Id" and not self:resolve_var(node[1][1]) then
-- Warn about mutated global.
self:check_var(node[1], "mutate")
elseif node[1].tag == "Index" then
self:scan_lhs_index(item, node[1])
else
self:scan_expr(item, node[1])
end

self:scan_expr(item, node[2])
end

LinState.scan_expr_Index = LinState.scan_exprs
LinState.scan_expr_Call = LinState.scan_exprs
LinState.scan_expr_Invoke = LinState.scan_exprs
Expand Down

0 comments on commit 002728c

Please sign in to comment.