blob: 4f0a38b72f14cdc013597a6a0372acc5609ecac3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
local files = require 'files'
local guide = require 'parser.guide'
local vm = require 'vm'
local lang = require 'language'
local await = require 'await'
local define = require 'proto.define'
---@param source parser.object
---@return boolean
local function allLiteral(source)
local result = true
guide.eachSource(source, function (src)
if src.type ~= 'unary'
and src.type ~= 'binary'
and not guide.isLiteral(src) then
result = false
return false
end
end)
return result
end
---@param block parser.object
---@return boolean
local function hasReturn(block)
if block.hasReturn or block.hasError then
return true
end
if block.type == 'if' then
local hasElse
for _, subBlock in ipairs(block) do
if not hasReturn(subBlock) then
return false
end
if subBlock.type == 'elseblock' then
hasElse = true
end
end
return hasElse == true
else
if block.type == 'while' then
if vm.testCondition(block.filter)
and not block.breaks
and allLiteral(block.filter) then
return true
end
end
for _, action in ipairs(block) do
if guide.isBlockType(action) then
if hasReturn(action) then
return true
end
end
end
end
return false
end
---@async
return function (uri, callback)
local state = files.getState(uri)
if not state then
return
end
---@async
guide.eachSourceTypes(state.ast, {'main', 'function'}, function (source)
await.delay()
for i, action in ipairs(source) do
if guide.isBlockType(action)
and hasReturn(action) then
if i < #source then
callback {
start = source[i+1].start,
finish = source[#source].finish,
tags = { define.DiagnosticTag.Unnecessary },
message = lang.script('DIAG_UNREACHABLE_CODE'),
}
end
return
end
end
end)
end
|