lua-language-server
lua-language-server copied to clipboard
`need-check-nil` doesn't recognize nil check propagation
How are you using the lua-language-server?
Visual Studio Code Extension (sumneko.lua)
Which OS are you using?
MacOS
What is the issue affecting?
Diagnostics/Syntax Checking
Expected Behaviour
Ideally, need-check-nil would infer a nil check has been completed if I've already done that nil check earlier as part of a larger check. Specifically, the statement if not (a and a.b) then will properly register as a nil check on a, but assigning the inner expression to a local var prevents LLS from detecting it as a nil check.
Actual Behaviour
LLS warns need-check-nil when a variable cannot possibly be nil, if the check was masked via another variable.
Reproduction steps
---@return table?
local function stuff()
return nil
end
local item = stuff()
local x = item and item.x
if not x then -- Works: `if not (item and item.x) then`
-- This includes case where `item` is `nil`
else
local subitem = item.key -- Error on `item` (need-check-nil)
end
Additional Notes
No response
Log File
No response
May be related to #1429
I don't know how to trace this usgage, please use ---@cast item -nil for now.
Similar observation.
Anyway @sumneko thank you for the great plugin 👍