lua-language-server icon indicating copy to clipboard operation
lua-language-server copied to clipboard

`need-check-nil` doesn't recognize nil check propagation

Open paigoddess opened this issue 2 years ago • 3 comments

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

paigoddess avatar Mar 10 '23 22:03 paigoddess

May be related to #1429

paigoddess avatar Mar 10 '23 22:03 paigoddess

I don't know how to trace this usgage, please use ---@cast item -nil for now.

sumneko avatar Mar 11 '23 03:03 sumneko

Similar observation.

image

Anyway @sumneko thank you for the great plugin 👍

decadence avatar Nov 08 '23 10:11 decadence