Length operator in boolean context
Zash opened this issue · comments
Kim Alvefur commented
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
.
Hisham Muhammad commented
That is not necessarily true in the presence of metamethods, and luacheck
does not have sufficient type information to verify that.
…Caleb Maclennan commented
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.