Skip to content

Commit

Permalink
add testing.FailNow and related function to unreachable check (#711)
Browse files Browse the repository at this point in the history
Signed-off-by: Abirdcfly <[email protected]>
  • Loading branch information
Abirdcfly authored Jul 20, 2022
1 parent 20101b3 commit fcc59ad
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions rule/unreachable-code.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func (*UnreachableCodeRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fail
failures = append(failures, failure)
}

testingFunctions := map[string]bool{
"Fatal": true,
"Fatalf": true,
"FailNow": true,
}
branchingFunctions := map[string]map[string]bool{
"os": {"Exit": true},
"log": {
Expand All @@ -26,6 +31,9 @@ func (*UnreachableCodeRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fail
"Panicf": true,
"Panicln": true,
},
"t": testingFunctions,
"b": testingFunctions,
"f": testingFunctions,
}

w := lintUnreachableCode{onFailure, branchingFunctions}
Expand Down
23 changes: 23 additions & 0 deletions testdata/unreachable-code.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"testing"
)

func foo() int {
Expand Down Expand Up @@ -35,3 +36,25 @@ func g() {

fmt.Println("Bye, playground")
}

func TestA(t *testing.T) {
tests := make([]int, 100)
for i := range tests {
println("i: ", i)
if i == 0 {
t.Fatal("i == 0") // MATCH /unreachable code after this statement/
println("unreachable")
continue
}
if i == 1 {
t.Fatalf("i:%d", i) // MATCH /unreachable code after this statement/
println("unreachable")
continue
}
if i == 2 {
t.FailNow() // MATCH /unreachable code after this statement/
println("unreachable")
continue
}
}
}

0 comments on commit fcc59ad

Please sign in to comment.