Skip to content

Commit

Permalink
SA1006: flag more functions
Browse files Browse the repository at this point in the history
Flag all stdlib functions that have both a Printf() and Print() variant.

Found with:

        % rg -g '!internal/' -g '!cmd/' '^func ([()*a-zA-Z0-9_ ]+)?[A-Z]\w+\(\w+ string, \w+ \.\.\.any\)'

(will need to filter out some false positives)

Fixes dominikh#1528
  • Loading branch information
arp242 committed May 25, 2024
1 parent 5275b91 commit b36eef3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
14 changes: 12 additions & 2 deletions staticcheck/sa1006/sa1006.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"go/ast"
"go/types"
"strings"

"honnef.co/go/tools/analysis/code"
"honnef.co/go/tools/analysis/edit"
Expand Down Expand Up @@ -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")
Expand All @@ -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))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"testing"
)

func fn(s string) {
Expand All @@ -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 }
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"testing"
)

func fn(s string) {
Expand All @@ -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 }

0 comments on commit b36eef3

Please sign in to comment.