Skip to content

Commit fcc59ad

Browse files
authored
add testing.FailNow and related function to unreachable check (#711)
Signed-off-by: Abirdcfly <[email protected]>
1 parent 20101b3 commit fcc59ad

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

rule/unreachable-code.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ 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+
}
1924
branchingFunctions := map[string]map[string]bool{
2025
"os": {"Exit": true},
2126
"log": {
@@ -26,6 +31,9 @@ func (*UnreachableCodeRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fail
2631
"Panicf": true,
2732
"Panicln": true,
2833
},
34+
"t": testingFunctions,
35+
"b": testingFunctions,
36+
"f": testingFunctions,
2937
}
3038

3139
w := lintUnreachableCode{onFailure, branchingFunctions}

testdata/unreachable-code.go

Lines changed: 23 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,25 @@ 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+
}
60+
}

0 commit comments

Comments
 (0)