Debian Codegraph @

go/analysis/passes/printf/testdata/src/b/b.go

raw symbols SCIP
1 package b
2
3 import "fmt"
4
5 // Wrapf is a printf wrapper.
6 func Wrapf(format string, args ...interface{}) { // want Wrapf:"printfWrapper"
7 fmt.Sprintf(format, args...)
8 }
9
10 // Wrap is a print wrapper.
11 func Wrap(args ...interface{}) { // want Wrap:"printWrapper"
12 fmt.Sprint(args...)
13 }
14
15 // NoWrap is not a wrapper.
16 func NoWrap(format string, args ...interface{}) {
17 }
18
19 // Wrapf2 is another printf wrapper.
20 func Wrapf2(format string, args ...interface{}) string { // want Wrapf2:"printfWrapper"
21
22 // This statement serves as an assertion that this function is a
23 // printf wrapper and that calls to it should be checked
24 // accordingly, even though the delegation below is obscured by
25 // the "("+format+")" operations.
26 if false {
27 fmt.Sprintf(format, args...)
28 }
29
30 // Effectively a printf delegation,
31 // but the printf checker can't see it.
32 return fmt.Sprintf("("+format+")", args...)
33 }
34
35 var (
36 // GlobalWrapf is assigned a literal printf wrapper
37 // in this package, and thus has a fact.
38 GlobalWrapf func(format string, args ...interface{}) // want GlobalWrapf:"printfWrapper"
39
40 // GlobalWrapf2 is also assigned, but in another package (a),
41 // and thus has no fact. Nonetheless it is checked as a
42 // wrapper in that package.
43 GlobalWrapf2 func(format string, args ...interface{})
44
45 // GlobalNonWrapf is never assigned a wrapper.
46 GlobalNonWrapf func(format string, args ...interface{})
47 )
48
49 var Struct struct {
50 // These fields follow the same pattern as the Global* vars.
51 Wrapf func(format string, args ...interface{}) // want Wrapf:"printfWrapper"
52 Wrapf2 func(format string, args ...interface{})
53 NonWrapf func(format string, args ...interface{})
54 }
55
56 func init() {
57 GlobalWrapf = func(format string, args ...any) {
58 println(fmt.Sprintf(format, args...))
59 }
60 GlobalWrapf("%s", 123) // want "GlobalWrapf format %s has arg 123 of wrong type int"
61 GlobalWrapf2("%s", 123) // nope
62 GlobalNonWrapf("%s", 123) // nope
63
64 Struct.Wrapf = func(format string, args ...any) {
65 println(fmt.Sprintf(format, args...))
66 }
67 Struct.Wrapf("%s", 123) // want "Wrapf format %s has arg 123 of wrong type int"
68 Struct.Wrapf2("%s", 123) // nope
69 Struct.NonWrapf("%s", 123) // nope
70 }