IntelliJ-Luanalysis
IntelliJ-Luanalysis copied to clipboard
Nil tracking/checking
Environment
name | version |
---|---|
IDEA version | IDEA 2020.1 |
Luanalysis version | 1.0.1 |
OS | Linux |
What are the steps to reproduce this issue?
- Make a local with type
foo|nil
(in my case, that's because the function's return is marked as so) - Check for
~nil
in a branch - Use the local knowing it's not nil
What happens?
The local is still assumed to be potentially nil
What were you expecting to happen?
The local should be un-flagged as "potentially nil" interally
I would definitely like to add this. Although of course can be complicated in certain circumstances.
It is going to be a bit difficult though as Lua allows overloading of operators, and whilst Lua itself isn't multi-threaded, it is frequently used in multi-threaded environments.
In the example above it's doing the right thing though i.e. using a local, rather than a table property. May need to inspect the type as well to be sure the comparison isn't overloaded.
For now, if you haven't already, take a look at @not
casts. They're good for casting away nil
without throwing away further type safety:
https://github.com/Benjamin-Dobell/IntelliJ-Luanalysis#not-type-casts
Can I use @not
for exists variable in if-scope?
Something like this:
---@param s string
function print(s) end
---@type string|nil
local a = "Hi"
if a ~= nil then
---@not nil: a
print(a)
end