Skip to content

False-Positive Inject-Field diagnostic suggests duplicate ---@class statement #2528

Description

@Eikonium

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?

Diagnostics/Syntax Checking

Expected Behaviour

---@class Animal
Animal = {
    name = 'default'
}

---@param name string
---@return Animal
function Animal.create(name)
    local new = {} ---@type Animal
    new.name = name
    return new
end

should not produce any warning.

Actual Behaviour

The code as presented above triggers the inject-field diagnostic.
grafik

The warning message suggests resolving it by replacing ---@type by ---@class. This does in fact clear the warning, but leads to a duplicate Animal-class annotation as below.

---@class Animal
Animal = {
    name = 'default'
}

---@param name string
---@return Animal
function Animal.create(name)
    local new = {} ---@class Animal
    new.name = name --this doesn't trigger a warning anymore
    return new
end
  1. The ---@type annotation should not produce a warning, as new.name = name is not injecting a new field, but rather relying on a previously defined one.
  2. The warning message should not suggest to duplicate the class-statement.
  3. The suggested use of ---@class (using it on an object of the class instead of the class itself) is also not documented on https://luals.github.io/wiki/annotations/#class

Reproduction steps

Paste the code from the Expected Behaviour box into an empty Lua-document in Visual Studio Code.

Additional Notes

Similar issues have been brought up in #2303 and #2341.
I don't consider these to be duplicates of my issue, because they are about injected-field misbehaviour in nested tables.

Log File

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions