Skip to content

Commit

Permalink
go/analysis/passes/printf: add missing call to Func.Origin
Browse files Browse the repository at this point in the history
A call to an instance of a generic printf wrapper should
be checked as a printf wrapper; but a missing call to
Func.Origin prevented that.

+ Test

Fixes golang/go#70572

Change-Id: I61de6dc42bfea9b152aabb895ea66962453cb0e4
Reviewed-on: https://go-review.googlesource.com/c/tools/+/631955
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
  • Loading branch information
adonovan authored and gopherbot committed Nov 26, 2024
1 parent 30a3bd9 commit 68e4702
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions go/analysis/passes/printf/printf.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ func printfNameAndKind(pass *analysis.Pass, call *ast.CallExpr) (fn *types.Func,
return nil, 0
}

// Facts are associated with generic declarations, not instantiations.
fn = fn.Origin()

_, ok := isPrint[fn.FullName()]
if !ok {
// Next look up just "printf", for use with -printf.funcs.
Expand Down
2 changes: 1 addition & 1 deletion go/analysis/passes/printf/printf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ func Test(t *testing.T) {
printf.Analyzer.Flags.Set("funcs", "Warn,Warnf")

analysistest.Run(t, testdata, printf.Analyzer,
"a", "b", "nofmt", "typeparams", "issue68744")
"a", "b", "nofmt", "typeparams", "issue68744", "issue70572")
analysistest.RunWithSuggestedFixes(t, testdata, printf.Analyzer, "fix")
}
25 changes: 25 additions & 0 deletions go/analysis/passes/printf/testdata/src/issue70572/issue70572.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package issue70572

// Regression test for failure to detect that a call to B[bool].Printf
// was printf-like, because of a missing call to types.Func.Origin.

import "fmt"

type A struct{}

func (v A) Printf(format string, values ...any) { // want Printf:"printfWrapper"
fmt.Printf(format, values...)
}

type B[T any] struct{}

func (v B[T]) Printf(format string, values ...any) { // want Printf:"printfWrapper"
fmt.Printf(format, values...)
}

func main() {
var a A
var b B[bool]
a.Printf("x", 1) // want "arguments but no formatting directives"
b.Printf("x", 1) // want "arguments but no formatting directives"
}

0 comments on commit 68e4702

Please sign in to comment.