Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions locale/en-us/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ DIAG_COUNT_DOWN_LOOP = 'Do you mean `{}` ?'
DIAG_IMPLICIT_ANY = 'Can not infer type.'
DIAG_DEPRECATED = 'Deprecated.'
DIAG_DIFFERENT_REQUIRES = 'The same file is required with different names.'
DIAG_REDUNDANT_RETURN = 'Redundant return.'

DIAG_CIRCLE_DOC_CLASS = 'Circularly inherited classes.'
DIAG_DOC_FIELD_NO_CLASS = 'The field must be defined after the class.'
Expand Down
1 change: 1 addition & 0 deletions locale/zh-cn/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ DIAG_COUNT_DOWN_LOOP = '你的意思是 `{}` 吗?'
DIAG_IMPLICIT_ANY = '无法推测出类型。'
DIAG_DEPRECATED = '已废弃。'
DIAG_DIFFERENT_REQUIRES = '使用了不同的名字 require 了同一个文件。'
DIAG_REDUNDANT_RETURN = '冗余返回。'

DIAG_CIRCLE_DOC_CLASS = '循环继承的类。'
DIAG_DOC_FIELD_NO_CLASS = '字段必须定义在类之后。'
Expand Down
27 changes: 27 additions & 0 deletions script/core/diagnostics/redundant-return.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
local files = require 'files'
local guide = require 'parser.guide'
local lang = require 'language'
local define = require 'proto.define'

-- reports 'return' or 'return nil' at the end of functions
return function (uri, callback)
local ast = files.getState(uri)
if not ast then
return
end

guide.eachSourceType(ast.ast, 'return', function (source)
if not source.parent or source.parent.type ~= "function" then
return
end
if #source > 0 then
return
end
callback {
start = source.start,
finish = source.finish,
tags = { define.DiagnosticTag.Unnecessary },
message = lang.script.DIAG_REDUNDANT_RETURN,
}
end)
end
2 changes: 2 additions & 0 deletions script/proto/define.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ m.DiagnosticDefaultSeverity = {
['newline-call'] = 'Information',
['newfield-call'] = 'Warning',
['redundant-parameter'] = 'Warning',
['redundant-return'] = 'Warning',
['ambiguity-1'] = 'Warning',
['lowercase-global'] = 'Information',
['undefined-env-child'] = 'Information',
Expand Down Expand Up @@ -82,6 +83,7 @@ m.DiagnosticDefaultNeededFileStatus = {
['newline-call'] = 'Any',
['newfield-call'] = 'Any',
['redundant-parameter'] = 'Opened',
['redundant-return'] = 'Opened',
['ambiguity-1'] = 'Any',
['lowercase-global'] = 'Any',
['undefined-env-child'] = 'Any',
Expand Down
37 changes: 37 additions & 0 deletions test/diagnostics/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,43 @@ end
local val = {}
location('uri', val)
]]

-- redundant-return
TEST [[
local function f()
<!return!>
end
f()
]]

TEST [[
local function f()
return nil
end
f()
]]

TEST [[
local function f()
local function x()
<!return!>
end
x()
return true
end
f()
]]

TEST [[
local function f()
local function x()
return true
end
return x()
end
f()
]]

---TODO(arthur)
do return end

Expand Down