lunarmodules / luacheck

A tool for linting and static analysis of Lua code.

Home Page:https://luacheck.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Length operator in boolean context

Zash opened this issue · comments

Could this be something to warn about?

if #foo then
-- undead code
else
-- dead code
end

Since the length operator should only ever return truthy number values, this is most likely a mistake of some kind, probably meant if #foo ~= 0.

As Hisham noted, the code shown may or may not function as you expect. It is possible in context to have tables behave differently and override the length method to return something falsey if there is not length. As such this is outside the scope of what luacheck is able to check for. You would need to actually execute the project in an interpreter to know for sure if this was an error or not.