Debian Codegraph @

go/analysis/passes/modernize/testdata/src/fmtappendf/fmtappendf.go

raw
1 package fmtappendf
2
3 import (
4 "fmt"
5 )
6
7 func two() string {
8 return "two"
9 }
10
11 func bye() {
12 _ = []byte(fmt.Sprintf("bye %d", 1)) // want "Replace .*Sprintf.* with fmt.Appendf"
13 }
14
15 func funcsandvars() {
16 one := "one"
17 _ = []byte(fmt.Sprintf("bye %d %s %s", 1, two(), one)) // want "Replace .*Sprintf.* with fmt.Appendf"
18 }
19
20 func typealias() {
21 type b = byte
22 type bt = []byte
23 _ = []b(fmt.Sprintf("bye %d", 1)) // want "Replace .*Sprintf.* with fmt.Appendf"
24 _ = bt(fmt.Sprintf("bye %d", 1)) // want "Replace .*Sprintf.* with fmt.Appendf"
25 }
26
27 func otherprints() {
28 _ = []byte(fmt.Sprint("bye %d", 1)) // want "Replace .*Sprint.* with fmt.Append"
29 _ = []byte(fmt.Sprintln("bye %d", 1)) // want "Replace .*Sprintln.* with fmt.Appendln"
30 }
31
32 func comma() {
33 type S struct{ Bytes []byte }
34 var _ = struct{ A S }{
35 A: S{
36 Bytes: []byte( // want "Replace .*Sprint.* with fmt.Appendf"
37 fmt.Sprintf("%d", 0),
38 ),
39 },
40 }
41 _ = []byte( // want "Replace .*Sprint.* with fmt.Appendf"
42 fmt.Sprintf("%d", 0),
43 )
44 }
45
46 func emptystring() {
47 // empty string edge case only applies to Sprintf
48 _ = []byte(fmt.Sprintln("")) // want "Replace .*Sprintln.* with fmt.Appendln"
49 // nope - these return []byte{}, while the fmt.Append version returns nil
50 _ = []byte(fmt.Sprint(""))
51 _ = []byte(fmt.Sprintf("%s", ""))
52 _ = []byte(fmt.Sprintf("%#s", ""))
53 _ = []byte(fmt.Sprintf("%s%v", "", getString()))
54 // conservatively omitting a suggested fix (ignoring precision and args)
55 _ = []byte(fmt.Sprintf("%.0q", "notprinted"))
56 _ = []byte(fmt.Sprintf("%v", "nonempty"))
57 // has non-operation characters
58 _ = []byte(fmt.Sprintf("%vother", "")) // want "Replace .*Sprint.* with fmt.Appendf"
59 }
60
61 func multiline() []byte {
62 _ = []byte( // want "Replace .*Sprintf.* with fmt.Appendf"
63 fmt.Sprintf("str %d", 1))
64
65 return []byte( // want "Replace .*Sprintf.* with fmt.Appendf"
66 fmt.Sprintf("str %d", 1),
67 )
68 }
69
70 func getString() string {
71 return ""
72 }