-
Notifications
You must be signed in to change notification settings - Fork 6
/
p.go
402 lines (364 loc) · 16.7 KB
/
p.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package p
import (
"errors"
"fmt" // want "Fix imports"
"io"
"log"
"net/url"
"os"
"time"
)
var errSentinel = errors.New("connection refused")
func positive() {
var s string
fmt.Sprintf("%s", "hello") // want "fmt.Sprintf can be replaced with just using the string"
fmt.Sprintf("%v", "hello") // want "fmt.Sprintf can be replaced with just using the string"
fmt.Sprintf("hello") // want "fmt.Sprintf can be replaced with just using the string"
fmt.Sprint("hello") // want "fmt.Sprint can be replaced with just using the string"
fmt.Sprintf("%s", s) // want "fmt.Sprintf can be replaced with just using the string"
fmt.Sprintf("%[1]s", s) // want "fmt.Sprintf can be replaced with just using the string"
fmt.Sprintf("%v", s) // want "fmt.Sprintf can be replaced with just using the string"
fmt.Sprint(s) // want "fmt.Sprint can be replaced with just using the string"
fmt.Errorf("hello") // want "fmt.Errorf can be replaced with errors.New"
fmt.Sprintf("Hello %s", s) // want "fmt.Sprintf can be replaced with string concatenation"
fmt.Sprintf("%s says Hello", s) // want "fmt.Sprintf can be replaced with string concatenation"
fmt.Sprintf("Hello says %[1]s", s) // want "fmt.Sprintf can be replaced with string concatenation"
var err error
fmt.Sprintf("%s", errSentinel) // want "fmt.Sprintf can be replaced with errSentinel.Error()"
fmt.Sprintf("%v", errSentinel) // want "fmt.Sprintf can be replaced with errSentinel.Error()"
fmt.Sprint(errSentinel) // want "fmt.Sprint can be replaced with errSentinel.Error()"
fmt.Sprintf("%s", io.EOF) // want "fmt.Sprintf can be replaced with io.EOF.Error()"
fmt.Sprintf("%v", io.EOF) // want "fmt.Sprintf can be replaced with io.EOF.Error()"
fmt.Sprint(io.EOF) // want "fmt.Sprint can be replaced with io.EOF.Error()"
fmt.Sprintf("%s", err) // want "fmt.Sprintf can be replaced with err.Error()"
fmt.Sprintf("%v", err) // want "fmt.Sprintf can be replaced with err.Error()"
fmt.Sprint(err) // want "fmt.Sprint can be replaced with err.Error()"
var b bool
fmt.Sprintf("%t", true) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
fmt.Sprintf("%v", true) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
fmt.Sprint(true) // want "fmt.Sprint can be replaced with faster strconv.FormatBool"
fmt.Sprintf("%t", false) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
fmt.Sprintf("%v", false) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
fmt.Sprint(false) // want "fmt.Sprint can be replaced with faster strconv.FormatBool"
fmt.Sprintf("%t", b) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
fmt.Sprintf("%v", b) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
fmt.Sprint(b) // want "fmt.Sprint can be replaced with faster strconv.FormatBool"
var bs []byte
var ba [3]byte
fmt.Sprintf("%x", []byte{'a'}) // want "fmt.Sprintf can be replaced with faster hex.EncodeToString"
fmt.Sprintf("%x", []uint8{'b'}) // want "fmt.Sprintf can be replaced with faster hex.EncodeToString"
fmt.Sprintf("%x", bs) // want "fmt.Sprintf can be replaced with faster hex.EncodeToString"
fmt.Sprintf("%x", ba) // want "fmt.Sprintf can be replaced with faster hex.EncodeToString"
var i int
var i8 int8
var i16 int16
var i32 int32
var i64 int64
fmt.Sprintf("%d", i) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
fmt.Sprintf("%v", i) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
fmt.Sprint(i) // want "fmt.Sprint can be replaced with faster strconv.Itoa"
fmt.Sprintf("%d", 42) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
fmt.Sprintf("%v", 42) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
fmt.Sprint(42) // want "fmt.Sprint can be replaced with faster strconv.Itoa"
fmt.Sprintf("%d", i8) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
fmt.Sprintf("%v", i8) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
fmt.Sprint(i8) // want "fmt.Sprint can be replaced with faster strconv.Itoa"
fmt.Sprintf("%d", int8(42)) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
fmt.Sprintf("%v", int8(42)) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
fmt.Sprint(int8(42)) // want "fmt.Sprint can be replaced with faster strconv.Itoa"
fmt.Sprintf("%d", i16) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
fmt.Sprintf("%v", i16) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
fmt.Sprint(i16) // want "fmt.Sprint can be replaced with faster strconv.Itoa"
fmt.Sprintf("%d", int16(42)) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
fmt.Sprintf("%v", int16(42)) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
fmt.Sprint(int16(42)) // want "fmt.Sprint can be replaced with faster strconv.Itoa"
fmt.Sprintf("%d", i32) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
fmt.Sprintf("%v", i32) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
fmt.Sprint(i32) // want "fmt.Sprint can be replaced with faster strconv.Itoa"
fmt.Sprintf("%d", int32(42)) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
fmt.Sprintf("%v", int32(42)) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
fmt.Sprint(int32(42)) // want "fmt.Sprint can be replaced with faster strconv.Itoa"
fmt.Sprintf("%d", i64) // want "fmt.Sprintf can be replaced with faster strconv.FormatInt"
fmt.Sprintf("%v", i64) // want "fmt.Sprintf can be replaced with faster strconv.FormatInt"
fmt.Sprint(i64) // want "fmt.Sprint can be replaced with faster strconv.FormatInt"
fmt.Sprintf("%d", int64(42)) // want "fmt.Sprintf can be replaced with faster strconv.FormatInt"
fmt.Sprintf("%v", int64(42)) // want "fmt.Sprintf can be replaced with faster strconv.FormatInt"
fmt.Sprint(int64(42)) // want "fmt.Sprint can be replaced with faster strconv.FormatInt"
var ui uint
var ui8 uint8
var ui16 uint16
var ui32 uint32
var ui64 uint64
fmt.Sprintf("%d", ui) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%v", ui) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprint(ui) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%d", uint(42)) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%v", uint(42)) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprint(uint(42)) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%d", ui8) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%v", ui8) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprint(ui8) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%d", uint8(42)) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%v", uint8(42)) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprint(uint8(42)) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%d", ui16) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%v", ui16) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprint(ui16) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%d", uint16(42)) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%v", uint16(42)) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprint(uint16(42)) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%d", ui32) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%v", ui32) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprint(ui32) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%d", uint32(42)) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%v", uint32(42)) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprint(uint32(42)) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%d", ui64) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%v", ui64) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%x", ui64) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%x", uint(42)) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprint(ui64) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%d", uint64(42)) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprintf("%v", uint64(42)) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
fmt.Sprint(uint64(42)) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
}
func suggestedFixesTest() {
_ = func() string {
if false {
return fmt.Sprint("replace me") // want "fmt.Sprint can be replaced with just using the string"
}
return fmt.Sprintf("%s", "replace me") // want "fmt.Sprintf can be replaced with just using the string"
}
fmt.Println(fmt.Sprint(errSentinel)) // want "fmt.Sprint can be replaced with errSentinel.Error()"
fmt.Println(fmt.Sprintf("%s", errSentinel)) // want "fmt.Sprintf can be replaced with errSentinel.Error()"
_ = func() string {
switch 42 {
case 1:
return fmt.Sprint(false) // want "fmt.Sprint can be replaced with faster strconv.FormatBool"
case 2:
return fmt.Sprintf("%t", true) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
}
return ""
}
var offset int
params := url.Values{}
params.Set("offset", fmt.Sprintf("%d", offset)) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
params.Set("offset", fmt.Sprint(offset)) // want "fmt.Sprint can be replaced with faster strconv.Itoa"
var pubKey []byte
if verifyPubKey := true; verifyPubKey {
log.Println("pubkey=" + fmt.Sprintf("%x", pubKey)) // want "fmt.Sprintf can be replaced with faster hex.EncodeToString"
}
var metaHash [16]byte
fn := fmt.Sprintf("%s.%x", "coverage.MetaFilePref", metaHash)
_ = "tmp." + fn + fmt.Sprintf("%d", time.Now().UnixNano()) // want "fmt.Sprintf can be replaced with faster strconv.FormatInt"
_ = "tmp." + fn + fmt.Sprint(time.Now().UnixNano()) // want "fmt.Sprint can be replaced with faster strconv.FormatInt"
var change struct{ User struct{ ID uint } }
var userStr string
if id := change.User.ID; id != 0 {
userStr = fmt.Sprintf("%d", id) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
userStr = fmt.Sprint(id) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
}
_ = userStr
}
func negative() {
const val = "val%d"
_ = int32(42)
fmt.Scan(42)
fmt.Scanf("%d", 42)
fmt.Println("%d", 42)
fmt.Printf("%d")
fmt.Printf("%v")
fmt.Printf("%d", 42)
fmt.Printf("%s %d", "hello", 42)
fmt.Errorf("this is %s", "complex")
fmt.Fprint(os.Stdout, "%d", 42)
fmt.Fprintf(os.Stdout, "test")
fmt.Fprintf(os.Stdout, "%d")
fmt.Fprintf(os.Stdout, "%v")
fmt.Fprintf(os.Stdout, "%d", 42)
fmt.Fprintf(os.Stdout, "%s %d", "hello", 42)
fmt.Sprint("test", 42)
fmt.Sprint(42, 42)
fmt.Sprintf("%d", 42, 42)
fmt.Sprintf("%#d", 42)
fmt.Sprintf("value %d", 42)
fmt.Sprintf(val, 42)
fmt.Sprintf("%s %v", "hello", "world")
fmt.Sprintf("%#v", 42)
fmt.Sprintf("%T", struct{ string }{})
fmt.Sprintf("%%v", 42)
fmt.Sprintf("%3d", 42)
fmt.Sprintf("% d", 42)
fmt.Sprintf("%-10d", 42)
fmt.Sprintf("%s %[1]s", "hello")
fmt.Sprintf("%[1]s %[1]s", "hello")
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
fmt.Sprintf("%[3]*.[2]*[1]f", 12.0, 2, 6)
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
// Integer.
fmt.Sprintf("%#x", uint64(42))
fmt.Sprintf("%#v", uint64(42))
fmt.Sprintf("%#b", 42)
fmt.Sprintf("%#o", 42)
fmt.Sprintf("%#x", 42)
fmt.Sprintf("%#X", 42)
fmt.Sprintf("%b", 42)
fmt.Sprintf("%c", 42)
fmt.Sprintf("%o", 42)
fmt.Sprintf("%O", 42)
fmt.Sprintf("%q", 42)
fmt.Sprintf("%x", 42)
fmt.Sprintf("%X", 42)
// Floating point.
fmt.Sprintf("%9f", 42.42)
fmt.Sprintf("%.2f", 42.42)
fmt.Sprintf("%.2f", 42.42)
fmt.Sprintf("%9.2f", 42.42)
fmt.Sprintf("%9.f", 42.42)
fmt.Sprintf("%.3g", 42.42)
fmt.Sprintf("%b", float32(42.42))
fmt.Sprintf("%e", float32(42.42))
fmt.Sprintf("%E", float32(42.42))
fmt.Sprintf("%f", float32(42.42))
fmt.Sprintf("%F", float32(42.42))
fmt.Sprintf("%g", float32(42.42))
fmt.Sprintf("%G", float32(42.42))
fmt.Sprintf("%x", float32(42.42))
fmt.Sprintf("%X", float32(42.42))
fmt.Sprintf("%v", float32(42.42))
fmt.Sprintf("%b", 42.42)
fmt.Sprintf("%e", 42.42)
fmt.Sprintf("%E", 42.42)
fmt.Sprintf("%f", 42.42)
fmt.Sprintf("%F", 42.42)
fmt.Sprintf("%g", 42.42)
fmt.Sprintf("%G", 42.42)
fmt.Sprintf("%x", 42.42)
fmt.Sprintf("%X", 42.42)
fmt.Sprintf("%v", 42.42)
fmt.Sprintf("%b", 42i+42)
fmt.Sprintf("%e", 42i+42)
fmt.Sprintf("%E", 42i+42)
fmt.Sprintf("%f", 42i+42)
fmt.Sprintf("%F", 42i+42)
fmt.Sprintf("%g", 42i+42)
fmt.Sprintf("%G", 42i+42)
fmt.Sprintf("%x", 42i+42)
fmt.Sprintf("%X", 42i+42)
fmt.Sprintf("%v", 42i+42)
// String & slice of bytes.
fmt.Sprintf("%q", "hello")
fmt.Sprintf("%#q", `"hello"`)
fmt.Sprintf("%+q", "hello")
fmt.Sprintf("%X", "hello")
// Slice.
fmt.Sprintf("%x", []uint16{'d'})
fmt.Sprintf("%x", []uint32{'d'})
fmt.Sprintf("%x", []uint64{'d'})
fmt.Sprintf("%x", []uint{'d'})
fmt.Sprintf("%x", [1]byte{'c'})
fmt.Sprintf("%x", [1]uint8{'d'})
fmt.Sprintf("%x", [1]uint16{'d'})
fmt.Sprintf("%x", [1]uint32{'d'})
fmt.Sprintf("%x", [1]uint64{'d'})
fmt.Sprintf("%x", [1]uint{'d'})
fmt.Sprintf("%x", []int8{1})
fmt.Sprintf("%x", []int16{1})
fmt.Sprintf("%x", []int32{1})
fmt.Sprintf("%x", []int64{1})
fmt.Sprintf("%x", []int{1})
fmt.Sprintf("%x", [...]int8{1})
fmt.Sprintf("%x", [...]int16{1})
fmt.Sprintf("%x", [...]int32{1})
fmt.Sprintf("%x", [...]int64{1})
fmt.Sprintf("%x", [...]int{1})
fmt.Sprintf("%x", []string{"hello"})
fmt.Sprintf("%x", []rune{'a'})
fmt.Sprintf("% x", []byte{1, 2, 3})
fmt.Sprintf("% X", []byte{1, 2, 3})
fmt.Sprintf("%p", []byte{1, 2, 3})
fmt.Sprintf("%#p", []byte{1, 2, 3})
// Pointer.
var ptr *int
fmt.Sprintf("%v", ptr)
fmt.Sprintf("%b", ptr)
fmt.Sprintf("%d", ptr)
fmt.Sprintf("%o", ptr)
fmt.Sprintf("%x", ptr)
fmt.Sprintf("%X", ptr)
}
func malformed() {
fmt.Sprintf("%d", "example")
fmt.Sprintf("%T", "example")
fmt.Sprintf("%t", "example")
fmt.Sprintf("%b", "example")
fmt.Sprintf("%e", "example")
fmt.Sprintf("%E", "example")
fmt.Sprintf("%f", "example")
fmt.Sprintf("%F", "example")
fmt.Sprintf("%g", "example")
fmt.Sprintf("%G", "example")
fmt.Sprintf("%x", "example")
fmt.Sprintf("%X", "example")
fmt.Sprintf("%d", errSentinel)
fmt.Sprintf("%T", errSentinel)
fmt.Sprintf("%t", errSentinel)
fmt.Sprintf("%b", errSentinel)
fmt.Sprintf("%e", errSentinel)
fmt.Sprintf("%E", errSentinel)
fmt.Sprintf("%f", errSentinel)
fmt.Sprintf("%F", errSentinel)
fmt.Sprintf("%g", errSentinel)
fmt.Sprintf("%G", errSentinel)
fmt.Sprintf("%x", errSentinel)
fmt.Sprintf("%X", errSentinel)
fmt.Sprintf("%d", true)
fmt.Sprintf("%T", true)
fmt.Sprintf("%b", true)
fmt.Sprintf("%e", true)
fmt.Sprintf("%E", true)
fmt.Sprintf("%f", true)
fmt.Sprintf("%F", true)
fmt.Sprintf("%g", true)
fmt.Sprintf("%G", true)
fmt.Sprintf("%x", true)
fmt.Sprintf("%X", true)
var bb []byte
fmt.Sprintf("%d", bb)
fmt.Sprintf("%T", bb)
fmt.Sprintf("%t", bb)
fmt.Sprintf("%b", bb)
fmt.Sprintf("%e", bb)
fmt.Sprintf("%E", bb)
fmt.Sprintf("%f", bb)
fmt.Sprintf("%F", bb)
fmt.Sprintf("%g", bb)
fmt.Sprintf("%G", bb)
fmt.Sprintf("%X", bb)
fmt.Sprintf("%v", bb)
fmt.Sprintf("%T", 42)
fmt.Sprintf("%t", 42)
fmt.Sprintf("%b", 42)
fmt.Sprintf("%e", 42)
fmt.Sprintf("%E", 42)
fmt.Sprintf("%f", 42)
fmt.Sprintf("%F", 42)
fmt.Sprintf("%g", 42)
fmt.Sprintf("%G", 42)
fmt.Sprintf("%x", 42)
fmt.Sprintf("%X", 42)
fmt.Sprintf("%T", uint(42))
fmt.Sprintf("%t", uint(42))
fmt.Sprintf("%b", uint(42))
fmt.Sprintf("%e", uint(42))
fmt.Sprintf("%E", uint(42))
fmt.Sprintf("%f", uint(42))
fmt.Sprintf("%F", uint(42))
fmt.Sprintf("%g", uint(42))
fmt.Sprintf("%G", uint(42))
fmt.Sprintf("%X", uint(42))
fmt.Sprintf("%d", 42.42)
fmt.Sprintf("%d", map[string]string{})
fmt.Sprint(make(chan int))
fmt.Sprint([]int{1, 2, 3})
}