summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-06-16 20:57:50 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-06-16 20:57:50 +0800
commit47beee7d7085af35f6869cb349e27dd09b82e7a4 (patch)
tree8b339629804656edc55c375d36c8769a0b5ff986
parent8a9e8d9cfd12d43496c2bf16f70874af550d4e7e (diff)
downloadlua-language-server-47beee7d7085af35f6869cb349e27dd09b82e7a4.zip
infer `nil` as redundant return value
-rw-r--r--changelog.md7
-rw-r--r--script/vm/compiler.lua10
-rw-r--r--script/vm/type.lua3
-rw-r--r--test/diagnostics/common.lua9
-rw-r--r--test/type_inference/init.lua7
5 files changed, 34 insertions, 2 deletions
diff --git a/changelog.md b/changelog.md
index 2048c043..2b9f5b00 100644
--- a/changelog.md
+++ b/changelog.md
@@ -1,5 +1,12 @@
# changelog
+## 3.4.0
+* `CHG` infer `nil` as redundant return value
+ ```lua
+ local function f() end
+ local x = f() -- `x` is `nil` instead of `unknown`
+ ```
+
## 3.3.1
* `FIX` [#1213](https://github.com/sumneko/lua-language-server/issues/1213)
* `FIX` [#1215](https://github.com/sumneko/lua-language-server/issues/1215)
diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua
index 29de39ae..65df3a1d 100644
--- a/script/vm/compiler.lua
+++ b/script/vm/compiler.lua
@@ -667,7 +667,7 @@ local function selectNode(source, list, index)
if list[i] then
local last = list[i]
if last.type == 'call'
- or last.type == '...' then
+ or last.type == 'varargs' then
index = index - i + 1
exp = last
end
@@ -1376,9 +1376,11 @@ local compilerSwitch = util.switch()
local hasMarkDoc
if func.bindDocs then
local sign = getObjectSign(func)
+ local lastReturn
for _, doc in ipairs(func.bindDocs) do
if doc.type == 'doc.return' then
for _, rtn in ipairs(doc.returns) do
+ lastReturn = rtn
if rtn.returnIndex == index then
hasMarkDoc = true
local hasGeneric
@@ -1396,12 +1398,18 @@ local compilerSwitch = util.switch()
end
end
end
+ if lastReturn and not hasMarkDoc and lastReturn.types[1][1] == '...' then
+ vm.setNode(source, vm.getGlobal('type', 'unknown'))
+ end
end
if func.returns and not hasMarkDoc then
for _, rtn in ipairs(func.returns) do
selectNode(source, rtn, index)
end
end
+ if vm.getNode(source):isEmpty() then
+ vm.setNode(source, vm.getGlobal('type', 'nil'))
+ end
end)
: case 'main'
: call(function (source)
diff --git a/script/vm/type.lua b/script/vm/type.lua
index 8027e933..3eb42e51 100644
--- a/script/vm/type.lua
+++ b/script/vm/type.lua
@@ -39,7 +39,8 @@ function vm.isSubNode(child, parent, mark)
mark = mark or {}
local childName = getNodeName(child)
local parentName = getNodeName(parent)
- if childName == 'any' or parentName == 'any' then
+ if childName == 'any'
+ or parentName == 'any' then
return true
end
diff --git a/test/diagnostics/common.lua b/test/diagnostics/common.lua
index d9dfb0b9..d7ec4b63 100644
--- a/test/diagnostics/common.lua
+++ b/test/diagnostics/common.lua
@@ -1608,3 +1608,12 @@ local x = 0
<!x!> = true
]]
+
+TEST [[
+---@diagnostic disable: unused-local
+
+---@type integer
+local x
+
+<!x!> = true
+]]
diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua
index 32a35fbd..c402cb8d 100644
--- a/test/type_inference/init.lua
+++ b/test/type_inference/init.lua
@@ -2650,3 +2650,10 @@ x = call(x)
print(<?x?>)
]]
+
+TEST 'nil' [[
+local function f()
+end
+
+local <?x?> = f()
+]]