github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/simple/s1038/testdata/src/example.com/CheckPrintSprintf/CheckPrintSprintf.go (about) 1 package pkg 2 3 import ( 4 "fmt" 5 "log" 6 "testing" 7 ) 8 9 func fn() { 10 fmt.Print(fmt.Sprintf("%d", 1)) //@ diag(`should use fmt.Printf`) 11 fmt.Println(fmt.Sprintf("%d", 1)) //@ diag(`don't forget the newline`) 12 fmt.Fprint(nil, fmt.Sprintf("%d", 1)) //@ diag(`should use fmt.Fprintf`) 13 fmt.Fprintln(nil, fmt.Sprintf("%d", 1)) //@ diag(`don't forget the newline`) 14 fmt.Sprint(fmt.Sprintf("%d", 1)) //@ diag(`should use fmt.Sprintf`) 15 fmt.Sprintln(fmt.Sprintf("%d", 1)) //@ diag(`don't forget the newline`) 16 17 arg := "%d" 18 fmt.Println(fmt.Sprintf(arg, 1)) 19 } 20 21 func Sprintf(string) string { return "" } 22 23 type Embedding1 struct { 24 *testing.T 25 } 26 27 type Embedding2 struct { 28 *testing.T 29 } 30 31 func (e Embedding2) Errorf() {} 32 33 type Embedding3 struct { 34 *testing.T 35 } 36 37 func (e Embedding3) Error(string, ...interface{}) {} 38 39 type Embedding4 struct { 40 testing.TB 41 } 42 43 func fn2() { 44 var t *testing.T 45 var b *testing.B 46 var tb testing.TB 47 48 // All of these are the basic cases that should be flagged 49 t.Error(fmt.Sprintf("")) //@ diag(`use t.Errorf(...) instead of t.Error(fmt.Sprintf(...))`) 50 b.Error(fmt.Sprintf("")) //@ diag(`use b.Errorf`) 51 tb.Error(fmt.Sprintf("")) //@ diag(`use tb.Errorf`) 52 t.Fatal(fmt.Sprintf("")) //@ diag(`use t.Fatalf`) 53 b.Fatal(fmt.Sprintf("")) //@ diag(`use b.Fatalf`) 54 tb.Fatal(fmt.Sprintf("")) //@ diag(`use tb.Fatalf`) 55 t.Log(fmt.Sprintf("")) //@ diag(`use t.Logf`) 56 b.Log(fmt.Sprintf("")) //@ diag(`use b.Logf`) 57 tb.Log(fmt.Sprintf("")) //@ diag(`use tb.Logf`) 58 t.Skip(fmt.Sprintf("")) //@ diag(`use t.Skipf`) 59 b.Skip(fmt.Sprintf("")) //@ diag(`use b.Skipf`) 60 tb.Skip(fmt.Sprintf("")) //@ diag(`use tb.Skipf`) 61 62 var e1 Embedding1 63 var e2 Embedding2 64 var e3 Embedding3 65 var e4 Embedding4 66 // Error and Errorf are both of *testing.common -> flag 67 e1.Error(fmt.Sprintf("")) //@ diag(`use e1.Errorf`) 68 // Fatal and Fatalf are both of *testing.common -> flag 69 e1.Fatal(fmt.Sprintf("")) //@ diag(`use e1.Fatalf`) 70 // Error is of *testing.common, but Errorf is Embedding2.Errorf -> don't flag 71 e2.Error(fmt.Sprintf("")) 72 // Fatal and Fatalf are both of *testing.common -> flag 73 e2.Fatal(fmt.Sprintf("")) //@ diag(`use e2.Fatalf`) 74 // Error is Embedding3.Error and Errorf is of *testing.common -> don't flag 75 e3.Error(fmt.Sprintf("")) 76 // Fatal and Fatalf are both of *testing.common -> flag 77 e3.Fatal(fmt.Sprintf("")) //@ diag(`use e3.Fatalf`) 78 // Error and Errorf are both of testing.TB -> flag 79 e4.Error(fmt.Sprintf("")) //@ diag(`use e4.Errorf`) 80 // Fatal and Fatalf are both of testing.TB -> flag 81 e4.Fatal(fmt.Sprintf("")) //@ diag(`use e4.Fatalf`) 82 83 // Basic cases 84 log.Fatal(fmt.Sprintf("")) //@ diag(`use log.Fatalf`) 85 log.Fatalln(fmt.Sprintf("")) //@ diag(`use log.Fatalf`) 86 log.Panic(fmt.Sprintf("")) //@ diag(`use log.Panicf`) 87 log.Panicln(fmt.Sprintf("")) //@ diag(`use log.Panicf`) 88 log.Print(fmt.Sprintf("")) //@ diag(`use log.Printf`) 89 log.Println(fmt.Sprintf("")) //@ diag(`use log.Printf`) 90 91 var l *log.Logger 92 l.Fatal(fmt.Sprintf("")) //@ diag(`use l.Fatalf(...) instead of l.Fatal(fmt.Sprintf(...)`) 93 l.Fatalln(fmt.Sprintf("")) //@ diag(`use l.Fatalf`) 94 l.Panic(fmt.Sprintf("")) //@ diag(`use l.Panicf`) 95 l.Panicln(fmt.Sprintf("")) //@ diag(`use l.Panicf`) 96 l.Print(fmt.Sprintf("")) //@ diag(`use l.Printf`) 97 l.Println(fmt.Sprintf("")) //@ diag(`use l.Printf`) 98 99 // log.Logger and testing.T share a code path, no need to check embedding again 100 }