Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Luanalysis doesn't seem to understand manual nil check #159

Open
yhslai opened this issue Jul 12, 2023 · 0 comments
Open

Luanalysis doesn't seem to understand manual nil check #159

yhslai opened this issue Jul 12, 2023 · 0 comments

Comments

@yhslai
Copy link

yhslai commented Jul 12, 2023

Environment

Name Version
IDEA version 2023.1.3
Luanalysis version 1.4.0
OS Windows 10

Preferences

image

Lua

Name Setting
Language level Lua 5.4

Type Safety

Name Setting
Strict nil checks ☑️
Unknown type (any) is indexable ☑️
Unknown type (any) is callabale ☑️

What are the steps to reproduce this issue?

---@type string[] | nil
local strs = {"abc"}

if strs ~= nil then
    print(strs[1])
end

What happens?

Error: No such indexer '[1] found on type 'nil'

What were you expecting to happen?

No error.

Any other comments?

I'm not sure if it's intentional or not, but I think it should report error in this case. For example, in C#, if we do manual null check first:

private string? _str = null;
...
if(_str != null)
{
   _str.ToUpper();
}

It'll compile without problem.

Furthermore, what's the alternative? Put --[[---@not nil]] everywhere?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant