mpeterv / luacheck

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

Bad error with global variable access : accessing undefined variable #93

Closed bew closed 7 years ago

bew commented 7 years ago

With version : Luacheck: 0.17.1

With the following snippet:

function do_something() -- luacheck: ignore do_something
    print('hello')
end

local function do_it()
    do_something()
    -- => bad luacheck error: accessing undefined variable 'do_something'
end

do_it()

I get an incorrect error:

Checking luacheck_fail.lua                        1 warning

    luacheck_fail.lua:6:2: accessing undefined variable do_something

Total: 1 warning / 0 errors in 1 file

It does the same error if do_something is a variable set without local


If I change do_something to:

local function do_something() -- notice the `local`
    print('hello')
end

There is no error

mpeterv commented 7 years ago

This is correct behaviour, inline options on the same line as some code only affect warnings for that line. If you move it one line above it will work as you want, ignoring all warnings related to do_something in the rest of the code:

-- luacheck: ignore do_something
function do_something()
    print('hello')
end

local function do_it()
    do_something()
    -- => bad luacheck error: accessing undefined variable 'do_something'
end

do_it()

See the part of https://luacheck.readthedocs.io/en/stable/inline.html after the table.

bew commented 7 years ago

Not exactly what I want, I clarify:

The first ignore was intended to be for that line only. I don't want to ignore the last warning, because I think there shouldn't be a a warning on that line at all.

The last warning is accessing undefined variable but the variable is defined, yes it's defined as a global, but it is defined!

And I don't think that just because the do_something has a warning, the definition should be canceled..

Is it fully intentional?

mpeterv commented 7 years ago

Yes, a global must be explicitly defined using --globals option or something equivalent. If you want to allow defining globals simply by assigning try --allow-defined or --allow-defined-top options, see https://luacheck.readthedocs.io/en/stable/warnings.html#implicitly-defined-globals.

bew commented 7 years ago

I understand, thanks for your reply