Debian Codegraph @

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

raw
1 package fmtappendf
2
3 import (
4 "fmt"
5 )
6
7 func two() string {
8 return "two"
9 }
10
11 func bye() {
12 _ = fmt.Appendf(nil, "bye %d", 1) // want "Replace .*Sprintf.* with fmt.Appendf"
13 }
14
15 func funcsandvars() {
16 one := "one"
17 _ = fmt.Appendf(nil, "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 _ = fmt.Appendf(nil, "bye %d", 1) // want "Replace .*Sprintf.* with fmt.Appendf"
24 _ = fmt.Appendf(nil, "bye %d", 1) // want "Replace .*Sprintf.* with fmt.Appendf"
25 }
26
27 func otherprints() {
28 _ = fmt.Append(nil, "bye %d", 1) // want "Replace .*Sprint.* with fmt.Append"
29 _ = fmt.Appendln(nil, "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: fmt.Appendf(nil, "%d", 0),
37 },
38 }
39 _ = fmt.Appendf(nil, "%d", 0)
40 }
41
42 func emptystring() {
43 // empty string edge case only applies to Sprintf
44 _ = fmt.Appendln(nil, "") // want "Replace .*Sprintln.* with fmt.Appendln"
45 // nope - these return []byte{}, while the fmt.Append version returns nil
46 _ = []byte(fmt.Sprint(""))
47 _ = []byte(fmt.Sprintf("%s", ""))
48 _ = []byte(fmt.Sprintf("%#s", ""))
49 _ = []byte(fmt.Sprintf("%s%v", "", getString()))
50 // conservatively omitting a suggested fix (ignoring precision and args)
51 _ = []byte(fmt.Sprintf("%.0q", "notprinted"))
52 _ = []byte(fmt.Sprintf("%v", "nonempty"))
53 // has non-operation characters
54 _ = fmt.Appendf(nil, "%vother", "") // want "Replace .*Sprint.* with fmt.Appendf"
55 }
56
57 func multiline() []byte {
58 _ = fmt.Appendf(nil, "str %d", 1)
59
60 return fmt.Appendf(nil, "str %d", 1)
61 }
62
63 func getString() string {
64 return ""
65 }