IntelliJ-Luanalysis icon indicating copy to clipboard operation
IntelliJ-Luanalysis copied to clipboard

Nil tracking/checking

Open adriweb opened this issue 3 years ago • 2 comments

Environment

name version
IDEA version IDEA 2020.1
Luanalysis version 1.0.1
OS Linux

What are the steps to reproduce this issue?

  1. Make a local with type foo|nil (in my case, that's because the function's return is marked as so)
  2. Check for ~nil in a branch
  3. Use the local knowing it's not nil

image

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

adriweb avatar Jul 14 '20 23:07 adriweb

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

Benjamin-Dobell avatar Jul 15 '20 04:07 Benjamin-Dobell

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

dvilker avatar Aug 17 '22 22:08 dvilker