Debian Codegraph @

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

raw
1 package stringsbuilder
2
3 import "strings"
4
5 // basic test
6 func _() {
7 var s string
8 s += "before"
9 for range 10 {
10 s += "in" // want "using string \\+= string in a loop is inefficient"
11 s += "in2"
12 }
13 s += "after"
14 print(s)
15 }
16
17 // with initializer
18 func _() {
19 var s = "a"
20 for range 10 {
21 s += "b" // want "using string \\+= string in a loop is inefficient"
22 }
23 print(s)
24 }
25
26 // with empty initializer
27 func _() {
28 var s = ""
29 for range 10 {
30 s += "b" // want "using string \\+= string in a loop is inefficient"
31 }
32 print(s)
33 }
34
35 // with short decl
36 func _() {
37 s := "a"
38 for range 10 {
39 s += "b" // want "using string \\+= string in a loop is inefficient"
40 }
41 print(s)
42 }
43
44 // with short decl and empty initializer
45 func _() {
46 s := ""
47 for range 10 {
48 s += "b" // want "using string \\+= string in a loop is inefficient"
49 }
50 print(s)
51 }
52
53 // nope: += must appear at least once within a loop.
54 func _() {
55 var s string
56 s += "a"
57 s += "b"
58 s += "c"
59 print(s)
60 }
61
62 // nope: the declaration of s is not in a block.
63 func _() {
64 if s := "a"; true {
65 for range 10 {
66 s += "x"
67 }
68 print(s)
69 }
70 }
71
72 // in a switch (special case of "in a block" logic)
73 func _() {
74 switch {
75 default:
76 s := "a"
77 for range 10 {
78 s += "b" // want "using string \\+= string in a loop is inefficient"
79 }
80 print(s)
81 }
82 }
83
84 // nope: don't handle direct assignments to the string (only +=).
85 func _(x string) string {
86 var s string
87 s = x
88 for range 3 {
89 s += "" + x
90 }
91 return s
92 }
93
94 // Regression test for bug in a GenDecl with parens.
95 func issue75318(slice []string) string {
96 var (
97 msg string
98 )
99 for _, s := range slice {
100 msg += s // want "using string \\+= string in a loop is inefficient"
101 }
102 return msg
103 }
104
105 // Regression test for https://go.dev/issue/76983.
106 // We offer only the first fix if the second would overlap.
107 // This is an ad-hoc mitigation of the more general issue #76476.
108 func _(slice []string) string {
109 a := "12"
110 for range 2 {
111 a += "34" // want "using string \\+= string in a loop is inefficient"
112 }
113 b := "56"
114 for range 2 {
115 b += "78"
116 }
117 a += b
118 return a
119 }
120 func _(slice []string) string {
121 var a strings.Builder
122 a.WriteString("12")
123 for range 2 {
124 a.WriteString("34")
125 }
126 b := "56"
127 for range 2 {
128 b += "78" // want "using string \\+= string in a loop is inefficient"
129 }
130 a.WriteString(b)
131 return a.String()
132 }
133
134 // Regression test for go.dev/issue/76934, which mutilated the var decl.
135 func stringDeclaredWithVarDecl() {
136 var (
137 before int // this is ok
138 str = "hello world"
139 )
140 for range 100 {
141 str += "!" // want "using string \\+= string in a loop is inefficient"
142 }
143 println(before, str)
144 }
145
146 func nopeStringIsNotLastValueSpecInVarDecl() {
147 var (
148 str = "hello world"
149 after int // this defeats the transformation
150 )
151 for range 100 {
152 str += "!" // nope
153 }
154 println(str, after)
155 }
156
157 // Regression test for go.dev/issue/77659.
158 // Multiple rvalue uses of the accumulated string should all be converted.
159 func _() {
160 acc := "s1"
161 for _, s := range []string{"foo", "bar"} {
162 acc += s // want "using string \\+= string in a loop is inefficient"
163 }
164 _ = acc
165 _ = acc + "footer2"
166 }
167
168 // Regression test for go.dev/issue/77659 (negative case).
169 // += after an rvalue use should still be rejected.
170 func _() {
171 var s string
172 for _, x := range []string{"a", "b"} {
173 s += x
174 }
175 print(s)
176 s += "extra" // nope: += after rvalue use
177 print(s)
178 }
179
180 // Regression test for go.dev/issue/78465.
181 // Self-referencing += should be handled correctly.
182 func _() string {
183 a := "12"
184 for range 2 {
185 a += a // want "using string \\+= string in a loop is inefficient"
186 }
187 return a
188 }
189
190 func _() string {
191 a := "12"
192 for range 2 {
193 a += "b" + a // want "using string \\+= string in a loop is inefficient"
194 }
195 return a
196 }
197
198 func _() string {
199 a := "12"
200 for range 2 {
201 a += a + "b" // want "using string \\+= string in a loop is inefficient"
202 }
203 return a
204 }
205
206 func _() string {
207 a := "12"
208 for range 2 {
209 a += a + a // want "using string \\+= string in a loop is inefficient"
210 }
211 return a
212 }
213
214 func _() string {
215 a := "12"
216 for range 2 {
217 a += (a) // want "using string \\+= string in a loop is inefficient"
218 }
219 return a
220 }