summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-01-04 20:30:17 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-01-04 20:30:17 +0800
commita927549a7a32d173eb686022f3cf1bfad9815f16 (patch)
tree7f61d2094d777c60eb6f408c01a82a92cb1c72a7
parente006a5a6c0cd566650836ceccbf98d4215449bce (diff)
downloadlua-language-server-a927549a7a32d173eb686022f3cf1bfad9815f16.zip
`unused-function` checks recursive
-rw-r--r--changelog.md3
-rw-r--r--script/core/diagnostics/unused-function.lua30
-rw-r--r--test/diagnostics/init.lua15
3 files changed, 40 insertions, 8 deletions
diff --git a/changelog.md b/changelog.md
index 42435361..cb26ebd6 100644
--- a/changelog.md
+++ b/changelog.md
@@ -3,11 +3,12 @@
## 1.10.0
* `NEW` workspace: supports `.dll`(`.so`) in `require`
* `NEW` folding: `---@class`, `--#region` and docs of function
-* `NEW` dianostic: `count-down-loop`
+* `NEW` diagnostic: `count-down-loop`
* `CHG` supports `~` in command line
* `CHG` completion: improve workspace words
* `CHG` completion: show words in string
* `CHG` completion: split `for .. in` to `for .. ipairs` and `for ..pairs`
+* `CHG` diagnostic: `unused-function` checks recursive
* `FIX` [#339](https://github.com/sumneko/lua-language-server/issues/339)
## 1.9.0
diff --git a/script/core/diagnostics/unused-function.lua b/script/core/diagnostics/unused-function.lua
index e6ae9386..41c239f9 100644
--- a/script/core/diagnostics/unused-function.lua
+++ b/script/core/diagnostics/unused-function.lua
@@ -23,25 +23,33 @@ return function (uri, callback)
if not ast then
return
end
- -- 只检查局部函数
- guide.eachSourceType(ast.ast, 'function', function (source)
+
+ local cache = {}
+ local function checkFunction(source)
+ if cache[source] ~= nil then
+ return cache[source]
+ end
+ cache[source] = false
local parent = source.parent
if not parent then
- return
+ return false
end
if parent.type ~= 'local'
and parent.type ~= 'setlocal' then
- return
+ return false
end
if isToBeClosed(parent) then
- return
+ return false
end
local hasGet
local refs = vm.getRefs(source)
for _, src in ipairs(refs) do
if vm.isGet(src) then
- hasGet = true
- break
+ local func = guide.getParentFunction(src)
+ if not checkFunction(func) then
+ hasGet = true
+ break
+ end
end
end
if not hasGet then
@@ -60,6 +68,14 @@ 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)
+ checkFunction(source)
end)
end
diff --git a/test/diagnostics/init.lua b/test/diagnostics/init.lua
index 4ec93acf..8a89179f 100644
--- a/test/diagnostics/init.lua
+++ b/test/diagnostics/init.lua
@@ -115,6 +115,21 @@ local <!x!>
<!x!> = <!function () end!>
]]
+TEST([[
+<!local function x()
+end!>
+<!local function y()
+ x()
+end!>
+]],
+[[
+local function x()
+end
+local function <!y!>()
+ x()
+end
+]]
+)
TEST [[
print(<!x!>)