diff options
-rw-r--r-- | changelog.md | 1 | ||||
-rw-r--r-- | locale/zh-cn/script.lua | 4 | ||||
-rw-r--r-- | script/core/diagnostics/redundant-return-value.lua | 69 | ||||
-rw-r--r-- | script/files.lua | 1 | ||||
-rw-r--r-- | script/proto/diagnostic.lua | 1 | ||||
-rw-r--r-- | script/vm/runner.lua | 3 | ||||
-rw-r--r-- | script/workspace/workspace.lua | 1 | ||||
-rw-r--r-- | test/diagnostics/common.lua | 20 |
8 files changed, 97 insertions, 3 deletions
diff --git a/changelog.md b/changelog.md index 844f71d1..6f3ec93d 100644 --- a/changelog.md +++ b/changelog.md @@ -8,6 +8,7 @@ * `unknown-cast-variable` * `cast-type-mismatch` * `missing-return-value` + * `redundant-return-value` * `NEW` settings: * `diagnostics.groupSeverity` * `diagnostics.groupFileStatus` diff --git a/locale/zh-cn/script.lua b/locale/zh-cn/script.lua index c0f478c3..fba3c9ea 100644 --- a/locale/zh-cn/script.lua +++ b/locale/zh-cn/script.lua @@ -132,6 +132,10 @@ DIAG_MISSING_RETURN_VALUE = '至少需要 {min} 个返回值,但此处只返回 {rmax} 个值。' DIAG_MISSING_RETURN_VALUE_RANGE = '至少需要 {min} 个返回值,但此处只返回 {rmin} 到 {rmax} 个值。' +DIAG_REDUNDANT_RETURN_VALUE = +'最多只有 {max} 个返回值,但此处返回了第 {rmax} 个值。' +DIAG_REDUNDANT_RETURN_VALUE_RANGE = +'最多只有 {max} 个返回值,但此处返回了第 {rmin} 到第 {rmax} 个值。' MWS_NOT_SUPPORT = '{} 目前还不支持多工作目录,我可能需要重启才能支持新的工作目录...' diff --git a/script/core/diagnostics/redundant-return-value.lua b/script/core/diagnostics/redundant-return-value.lua new file mode 100644 index 00000000..8f8422fe --- /dev/null +++ b/script/core/diagnostics/redundant-return-value.lua @@ -0,0 +1,69 @@ +local files = require 'files' +local guide = require 'parser.guide' +local vm = require 'vm' +local lang = require 'language' + +local function hasDocReturn(func) + if not func.bindDocs then + return false + end + for _, doc in ipairs(func.bindDocs) do + if doc.type == 'doc.return' then + return true + end + end + return false +end + +return function (uri, callback) + local state = files.getState(uri) + if not state then + return + end + + guide.eachSourceType(state.ast, 'function', function (source) + if not hasDocReturn(source) then + return + end + local _, max = vm.countReturnsOfFunction(source) + local returns = source.returns + if not returns then + return + end + for _, ret in ipairs(returns) do + local rmin, rmax = vm.countList(ret) + if rmin > max then + for i = max + 1, #ret - 1 do + callback { + start = ret[i].start, + finish = ret[i].finish, + message = lang.script('DIAG_REDUNDANT_RETURN_VALUE', { + max = max, + rmax = i, + }), + } + end + if #ret == rmax then + callback { + start = ret[#ret].start, + finish = ret[#ret].finish, + message = lang.script('DIAG_REDUNDANT_RETURN_VALUE', { + max = max, + rmax = rmax, + }), + } + else + callback { + start = ret[#ret].start, + finish = ret[#ret].finish, + message = lang.script('DIAG_REDUNDANT_RETURN_VALUE_RANGE', { + max = max, + rmin = #ret, + rmax = rmax, + }), + } + end + end + end + end) +end diff --git a/script/files.lua b/script/files.lua index 3bef6d33..d9148e27 100644 --- a/script/files.lua +++ b/script/files.lua @@ -457,7 +457,6 @@ function m.eachFile(suri) end --- Pairs dll files ----@return function function m.eachDll() local map = {} for uri, file in pairs(m.dllMap) do diff --git a/script/proto/diagnostic.lua b/script/proto/diagnostic.lua index 5972150c..e449d864 100644 --- a/script/proto/diagnostic.lua +++ b/script/proto/diagnostic.lua @@ -59,6 +59,7 @@ m.register { 'redundant-parameter', 'missing-parameter', 'missing-return-value', + 'redundant-return-value', } { group = 'unbalanced', severity = 'Warning', diff --git a/script/vm/runner.lua b/script/vm/runner.lua index 0dd33595..b8ac13fd 100644 --- a/script/vm/runner.lua +++ b/script/vm/runner.lua @@ -113,7 +113,8 @@ end ---@param action parser.object ---@param topNode vm.node ---@param outNode? vm.node ----@return vm.node +---@return vm.node topNode +---@return vm.node outNode function mt:_lookInto(action, topNode, outNode) if not action then return topNode, outNode diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua index 3aebc5e0..d24cbfe9 100644 --- a/script/workspace/workspace.lua +++ b/script/workspace/workspace.lua @@ -397,6 +397,7 @@ end ---@param uriOrPath uri|string ---@return string +---@return boolean suc function m.getRelativePath(uriOrPath) local path, uri if uriOrPath:sub(1, 5) == 'file:' then diff --git a/test/diagnostics/common.lua b/test/diagnostics/common.lua index 16773633..2d56103b 100644 --- a/test/diagnostics/common.lua +++ b/test/diagnostics/common.lua @@ -885,6 +885,7 @@ TEST [[ ---@param v T ---@param message any ---@return T +---@return any message function assert(v, message) return v, message end @@ -1734,7 +1735,6 @@ function F() end ]] -do return end TEST [[ ---@return number, number? function F() @@ -1743,6 +1743,24 @@ end ]] TEST [[ +---@return number, number? +function F() + return 1, 1, <!1!>, <!2!>, <!3!> +end +]] + +TEST [[ +---@return number, number +local function r2() end + +---@return number, number? +function F() + return 1, <!r2()!> +end +]] + +do return end +TEST [[ ---@return number function F() X = 1<!!> |