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

Invalid type diagnostic doesn't show when passing value from array directly to a function

Open Jummit opened this issue 3 years ago • 2 comments

How are you using the lua-language-server?

Other

Which OS are you using?

Linux

What is the issue affecting?

Type Checking, Diagnostics/Syntax Checking

Expected Behaviour

When using a value from an array as a parameter I expect type checking to work.

Actual Behaviour

Type checking only works when I assign the value from the array to a temporary variable, not when passing it to a function directly.

Reproduction steps

This doesn't show a warning:

---@param num integer
function receive(num)
end

---@type (integer?)[]
local values = {}

receive(values[1])

This works:

local value = values[1]
receive(value) -- Cannot assign `integer?` to parameter `integer`.

Additional Notes

No response

Log File

No response

Jummit avatar Oct 19 '22 13:10 Jummit

if values[1] then
    receive(values[1]) --> dose not support narrow `integer?` to `integer` in this case
end

Since the expression cannot be narrowed at present, I can only ignore nil type to prevent false warning.

sumneko avatar Oct 19 '22 16:10 sumneko

Is the following an instance of the same issue or a different one? It would be really useful to get warnings about nil values when assigning an array (e.g. when using variables for array elements and mistyped a variable name: local x = { a.first, a.seeecond, a.third })

---@type string[]
local x = { 'a', nil, 'b' } -- no assign-type-mismatch

local y = x[2] -- y is string, but can actually be nil

firas-assaad avatar Jan 25 '24 08:01 firas-assaad