diff options
author | Cr4xy <Cr4xy@Live.de> | 2021-10-25 14:08:32 +0200 |
---|---|---|
committer | Cr4xy <Cr4xy@Live.de> | 2021-10-25 14:08:32 +0200 |
commit | 751e5b623184005532eac6fa12f9b1094030a613 (patch) | |
tree | 8c41aebd91135971855c8a48248ee14ef0758c58 | |
parent | 8ced2703cc4598fe880cf27f2f82a90e947b532f (diff) | |
download | lua-language-server-751e5b623184005532eac6fa12f9b1094030a613.zip |
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 | 32 | ||||
-rw-r--r-- | script/proto/define.lua | 2 |
4 files changed, 36 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..3d0fc566 --- /dev/null +++ b/script/core/diagnostics/redundant-return.lua @@ -0,0 +1,32 @@ +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 + for _, node in ipairs(source) do + while node and node.type == "paren" do + node = node.exp + end + if node and (node.type ~= "nil" or #node > 0) then + return + end + 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', |