mirror of
				https://github.com/mgechev/revive.git
				synced 2025-10-30 23:37:49 +02:00 
			
		
		
		
	add testing.FailNow and related function to unreachable check (#711)
Signed-off-by: Abirdcfly <fp544037857@gmail.com>
This commit is contained in:
		| @@ -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} | ||||
|   | ||||
							
								
								
									
										23
									
								
								testdata/unreachable-code.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										23
									
								
								testdata/unreachable-code.go
									
									
									
									
										vendored
									
									
								
							| @@ -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 | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user