LuaLS / LuaLS/lua-language-server
Missing diagnostic when passing nil-able fields to pairs/ipairs
Nobody has claimed this yet.
- Dominant language
- Lua
- Stars
- 4.4k
- Forks
- 442
- PR merge metrics
- No merged PRs in 30d
Description
### How are you using the lua-language-server?
Visual Studio Code Extension (sumneko.lua)
### Which OS are you using?
Windows
### What is the issue affecting?
Type Checking, Diagnostics/Syntax Checking
### Expected Behaviour
In the following code, I expect to get param-type mismatch for each of the `for` loops (with `pairs` and `ipairs`) because the variable being iterated can be `nil`:
```lua
---@type string[]?
local x1 = {}
for _ in ipairs(x1) do -- param-type-mismatch: Cannot assign `string[]?` to parameter `
break
end
---@type table?
local y1 = {}
for _ in ipairs(y1) do -- param-type-mismatch: Cannot assign `table?` to parameter `
break
end
---@class Class
---@field x string[]?
---@field y table?
---@type Class
local x2 = {}
for _ in ipairs(x2.x) do -- Should warn, but it doesn't
break
end
for _ in pairs(x2.y) do -- Should warn, but it doesn't
break
end
```
### Actual Behaviour
The param-type-mismatch diagnostic is only shown for the first two loops (direct variables without nesting/fields), but not when using fields (the third and fourth for loops).
### Reproduction steps
Copy the code into a Lua file and observe the diagnostics
### Additional Notes
_No response_
### Log File
_No response_
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue by copying the provided Lua example into a Lua file and comparing diagnostics for direct variables versus nested fields. Trace the type-checking path that handles ipairs and pairs arguments, then verify that nullable fields receive the same param-type-mismatch diagnostic as direct variables.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100