Skip to content

Commit 29eec50

Browse files
committed
add testing.FailNow testing.SkipNow and related function to unreachable check
Signed-off-by: Abirdcfly <[email protected]>
1 parent 60e1ae4 commit 29eec50

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

rule/unreachable-code.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ func (*UnreachableCodeRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fail
1616
failures = append(failures, failure)
1717
}
1818

19+
testingFunctions := map[string]bool{
20+
"Fatal": true,
21+
"Fatalf": true,
22+
"FailNow": true,
23+
"Skip": true,
24+
"Skipf": true,
25+
"SkipNow": true,
26+
}
1927
branchingFunctions := map[string]map[string]bool{
2028
"os": {"Exit": true},
2129
"log": {
@@ -26,6 +34,9 @@ func (*UnreachableCodeRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fail
2634
"Panicf": true,
2735
"Panicln": true,
2836
},
37+
"t": testingFunctions,
38+
"b": testingFunctions,
39+
"f": testingFunctions,
2940
}
3041

3142
w := lintUnreachableCode{onFailure, branchingFunctions}

testdata/unreachable-code.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66
"os"
7+
"testing"
78
)
89

910
func foo() int {
@@ -35,3 +36,40 @@ func g() {
3536

3637
fmt.Println("Bye, playground")
3738
}
39+
40+
func TestA(t *testing.T) {
41+
tests := make([]int, 100)
42+
for i := range tests {
43+
println("i: ", i)
44+
if i == 0 {
45+
t.Fatal("i == 0") // MATCH /unreachable code after this statement/
46+
println("unreachable")
47+
continue
48+
}
49+
if i == 1 {
50+
t.Fatalf("i:%d", i) // MATCH /unreachable code after this statement/
51+
println("unreachable")
52+
continue
53+
}
54+
if i == 2 {
55+
t.FailNow() // MATCH /unreachable code after this statement/
56+
println("unreachable")
57+
continue
58+
}
59+
if i == 3 {
60+
t.Skip("i == 3") // MATCH /unreachable code after this statement/
61+
println("unreachable")
62+
continue
63+
}
64+
if i == 4 {
65+
t.Skipf("i:%d", i) // MATCH /unreachable code after this statement/
66+
println("unreachable")
67+
continue
68+
}
69+
if i == 5 {
70+
t.SkipNow() // MATCH /unreachable code after this statement/
71+
println("unreachable")
72+
continue
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)