LuaLS / LuaLS/lua-language-server
Initializing a table doesn't satisfied `need-check-nil`
Nobody has claimed this yet.
- Dominant language
- Lua
- Stars
- 4.4k
- Forks
- 442
- PR merge metrics
- No merged PRs in 30d
Description
Issue Description
Hi!
I have a code that looks something like this:
---@param t? table
function api.f(t)
t = type(t) == "table" and t or {}
t.x = t.x or {} --warning is raised but t can never be nil here because it is set to {} if it was but diagnostics fail to see that
--etc.
end
Nil checks are not required here because t should never be nil, the preceding lines redefines it as an empty table if it was nil or invalid so the reference should always exist when trying to check if x exists within the table.
I have seen similar reports but I'm not sure if this exact case has been reported or not. I couldn't find a match or I failed to recognize it.
Additional Notes
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
No source files or tests are named. Start by reproducing the Lua example and tracing the nil-check diagnostic for the need-check-nil warning; done means the warning is not reported for t.x after t is normalized to a table.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100