File: unreachable_code_spec.lua

package info (click to toggle)
luacheck 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,704 kB
  • sloc: sh: 106; makefile: 96; python: 35
file content (126 lines) | stat: -rw-r--r-- 2,342 bytes parent folder | download | duplicates (5)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
local helper = require "spec.helper"

local function assert_warnings(warnings, src)
   assert.same(warnings, helper.get_stage_warnings("detect_unreachable_code", src))
end

describe("unreachable code detection", function()
   it("detects unreachable code", function()
      assert_warnings({
         {code = "511", line = 2, column = 1, end_column = 24}
      }, [[
do return end
if ... then return 6 end
return 3
]])

      assert_warnings({
         {code = "511", line = 7, column = 1, end_column = 11},
         {code = "511", line = 13, column = 1, end_column = 8}
      }, [[
if ... then
   return 4
else
   return 6
end

if ... then
   return 7
else
   return 8
end

return 3
]])
   end)

   it("detects unreachable code with literal conditions", function()
      assert_warnings({
         {code = "511", line = 4, column = 1, end_column = 6}
      }, [[
while true do
   (...)()
end
return
]])

      assert_warnings({}, [[
repeat
   if ... then
      break
   end
until false
return
]])

      assert_warnings({
         {code = "511", line = 6, column = 1, end_column = 6}
      }, [[
repeat
   if nil then
      break
   end
until false
return
]])
   end)

   it("detects unreachable expressions", function()
      assert_warnings({
         {code = "511", line = 3, column = 7, end_column = 9}
      }, [[
repeat
   return
until ...
]])

      assert_warnings({
         {code = "511", line = 3, column = 8, end_column = 10}
      }, [[
if true then
   (...)()
elseif ... then
   (...)()
end
]])
   end)

   it("detects unreachable functions", function()
      assert_warnings({
         {code = "511", line = 3, column = 1, end_column = 16}
      }, [[
local f = nil
do return end
function f() end
]])
   end)

   it("detects unreachable code in nested function", function()
      assert_warnings({
         {code = "511", line = 4, column = 7, end_column = 12}
      }, [[
return function()
   return function()
      do return end
      return
   end
end
]])
   end)

   it("detects unreachable code in unreachable nested function", function()
      assert_warnings({
         {code = "511", line = 4, column = 4, end_column = 20},
         {code = "511", line = 6, column = 7, end_column = 12}
      }, [[
return function()
   do return end

   return function()
      do return end
      return
   end
end
]])
   end)
end)