summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-04-22 02:18:20 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-04-22 02:18:20 +0800
commitb85ff57a116fb252a0da35e525264e0940af88c7 (patch)
treef347bf477a6e8294e6f8601e8a5bbacb8e77c9d7
parent264a3dbd41121ea975f934a6281a2b0781507fe0 (diff)
downloadlua-language-server-b85ff57a116fb252a0da35e525264e0940af88c7.zip
fix `unused-function` does not recognize recursion
-rw-r--r--changelog.md1
-rw-r--r--script/core/diagnostics/unused-function.lua7
-rw-r--r--test/diagnostics/common.lua12
3 files changed, 12 insertions, 8 deletions
diff --git a/changelog.md b/changelog.md
index e8fa5afb..667276e2 100644
--- a/changelog.md
+++ b/changelog.md
@@ -24,6 +24,7 @@
local y = x--[[@as integer]] -- y is `integer` here
```
* `CHG` diagnostic: no longer mark `redundant-parameter` as `Unnecessary`
+* `FIX` diagnostic: `unused-function` does not recognize recursion
* `FIX` [#1051](https://github.com/sumneko/lua-language-server/issues/1051)
* `FIX` [#1072](https://github.com/sumneko/lua-language-server/issues/1072)
* `FIX` [#1077](https://github.com/sumneko/lua-language-server/issues/1077)
diff --git a/script/core/diagnostics/unused-function.lua b/script/core/diagnostics/unused-function.lua
index 79cb16e2..d3aca1d9 100644
--- a/script/core/diagnostics/unused-function.lua
+++ b/script/core/diagnostics/unused-function.lua
@@ -53,6 +53,7 @@ return function (uri, callback)
local refs = parent.ref
local hasGet
if refs then
+ cache[source] = true
for _, src in ipairs(refs) do
if guide.isGet(src) then
local func = guide.getParentFunction(src)
@@ -62,6 +63,7 @@ return function (uri, callback)
end
end
end
+ cache[source] = not hasGet
end
if not hasGet then
if client.isVSCode() then
@@ -79,14 +81,11 @@ return function (uri, callback)
message = lang.script.DIAG_UNUSED_FUNCTION,
}
end
- cache[source] = true
return true
end
return false
end
-- 只检查局部函数
- guide.eachSourceType(ast.ast, 'function', function (source) ---@async
- checkFunction(source)
- end)
+ guide.eachSourceType(ast.ast, 'function', checkFunction)
end
diff --git a/test/diagnostics/common.lua b/test/diagnostics/common.lua
index b8d3d18a..e6808784 100644
--- a/test/diagnostics/common.lua
+++ b/test/diagnostics/common.lua
@@ -286,6 +286,12 @@ TEST [[
local <!function f() end!>
]]
+TEST [[
+local <!function f()
+ f()
+end!>
+]]
+
config.get(nil, 'Lua.diagnostics.disable')['unused-local'] = nil
TEST [[
local mt, x
@@ -494,11 +500,10 @@ _ = 1, <!2!>
]]
TEST [[
-local function x()
+function X()
do
local k
print(k)
- x()
end
local k = 1
print(k)
@@ -506,9 +511,8 @@ end
]]
TEST [[
-local function x()
+function X()
local loc
- x()
print(loc)
end
]]