Lua static analysis: defining an uninitialized table field

I use luacheck (in the Atom editor), but open to other static analysis tools.

Is there a way to verify that I am using an uninitialized table field? I read the docs ( http://luacheck.readthedocs.io/en/stable/index.html ), but maybe I missed how to do this?

In all three cases, in the code below, I try to find that I (erroneously) used the 'y1' field. None of them do. (It was detected at runtime, but I'm trying to catch it before runtime).

local a = {}
a.x = 10
a.y = 20
print(a.x + a.y1)         -- no warning about uninitialized field y1 !?

-- luacheck: globals b
b = {}
b.x = 10
b.y = 20
print(b.x + b.y1)         -- no warning about uninitialized field y1 !?

-- No inline option for luacheck re: 'c', so plenty of complaints
-- about "non-standard global variable 'c'."
c = {}                    --   warning about setting
c.x = 10                  --   warning about mutating
c.y = 20                  --     "       "     "
print(c.x + c.y1)         --   more warnings (but NOT about field y1)

The point is: as projects grow (increase in files and increase in the number and size of modules), it would be nice to prevent skipping such errors.

Thank.

+4
1

lua-inspect . ZeroBrane Studio IDE :

unknown-field.lua:4: first use of unknown field 'y1' in 'a'
unknown-field.lua:7: first assignment to global variable 'b'
unknown-field.lua:10: first use of unknown field 'y1' in 'b'
unknown-field.lua:14: first assignment to global variable 'c'
unknown-field.lua:17: first use of unknown field 'y1' in 'c'

( , , , , .)

+2

Source: https://habr.com/ru/post/1654273/


All Articles