diff --git a/staticcheck/sa1006/sa1006.go b/staticcheck/sa1006/sa1006.go index 8c9bdbbcc..3a1709b2d 100644 --- a/staticcheck/sa1006/sa1006.go +++ b/staticcheck/sa1006/sa1006.go @@ -4,6 +4,7 @@ import ( "fmt" "go/ast" "go/types" + "strings" "honnef.co/go/tools/analysis/code" "honnef.co/go/tools/analysis/edit" @@ -57,7 +58,12 @@ func run(pass *analysis.Pass) (interface{}, error) { var arg int switch name { - case "fmt.Printf", "fmt.Sprintf", "log.Printf": + case "fmt.Errorf", "fmt.Printf", "fmt.Sprintf", + "log.Fatalf", "log.Panicf", "log.Printf", "(*log.Logger).Printf", + "(*testing.common).Logf", "(*testing.common).Errorf", + "(*testing.common).Fatalf", "(*testing.common).Skipf", + "(testing.TB).Logf", "(testing.TB).Errorf", + "(testing.TB).Fatalf", "(testing.TB).Skipf": arg = knowledge.Arg("fmt.Printf.format") case "fmt.Fprintf": arg = knowledge.Arg("fmt.Fprintf.format") @@ -80,7 +86,11 @@ func run(pass *analysis.Pass) (interface{}, error) { return } - alt := name[:len(name)-1] + alt := name[:len(name)-1] // fmt.Printf + if alt[0] == '(' { // (*log.Logger).Printf + _, alt, _ = strings.Cut(alt, ")") + alt = call.Fun.(*ast.SelectorExpr).X.(*ast.Ident).Name + alt + } report.Report(pass, call, "printf-style function with dynamic format string and no further arguments should use print-style function instead", report.Fixes(edit.Fix(fmt.Sprintf("use %s instead of %s", alt, name), edit.ReplaceWithString(call.Fun, alt)))) diff --git a/staticcheck/sa1006/testdata/src/example.com/CheckUnsafePrintf/CheckUnsafePrintf.go b/staticcheck/sa1006/testdata/src/example.com/CheckUnsafePrintf/CheckUnsafePrintf.go index e4813f8f4..37ea094c8 100644 --- a/staticcheck/sa1006/testdata/src/example.com/CheckUnsafePrintf/CheckUnsafePrintf.go +++ b/staticcheck/sa1006/testdata/src/example.com/CheckUnsafePrintf/CheckUnsafePrintf.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "testing" ) func fn(s string) { @@ -20,6 +21,28 @@ func fn(s string) { fmt.Printf("") fmt.Printf("%s", "") fmt.Printf(fn3()) + + l := log.New(os.Stdout, "", 0) + l.Printf("xx: %q", "yy") + l.Printf(s) //@ diag(`should use print-style function`) + + var t testing.T + t.Logf(fn2()) //@ diag(`should use print-style function`) + t.Errorf(s) //@ diag(`should use print-style function`) + t.Fatalf(s) //@ diag(`should use print-style function`) + t.Skipf(s) //@ diag(`should use print-style function`) + + var b testing.B + b.Logf(fn2()) //@ diag(`should use print-style function`) + b.Errorf(s) //@ diag(`should use print-style function`) + b.Fatalf(s) //@ diag(`should use print-style function`) + b.Skipf(s) //@ diag(`should use print-style function`) + + var tb testing.TB + tb.Logf(fn2()) //@ diag(`should use print-style function`) + tb.Errorf(s) //@ diag(`should use print-style function`) + tb.Fatalf(s) //@ diag(`should use print-style function`) + tb.Skipf(s) //@ diag(`should use print-style function`) } func fn3() (string, int) { return "", 0 } diff --git a/staticcheck/sa1006/testdata/src/example.com/CheckUnsafePrintf/CheckUnsafePrintf.go.golden b/staticcheck/sa1006/testdata/src/example.com/CheckUnsafePrintf/CheckUnsafePrintf.go.golden index ef2c9c950..485ad3f59 100644 --- a/staticcheck/sa1006/testdata/src/example.com/CheckUnsafePrintf/CheckUnsafePrintf.go.golden +++ b/staticcheck/sa1006/testdata/src/example.com/CheckUnsafePrintf/CheckUnsafePrintf.go.golden @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "testing" ) func fn(s string) { @@ -20,6 +21,28 @@ func fn(s string) { fmt.Printf("") fmt.Printf("%s", "") fmt.Printf(fn3()) + + l := log.New(os.Stdout, "", 0) + l.Printf("xx: %q", "yy") + l.Print(s) //@ diag(`should use print-style function`) + + var t testing.T + t.Log(fn2()) //@ diag(`should use print-style function`) + t.Error(s) //@ diag(`should use print-style function`) + t.Fatal(s) //@ diag(`should use print-style function`) + t.Skip(s) //@ diag(`should use print-style function`) + + var b testing.B + b.Log(fn2()) //@ diag(`should use print-style function`) + b.Error(s) //@ diag(`should use print-style function`) + b.Fatal(s) //@ diag(`should use print-style function`) + b.Skip(s) //@ diag(`should use print-style function`) + + var tb testing.TB + tb.Log(fn2()) //@ diag(`should use print-style function`) + tb.Error(s) //@ diag(`should use print-style function`) + tb.Fatal(s) //@ diag(`should use print-style function`) + tb.Skip(s) //@ diag(`should use print-style function`) } func fn3() (string, int) { return "", 0 }