LuaLS / LuaLS/lua-language-server

Initializing a table doesn't satisfied `need-check-nil`

Open
#3,326 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feat/diagnostics
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.