Skip to content

Commit 751e5b6

Browse files
committed
add redundant-return diagnostic
1 parent 8ced270 commit 751e5b6

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

locale/en-us/script.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ DIAG_COUNT_DOWN_LOOP = 'Do you mean `{}` ?'
4242
DIAG_IMPLICIT_ANY = 'Can not infer type.'
4343
DIAG_DEPRECATED = 'Deprecated.'
4444
DIAG_DIFFERENT_REQUIRES = 'The same file is required with different names.'
45+
DIAG_REDUNDANT_RETURN = 'Redundant return.'
4546

4647
DIAG_CIRCLE_DOC_CLASS = 'Circularly inherited classes.'
4748
DIAG_DOC_FIELD_NO_CLASS = 'The field must be defined after the class.'

locale/zh-cn/script.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ DIAG_COUNT_DOWN_LOOP = '你的意思是 `{}` 吗?'
4242
DIAG_IMPLICIT_ANY = '无法推测出类型。'
4343
DIAG_DEPRECATED = '已废弃。'
4444
DIAG_DIFFERENT_REQUIRES = '使用了不同的名字 require 了同一个文件。'
45+
DIAG_REDUNDANT_RETURN = '冗余返回。'
4546

4647
DIAG_CIRCLE_DOC_CLASS = '循环继承的类。'
4748
DIAG_DOC_FIELD_NO_CLASS = '字段必须定义在类之后。'
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
local files = require 'files'
2+
local guide = require 'parser.guide'
3+
local lang = require 'language'
4+
local define = require 'proto.define'
5+
6+
-- reports 'return' or 'return nil' at the end of functions
7+
return function (uri, callback)
8+
local ast = files.getState(uri)
9+
if not ast then
10+
return
11+
end
12+
13+
guide.eachSourceType(ast.ast, 'return', function (source)
14+
if not source.parent or source.parent.type ~= "function" then
15+
return
16+
end
17+
for _, node in ipairs(source) do
18+
while node and node.type == "paren" do
19+
node = node.exp
20+
end
21+
if node and (node.type ~= "nil" or #node > 0) then
22+
return
23+
end
24+
end
25+
callback {
26+
start = source.start,
27+
finish = source.finish,
28+
tags = { define.DiagnosticTag.Unnecessary },
29+
message = lang.script.DIAG_REDUNDANT_RETURN,
30+
}
31+
end)
32+
end

script/proto/define.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ m.DiagnosticDefaultSeverity = {
2929
['newline-call'] = 'Information',
3030
['newfield-call'] = 'Warning',
3131
['redundant-parameter'] = 'Warning',
32+
['redundant-return'] = 'Warning',
3233
['ambiguity-1'] = 'Warning',
3334
['lowercase-global'] = 'Information',
3435
['undefined-env-child'] = 'Information',
@@ -82,6 +83,7 @@ m.DiagnosticDefaultNeededFileStatus = {
8283
['newline-call'] = 'Any',
8384
['newfield-call'] = 'Any',
8485
['redundant-parameter'] = 'Opened',
86+
['redundant-return'] = 'Opened',
8587
['ambiguity-1'] = 'Any',
8688
['lowercase-global'] = 'Any',
8789
['undefined-env-child'] = 'Any',

0 commit comments

Comments
 (0)