Benjamin-Dobell / IntelliJ-Luanalysis

Type-safe Lua IDE — IntelliJ IDEA plugin
Apache License 2.0
155 stars 22 forks source link

Returned anonymous function is not type-checked #158

Closed yhslai closed 1 year ago

yhslai commented 1 year ago

Environment

JetBrains Rider 2023.1.3 on Windows 10

Preferences

image

Lua

Name | Setting
Lua 5.4

Type Safety

Strict nil checks | ☑️ Unknown type (any) is indexable | ☑️ Unknown type (any) is callabale | ☑️

What are the steps to reproduce this issue?

---@type fun(): number
return function()
    return "1"
end

What happens?

No warning or error.

What were you expecting to happen?

Warning or error telling me I'm returning a fun(): string instead of fun(): number

Any other comments?

---@return fun(): number
return function()
    return "1"
end

Doesn't work either.

Benjamin-Dobell commented 1 year ago

The ---@return tag ought to be applied to local or table method function declarations/assignments and pertains to the return type only e.g.

---@return number
local function test1()
    return "1"
end
image

If you're trying to specify a return type, where the return type is a function you'd do:

---@return fun(): number
local function test2()
    return function()
        return "1"
    end
end
image

Alternatively, if you do want to specify the intended return type inline, you can do so with a ---@type for the return statement e.g.

local test3 = function()
    ---@type fun(): number
    return function()
        return "1"
    end
end
image

Basically, the @return tag shouldn't appear on return statements, rather it's used to specify the return type of the associated function definition.