diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-10-26 10:26:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-26 10:26:59 +0800 |
commit | 40c8d318c51c51be71e3fff38c4f4a3b40f76f76 (patch) | |
tree | b1aecae2b9cc2fb56c3353aec48b836f55bcb5fc | |
parent | 8ced2703cc4598fe880cf27f2f82a90e947b532f (diff) | |
parent | a9f091bc6573fd7b82ef8591c0240f2011e1aee9 (diff) | |
download | lua-language-server-40c8d318c51c51be71e3fff38c4f4a3b40f76f76.zip |
Merge pull request #757 from Cr4xy/master
Add redundant-return diagnostic
-rw-r--r-- | locale/en-us/script.lua | 1 | ||||
-rw-r--r-- | locale/zh-cn/script.lua | 1 | ||||
-rw-r--r-- | script/core/diagnostics/redundant-return.lua | 27 | ||||
-rw-r--r-- | script/proto/define.lua | 2 | ||||
-rw-r--r-- | test/diagnostics/init.lua | 37 |
5 files changed, 68 insertions, 0 deletions
diff --git a/locale/en-us/script.lua b/locale/en-us/script.lua index fd423ced..c5e683b0 100644 --- a/locale/en-us/script.lua +++ b/locale/en-us/script.lua @@ -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.' diff --git a/locale/zh-cn/script.lua b/locale/zh-cn/script.lua index cad50612..de204e63 100644 --- a/locale/zh-cn/script.lua +++ b/locale/zh-cn/script.lua @@ -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 = '字段必须定义在类之后。' diff --git a/script/core/diagnostics/redundant-return.lua b/script/core/diagnostics/redundant-return.lua new file mode 100644 index 00000000..b76d8efa --- /dev/null +++ b/script/core/diagnostics/redundant-return.lua @@ -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 diff --git a/script/proto/define.lua b/script/proto/define.lua index 63153423..713857af 100644 --- a/script/proto/define.lua +++ b/script/proto/define.lua @@ -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', @@ -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', diff --git a/test/diagnostics/init.lua b/test/diagnostics/init.lua index c526560a..ab55cd92 100644 --- a/test/diagnostics/init.lua +++ b/test/diagnostics/init.lua @@ -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 |