summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-10-13 01:38:26 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-10-13 01:38:26 +0800
commitd94e046f62badc58eeb761e39269b0f39e08d8ae (patch)
tree2c5d87377fe3cc6c4d4fedab207e47e23f4e944c
parent7f04a46898cae87e57912a7e6b4739c61d74b18c (diff)
downloadlua-language-server-d94e046f62badc58eeb761e39269b0f39e08d8ae.zip
diagnostics consider `overload`
resolve #1582
-rw-r--r--changelog.md6
-rw-r--r--script/core/diagnostics/missing-return-value.lua23
-rw-r--r--script/core/diagnostics/missing-return.lua23
-rw-r--r--script/core/diagnostics/redundant-return-value.lua28
-rw-r--r--test/diagnostics/type-check.lua33
5 files changed, 69 insertions, 44 deletions
diff --git a/changelog.md b/changelog.md
index 4988b337..6ef6f3c5 100644
--- a/changelog.md
+++ b/changelog.md
@@ -29,6 +29,11 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`.
print(class.a.b.c.e.f.g) --> infered as integer
```
+* `CHG` [#1582] the following diagnostics consider `overload`
+ * `missing-return`
+ * `missing-return-value`
+ * `redundant-return-value`
+ * `return-type-mismatch`
* `FIX` [#1567]
* `FIX` [#1593]
* `FIX` [#1595]
@@ -41,6 +46,7 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`.
[#1558]: https://github.com/sumneko/lua-language-server/issues/1558
[#1561]: https://github.com/sumneko/lua-language-server/issues/1561
[#1567]: https://github.com/sumneko/lua-language-server/issues/1567
+[#1582]: https://github.com/sumneko/lua-language-server/issues/1582
[#1593]: https://github.com/sumneko/lua-language-server/issues/1593
[#1595]: https://github.com/sumneko/lua-language-server/issues/1595
[#1606]: https://github.com/sumneko/lua-language-server/issues/1606
diff --git a/script/core/diagnostics/missing-return-value.lua b/script/core/diagnostics/missing-return-value.lua
index 3aa7bd55..9eab7074 100644
--- a/script/core/diagnostics/missing-return-value.lua
+++ b/script/core/diagnostics/missing-return-value.lua
@@ -5,27 +5,9 @@ local lang = require 'language'
local await = require 'await'
---@param func parser.object
-local function hasDocReturn(func)
- if not func.bindDocs then
- return false
- end
- for _, doc in ipairs(func.bindDocs) do
- if doc.type == 'doc.return' then
- return true
- end
- if doc.type == 'doc.overload' then
- if #doc.overload.returns > 0 then
- return true
- end
- end
- end
- return false
-end
-
----@param func parser.object
---@return integer
local function getReturnsMin(func)
- local min = vm.countReturnsOfFunction(func)
+ local min = vm.countReturnsOfFunction(func, true)
if min == 0 then
return 0
end
@@ -57,9 +39,6 @@ return function (uri, callback)
if not returns then
return
end
- if not hasDocReturn(source) then
- return
- end
local min = getReturnsMin(source)
if min == 0 then
return
diff --git a/script/core/diagnostics/missing-return.lua b/script/core/diagnostics/missing-return.lua
index e3539ac0..d90311d7 100644
--- a/script/core/diagnostics/missing-return.lua
+++ b/script/core/diagnostics/missing-return.lua
@@ -49,6 +49,27 @@ local function isEmptyFunction(func)
return finishRow - startRow <= 1
end
+---@param func parser.object
+---@return integer
+local function getReturnsMin(func)
+ local min = vm.countReturnsOfFunction(func, true)
+ if min == 0 then
+ return 0
+ end
+ for _, doc in ipairs(func.bindDocs) do
+ if doc.type == 'doc.overload' then
+ local n = vm.countReturnsOfFunction(doc.overload)
+ if n == 0 then
+ return 0
+ end
+ if n < min then
+ min = n
+ end
+ end
+ end
+ return min
+end
+
---@async
return function (uri, callback)
local state = files.getState(uri)
@@ -63,7 +84,7 @@ return function (uri, callback)
return
end
await.delay()
- if vm.countReturnsOfFunction(source, true) == 0 then
+ if getReturnsMin(source) == 0 then
return
end
if hasReturn(source) then
diff --git a/script/core/diagnostics/redundant-return-value.lua b/script/core/diagnostics/redundant-return-value.lua
index 36432f98..9b913438 100644
--- a/script/core/diagnostics/redundant-return-value.lua
+++ b/script/core/diagnostics/redundant-return-value.lua
@@ -4,16 +4,25 @@ local vm = require 'vm'
local lang = require 'language'
local await = require 'await'
-local function hasDocReturn(func)
- if not func.bindDocs then
- return false
+---@param func parser.object
+---@return number
+local function getReturnsMax(func)
+ local _, max = vm.countReturnsOfFunction(func, true)
+ if max == math.huge then
+ return max
end
for _, doc in ipairs(func.bindDocs) do
- if doc.type == 'doc.return' then
- return true
+ if doc.type == 'doc.overload' then
+ local _, n = vm.countReturnsOfFunction(doc.overload)
+ if n == math.huge then
+ return n
+ end
+ if n > max then
+ max = n
+ end
end
end
- return false
+ return max
end
---@async
@@ -25,15 +34,12 @@ return function (uri, callback)
---@async
guide.eachSourceType(state.ast, 'function', function (source)
- await.delay()
- if not hasDocReturn(source) then
- return
- end
- local _, max = vm.countReturnsOfFunction(source)
local returns = source.returns
if not returns then
return
end
+ await.delay()
+ local max = getReturnsMax(source)
for _, ret in ipairs(returns) do
local rmin, rmax = vm.countList(ret)
if rmin > max then
diff --git a/test/diagnostics/type-check.lua b/test/diagnostics/type-check.lua
index 5450eda4..a938f14c 100644
--- a/test/diagnostics/type-check.lua
+++ b/test/diagnostics/type-check.lua
@@ -810,16 +810,29 @@ local function f(x)
end
]]
---TEST [[
------@param x boolean
------@return number
------@overload fun()
---local function f(x)
--- if x then
--- return 1
--- end
---end
---]]
+TEST [[
+---@param x boolean
+---@return number
+---@overload fun()
+local function f(x)
+ if x then
+ return 1
+ end
+end
+]]
+
+TEST [[
+---@param x boolean
+---@return number
+---@overload fun(): boolean, boolean
+local function f(x)
+ if x then
+ return 1
+ else
+ return false, false
+ end
+end
+]]
config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
config.remove(nil, 'Lua.diagnostics.disable', 'unused-function')