Skip to content

Commit 68e4702

Browse files
adonovangopherbot
authored andcommitted
go/analysis/passes/printf: add missing call to Func.Origin
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>
1 parent 30a3bd9 commit 68e4702

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

go/analysis/passes/printf/printf.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,9 @@ func printfNameAndKind(pass *analysis.Pass, call *ast.CallExpr) (fn *types.Func,
433433
return nil, 0
434434
}
435435

436+
// Facts are associated with generic declarations, not instantiations.
437+
fn = fn.Origin()
438+
436439
_, ok := isPrint[fn.FullName()]
437440
if !ok {
438441
// Next look up just "printf", for use with -printf.funcs.

go/analysis/passes/printf/printf_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ func Test(t *testing.T) {
1616
printf.Analyzer.Flags.Set("funcs", "Warn,Warnf")
1717

1818
analysistest.Run(t, testdata, printf.Analyzer,
19-
"a", "b", "nofmt", "typeparams", "issue68744")
19+
"a", "b", "nofmt", "typeparams", "issue68744", "issue70572")
2020
analysistest.RunWithSuggestedFixes(t, testdata, printf.Analyzer, "fix")
2121
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package issue70572
2+
3+
// Regression test for failure to detect that a call to B[bool].Printf
4+
// was printf-like, because of a missing call to types.Func.Origin.
5+
6+
import "fmt"
7+
8+
type A struct{}
9+
10+
func (v A) Printf(format string, values ...any) { // want Printf:"printfWrapper"
11+
fmt.Printf(format, values...)
12+
}
13+
14+
type B[T any] struct{}
15+
16+
func (v B[T]) Printf(format string, values ...any) { // want Printf:"printfWrapper"
17+
fmt.Printf(format, values...)
18+
}
19+
20+
func main() {
21+
var a A
22+
var b B[bool]
23+
a.Printf("x", 1) // want "arguments but no formatting directives"
24+
b.Printf("x", 1) // want "arguments but no formatting directives"
25+
}

0 commit comments

Comments
 (0)