Repository: mgechev/revive
Pull request #711 (2022-07-20 08:56:15)

add testing.FailNow  and related function to unreachable check

Signed-off-by: Abirdcfly <fp544037857@gmail.com>

<!-- ### IMPORTANT ### -->
<!-- Please do not create a Pull Request without creating an issue first.** -->
<!-- If you're fixing a typo or improving the documentation, you may not have to open an issue. -->

<!-- ### CHECKLIST ### -->
<!-- Please, describe in details what's your motivation for this PR -->
<!-- Did you add tests? -->
<!-- Does your code follows the coding style of the rest of the repository? -->
<!-- Does the Travis build passes? -->

<!-- ### FOOTER (OPTIONAL) ### -->
<!-- If you're closing an issue add "Closes #XXXX" in your comment. This way, the PR will be linked to the issue automatically. -->

Closes  #710 


Patch:
diff --git a/go.mod b/go.mod
index acab9a3..433fa6c 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,7 @@ module github.com/mgechev/revive
 go 1.12
 
 require (
-	github.com/BurntSushi/toml v1.1.0
+	github.com/BurntSushi/toml v1.2.0
 	github.com/chavacava/garif v0.0.0-20220630083739-93517212f375
 	github.com/fatih/color v1.13.0
 	github.com/fatih/structtag v1.2.0
diff --git a/go.sum b/go.sum
index 50f10ab..5b31f32 100644
--- a/go.sum
+++ b/go.sum
@@ -2,6 +2,8 @@ github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU
 github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
 github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
 github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
+github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
+github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
 github.com/chavacava/garif v0.0.0-20210405164556-e8a0a408d6af h1:spmv8nSH9h5oCQf40jt/ufBCt9j0/58u4G+rkeMqXGI=
 github.com/chavacava/garif v0.0.0-20210405164556-e8a0a408d6af/go.mod h1:Qjyv4H3//PWVzTeCezG2b9IRn6myJxJSr4TD/xo6ojU=
 github.com/chavacava/garif v0.0.0-20220316182200-5cad0b5181d4 h1:tFXjAxje9thrTF4h57Ckik+scJjTWdwAtZqZPtOT48M=
diff --git a/rule/unreachable-code.go b/rule/unreachable-code.go
index 71ca91e..dcc5b79 100644
--- a/rule/unreachable-code.go
+++ b/rule/unreachable-code.go
@@ -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": {
@@ -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}


Test patch:
diff --git a/testdata/unreachable-code.go b/testdata/unreachable-code.go
index 346e1cb..18d86a6 100644
--- a/testdata/unreachable-code.go
+++ b/testdata/unreachable-code.go
@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"log"
 	"os"
+	"testing"
 )
 
 func foo() int {
@@ -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
+		}
+	}
+}