Skip to content

Commit abdeade

Browse files
committed
new diag redundant-return-value
1 parent ae02495 commit abdeade

File tree

8 files changed

+97
-3
lines changed

8 files changed

+97
-3
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* `unknown-cast-variable`
99
* `cast-type-mismatch`
1010
* `missing-return-value`
11+
* `redundant-return-value`
1112
* `NEW` settings:
1213
* `diagnostics.groupSeverity`
1314
* `diagnostics.groupFileStatus`

locale/zh-cn/script.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ DIAG_MISSING_RETURN_VALUE =
132132
'至少需要 {min} 个返回值,但此处只返回 {rmax} 个值。'
133133
DIAG_MISSING_RETURN_VALUE_RANGE =
134134
'至少需要 {min} 个返回值,但此处只返回 {rmin} 到 {rmax} 个值。'
135+
DIAG_REDUNDANT_RETURN_VALUE =
136+
'最多只有 {max} 个返回值,但此处返回了第 {rmax} 个值。'
137+
DIAG_REDUNDANT_RETURN_VALUE_RANGE =
138+
'最多只有 {max} 个返回值,但此处返回了第 {rmin} 到第 {rmax} 个值。'
135139

136140
MWS_NOT_SUPPORT =
137141
'{} 目前还不支持多工作目录,我可能需要重启才能支持新的工作目录...'
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
local files = require 'files'
2+
local guide = require 'parser.guide'
3+
local vm = require 'vm'
4+
local lang = require 'language'
5+
6+
local function hasDocReturn(func)
7+
if not func.bindDocs then
8+
return false
9+
end
10+
for _, doc in ipairs(func.bindDocs) do
11+
if doc.type == 'doc.return' then
12+
return true
13+
end
14+
end
15+
return false
16+
end
17+
18+
return function (uri, callback)
19+
local state = files.getState(uri)
20+
if not state then
21+
return
22+
end
23+
24+
guide.eachSourceType(state.ast, 'function', function (source)
25+
if not hasDocReturn(source) then
26+
return
27+
end
28+
local _, max = vm.countReturnsOfFunction(source)
29+
local returns = source.returns
30+
if not returns then
31+
return
32+
end
33+
for _, ret in ipairs(returns) do
34+
local rmin, rmax = vm.countList(ret)
35+
if rmin > max then
36+
for i = max + 1, #ret - 1 do
37+
callback {
38+
start = ret[i].start,
39+
finish = ret[i].finish,
40+
message = lang.script('DIAG_REDUNDANT_RETURN_VALUE', {
41+
max = max,
42+
rmax = i,
43+
}),
44+
}
45+
end
46+
if #ret == rmax then
47+
callback {
48+
start = ret[#ret].start,
49+
finish = ret[#ret].finish,
50+
message = lang.script('DIAG_REDUNDANT_RETURN_VALUE', {
51+
max = max,
52+
rmax = rmax,
53+
}),
54+
}
55+
else
56+
callback {
57+
start = ret[#ret].start,
58+
finish = ret[#ret].finish,
59+
message = lang.script('DIAG_REDUNDANT_RETURN_VALUE_RANGE', {
60+
max = max,
61+
rmin = #ret,
62+
rmax = rmax,
63+
}),
64+
}
65+
end
66+
end
67+
end
68+
end)
69+
end

script/files.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,6 @@ function m.eachFile(suri)
457457
end
458458

459459
--- Pairs dll files
460-
---@return function
461460
function m.eachDll()
462461
local map = {}
463462
for uri, file in pairs(m.dllMap) do

script/proto/diagnostic.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ m.register {
5959
'redundant-parameter',
6060
'missing-parameter',
6161
'missing-return-value',
62+
'redundant-return-value',
6263
} {
6364
group = 'unbalanced',
6465
severity = 'Warning',

script/vm/runner.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ end
113113
---@param action parser.object
114114
---@param topNode vm.node
115115
---@param outNode? vm.node
116-
---@return vm.node
116+
---@return vm.node topNode
117+
---@return vm.node outNode
117118
function mt:_lookInto(action, topNode, outNode)
118119
if not action then
119120
return topNode, outNode

script/workspace/workspace.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ end
397397

398398
---@param uriOrPath uri|string
399399
---@return string
400+
---@return boolean suc
400401
function m.getRelativePath(uriOrPath)
401402
local path, uri
402403
if uriOrPath:sub(1, 5) == 'file:' then

test/diagnostics/common.lua

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ TEST [[
885885
---@param v T
886886
---@param message any
887887
---@return T
888+
---@return any message
888889
function assert(v, message)
889890
return v, message
890891
end
@@ -1734,14 +1735,31 @@ function F()
17341735
end
17351736
]]
17361737

1737-
do return end
17381738
TEST [[
17391739
---@return number, number?
17401740
function F()
17411741
return 1, 1, <!1!>
17421742
end
17431743
]]
17441744

1745+
TEST [[
1746+
---@return number, number?
1747+
function F()
1748+
return 1, 1, <!1!>, <!2!>, <!3!>
1749+
end
1750+
]]
1751+
1752+
TEST [[
1753+
---@return number, number
1754+
local function r2() end
1755+
1756+
---@return number, number?
1757+
function F()
1758+
return 1, <!r2()!>
1759+
end
1760+
]]
1761+
1762+
do return end
17451763
TEST [[
17461764
---@return number
17471765
function F()

0 commit comments

Comments
 (0)