You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
table newindex shows invalid need check for nil warning if key is false #3359
t[key] = {} should throw no warning if key is not nil.
Actual Behaviour
t[key] = {} throws an invalid warning if key is false but not nil.
Reproduction steps
localCACHE= {}
CACHE[false] = {} -- oklocalkey=falseCACHE[key] = {} -- should be ok as well, but throws invalid warning
Additional Notes
This happened to me in a more complex method where I ensured a key is not nil, by redefining the parameter:
functionapicall(input)
localkey=inputorfalse-- more stuff...cache[key] = {}
returncache[key]
end
I'd like to note an annoyance when it comes to casting, the following casts all throw warnings, even if explicitly casting the variable, requiring me to invent a new variable (or forcing me disable redefining local):
---@paraminputstring?functionapicall(input)
-- this cast does nothing and also throws no warning---@castinput+false-- this cast warns that it can't cast string? to string|false---@castinputstring|false-- this is what I do, it just throws an info warning, which I've disabled:---@typestring | falselocalinput=inputorfalseend
The logic of need-check-nil diagnostics currently checks for node:hasFalsy(), which includes optional / nil / false and this seems to be the root problem 🤔 It should only check for optional and nil I believe?
Activity
tomlau10 commentedon Feb 24, 2026
analysis
The logic of
need-check-nildiagnostics currently checks fornode:hasFalsy(), which includesoptional/nil/falseand this seems to be the root problem 🤔 It should only check foroptionalandnilI believe?lua-language-server/script/core/diagnostics/need-check-nil.lua
Line 40 in 0187ddf
lua-language-server/script/vm/node.lua
Lines 106 to 116 in 0187ddf
possible fix
To address this issue, I tested that we can add a
node:hasNil()method:diagnostics/need-check-nil.luachange that line toThis can fix the case in Reproduction steps