Skip to content

Initializing a table doesn't satisfied need-check-nil #3326

@Arxareon

Description

@Arxareon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat/diagnosticsRelated to the diagnostics feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions