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.

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
- 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.
- The warning message should not suggest to duplicate the class-statement.
- 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
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
should not produce any warning.
Actual Behaviour
The code as presented above triggers the inject-field diagnostic.

The warning message suggests resolving it by replacing
---@typeby---@class. This does in fact clear the warning, but leads to a duplicate Animal-class annotation as below.---@typeannotation should not produce a warning, asnew.name = nameis not injecting a new field, but rather relying on a previously defined one.---@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/#classReproduction 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