summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server-beta/src/core/diagnostics/unused-function.lua10
-rw-r--r--server-beta/src/parser/ast.lua28
-rw-r--r--server-beta/test/diagnostics/init.lua23
3 files changed, 49 insertions, 12 deletions
diff --git a/server-beta/src/core/diagnostics/unused-function.lua b/server-beta/src/core/diagnostics/unused-function.lua
index eb6b8f7f..ca3a751d 100644
--- a/server-beta/src/core/diagnostics/unused-function.lua
+++ b/server-beta/src/core/diagnostics/unused-function.lua
@@ -10,7 +10,17 @@ return function (uri, callback)
if not ast then
return
end
+ -- 只检查局部函数与全局函数
guide.eachSourceType(ast.ast, 'function', function (source)
+ local parent = source.parent
+ if not parent then
+ return
+ end
+ if parent.type ~= 'local'
+ and parent.type ~= 'setlocal'
+ and parent.type ~= 'setglobal' then
+ return
+ end
local hasSet
local hasGet = searcher.eachRef(source, function (info)
if info.mode == 'get' then
diff --git a/server-beta/src/parser/ast.lua b/server-beta/src/parser/ast.lua
index f3fec01b..ded42891 100644
--- a/server-beta/src/parser/ast.lua
+++ b/server-beta/src/parser/ast.lua
@@ -697,6 +697,7 @@ local Defs = {
return lv1 < lv2
end
end)
+ local final
for i = #ops, 1, -1 do
local n = ops[i]
local op = list[n]
@@ -710,17 +711,28 @@ local Defs = {
[1] = left,
[2] = right,
}
- list[n-1] = exp
- list[n+1] = exp
- local lastN = ops[i+1]
- if lastN then
- list[lastN-1] = exp
- list[lastN+1] = exp
+ local leftIndex, rightIndex
+ if list[left] then
+ leftIndex = list[left[1]]
+ else
+ leftIndex = n - 1
+ end
+ if list[right] then
+ rightIndex = list[right[2]]
+ else
+ rightIndex = n + 1
end
+
+ list[leftIndex] = exp
+ list[rightIndex] = exp
+ list[left] = leftIndex
+ list[right] = rightIndex
+ list[exp] = n
+ final = exp
+
checkOpVersion(op)
end
- local final = ops[1]
- return list[final-1]
+ return final
end,
Paren = function (start, exp, finish)
if exp and exp.type == 'paren' then
diff --git a/server-beta/test/diagnostics/init.lua b/server-beta/test/diagnostics/init.lua
index aaf51987..40f605e6 100644
--- a/server-beta/test/diagnostics/init.lua
+++ b/server-beta/test/diagnostics/init.lua
@@ -297,13 +297,28 @@ return [[
]]
]=]
+config.config.diagnostics.disable['unused-local'] = true
TEST [[
-local t = {}
-<!function t:x()
-end!>
-return t
+local f = <!function () end!>
+]]
+
+TEST [[
+local f;f = <!function () end!>
+]]
+
+TEST [[
+<!local function f() end!>
+]]
+
+TEST [[
+F = <!function () end!>
+]]
+
+TEST [[
+<!function F() end!>
]]
+config.config.diagnostics.disable['unused-local'] = false
config.config.diagnostics.disable['unused-function'] = true
TEST [[
local mt, x