Skip to content

Commit df4fb9b

Browse files
committed
diag missing-return
1 parent 7de3d68 commit df4fb9b

File tree

14 files changed

+235
-15
lines changed

14 files changed

+235
-15
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* `cast-type-mismatch`
1010
* `missing-return-value`
1111
* `redundant-return-value`
12+
* `missing-return`
1213
* `NEW` settings:
1314
* `diagnostics.groupSeverity`
1415
* `diagnostics.groupFileStatus`

locale/zh-cn/script.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ DIAG_REDUNDANT_RETURN_VALUE =
136136
'最多只有 {max} 个返回值,但此处返回了第 {rmax} 个值。'
137137
DIAG_REDUNDANT_RETURN_VALUE_RANGE =
138138
'最多只有 {max} 个返回值,但此处返回了第 {rmin} 到第 {rmax} 个值。'
139+
DIAG_MISSING_RETURN =
140+
'此处需要返回值。'
139141

140142
MWS_NOT_SUPPORT =
141143
'{} 目前还不支持多工作目录,我可能需要重启才能支持新的工作目录...'

script/config/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ local function getScope(uri, key)
3838
end
3939
end
4040
if uri then
41-
---@type scope
41+
---@type scope?
4242
local scp = scope.getFolder(uri) or scope.getLinkedScope(uri)
4343
if scp then
4444
if not key

script/core/completion/completion.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,6 @@ local function cleanEnums(enums, source)
11331133
return enums
11341134
end
11351135

1136-
---@return boolean
11371136
local function insertEnum(state, src, enums, isInArray)
11381137
if src.type == 'doc.type.string'
11391138
or src.type == 'doc.type.integer'
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
local files = require 'files'
2+
local guide = require 'parser.guide'
3+
local vm = require 'vm'
4+
local lang = require 'language'
5+
6+
---@param uri uri
7+
---@param func parser.object
8+
local function hasDocReturn(uri, func)
9+
if not func.bindDocs then
10+
return false
11+
end
12+
for _, doc in ipairs(func.bindDocs) do
13+
if doc.type == 'doc.return' then
14+
-- don't need return with only one `any`
15+
local lastReturn = doc.returns[#doc.returns]
16+
if lastReturn.returnIndex ~= 1
17+
or vm.getInfer(lastReturn):view(uri) ~= 'any' then
18+
return true
19+
end
20+
end
21+
end
22+
return false
23+
end
24+
25+
---@param block parser.object
26+
---@return boolean
27+
local function hasReturn(block)
28+
if block.hasReturn or block.hasError then
29+
return true
30+
end
31+
if block.type == 'if' then
32+
local hasElse
33+
for _, subBlock in ipairs(block) do
34+
if not hasReturn(subBlock) then
35+
return false
36+
end
37+
if subBlock.type == 'elseblock' then
38+
hasElse = true
39+
end
40+
end
41+
return hasElse == true
42+
else
43+
if block.type == 'while' then
44+
if vm.testCondition(block.filter) then
45+
return true
46+
end
47+
end
48+
for _, action in ipairs(block) do
49+
if guide.isBlockType(action) then
50+
if hasReturn(action) then
51+
return true
52+
end
53+
end
54+
end
55+
end
56+
return false
57+
end
58+
59+
return function (uri, callback)
60+
local state = files.getState(uri)
61+
if not state then
62+
return
63+
end
64+
65+
guide.eachSourceType(state.ast, 'function', function (source)
66+
-- check declare only
67+
if #source == 0 then
68+
return
69+
end
70+
if not hasDocReturn(uri, source) then
71+
return
72+
end
73+
if hasReturn(source) then
74+
return
75+
end
76+
local lastAction = source[#source]
77+
local finish = lastAction.range or lastAction.finish
78+
callback {
79+
start = finish,
80+
finish = finish,
81+
message = lang.script('DIAG_MISSING_RETURN'),
82+
}
83+
end)
84+
end

script/lclient.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ function mt:reportHangs()
8080
end
8181

8282
---@param callback async fun(client: languageClient)
83-
---@return languageClient
8483
function mt:start(callback)
8584
CLI = true
8685

script/parser/compile.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2833,7 +2833,8 @@ local function compileExpAsAction(exp)
28332833
local block = Chunk[i]
28342834
if block.type == 'ifblock'
28352835
or block.type == 'elseifblock'
2836-
or block.type == 'else' then
2836+
or block.type == 'elseblock'
2837+
or block.type == 'function' then
28372838
block.hasError = true
28382839
break
28392840
end
@@ -2986,7 +2987,8 @@ local function parseReturn()
29862987
local block = Chunk[i]
29872988
if block.type == 'ifblock'
29882989
or block.type == 'elseifblock'
2989-
or block.type == 'else' then
2990+
or block.type == 'elseblock'
2991+
or block.type == 'function' then
29902992
block.hasReturn = true
29912993
break
29922994
end

script/parser/guide.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,4 +1276,10 @@ function m.isBasicType(str)
12761276
return basicTypeMap[str] == true
12771277
end
12781278

1279+
---@param source parser.object
1280+
---@return boolean
1281+
function m.isBlockType(source)
1282+
return blockTypes[source.type] == true
1283+
end
1284+
12791285
return m

script/proto/diagnostic.lua

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

script/pub/pub.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ function m.task(name, params, callback)
136136
end
137137

138138
--- 接收反馈
139-
--- 返回接收到的反馈数量
140-
---@return integer
141139
function m.recieve(block)
142140
if block then
143141
local id, name, result = waiter:bpop()

0 commit comments

Comments
 (0)