@@ -54,6 +54,21 @@ func newPerfSprint() *perfSprint {
54
54
}
55
55
}
56
56
57
+ const (
58
+ // checkerErrorFormat checks for error formatting.
59
+ checkerErrorFormat = "error-format"
60
+ // checkerIntegerFormat checks for integer formatting.
61
+ checkerIntegerFormat = "integer-format"
62
+ // checkerStringFormat checks for string formatting.
63
+ checkerStringFormat = "string-format"
64
+ // checkerBoolFormat checks for bool formatting.
65
+ checkerBoolFormat = "bool-format"
66
+ // checkerHexFormat checks for hexadecimal formatting.
67
+ checkerHexFormat = "hex-format"
68
+ // checkerFixImports fix needed imports from other fixes.
69
+ checkerFixImports = "fiximports"
70
+ )
71
+
57
72
func New () * analysis.Analyzer {
58
73
n := newPerfSprint ()
59
74
r := & analysis.Analyzer {
@@ -63,17 +78,19 @@ func New() *analysis.Analyzer {
63
78
Run : n .run ,
64
79
Requires : []* analysis.Analyzer {inspect .Analyzer },
65
80
}
66
- r .Flags .BoolVar (& n .intFormat .enabled , "integer-format" , n .intFormat .enabled , "enable/disable optimization of integer formatting" )
81
+
82
+ r .Flags .BoolVar (& n .intFormat .enabled , checkerIntegerFormat , n .intFormat .enabled , "enable/disable optimization of integer formatting" )
67
83
r .Flags .BoolVar (& n .intFormat .intConv , "int-conversion" , n .intFormat .intConv , "optimizes even if it requires an int or uint type cast" )
68
- r .Flags .BoolVar (& n .errFormat .enabled , "error-format" , n .errFormat .enabled , "enable/disable optimization of error formatting" )
84
+ r .Flags .BoolVar (& n .errFormat .enabled , checkerErrorFormat , n .errFormat .enabled , "enable/disable optimization of error formatting" )
69
85
r .Flags .BoolVar (& n .errFormat .errError , "err-error" , n .errFormat .errError , "optimizes into err.Error() even if it is only equivalent for non-nil errors" )
70
86
r .Flags .BoolVar (& n .errFormat .errorf , "errorf" , n .errFormat .errorf , "optimizes fmt.Errorf" )
71
- r .Flags .BoolVar (& n .boolFormat , "bool-format" , n .boolFormat , "enable/disable optimization of bool formatting" )
72
- r .Flags .BoolVar (& n .hexFormat , "hex-format" , n .hexFormat , "enable/disable optimization of hex formatting" )
73
- r .Flags .BoolVar (& n .strFormat .enabled , "string-format" , n .strFormat .enabled , "enable/disable optimization of string formatting" )
87
+
88
+ r .Flags .BoolVar (& n .boolFormat , checkerBoolFormat , n .boolFormat , "enable/disable optimization of bool formatting" )
89
+ r .Flags .BoolVar (& n .hexFormat , checkerHexFormat , n .hexFormat , "enable/disable optimization of hex formatting" )
90
+ r .Flags .BoolVar (& n .strFormat .enabled , checkerStringFormat , n .strFormat .enabled , "enable/disable optimization of string formatting" )
74
91
r .Flags .BoolVar (& n .strFormat .sprintf1 , "sprintf1" , n .strFormat .sprintf1 , "optimizes fmt.Sprintf with only one argument" )
75
92
r .Flags .BoolVar (& n .strFormat .strconcat , "strconcat" , n .strFormat .strconcat , "optimizes into strings concatenation" )
76
- r .Flags .BoolVar (& n .fiximports , "fiximports" , n .fiximports , "fix needed imports from other fixes" )
93
+ r .Flags .BoolVar (& n .fiximports , checkerFixImports , n .fiximports , "fix needed imports from other fixes" )
77
94
78
95
return r
79
96
}
@@ -198,7 +215,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
198
215
if fn == "fmt.Errorf" && n .errFormat .enabled {
199
216
neededPackages [fname ]["errors" ] = struct {}{}
200
217
d = newAnalysisDiagnostic (
201
- "" , // TODO: precise checker
218
+ checkerErrorFormat ,
202
219
call ,
203
220
fn + " can be replaced with errors.New" ,
204
221
[]analysis.SuggestedFix {
@@ -214,7 +231,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
214
231
)
215
232
} else if fn != "fmt.Errorf" && n .strFormat .enabled {
216
233
d = newAnalysisDiagnostic (
217
- "" , // TODO: precise checker
234
+ checkerStringFormat ,
218
235
call ,
219
236
fn + " can be replaced with just using the string" ,
220
237
[]analysis.SuggestedFix {
@@ -236,7 +253,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
236
253
fname := pass .Fset .File (call .Pos ()).Name ()
237
254
removedFmtUsages [fname ]++
238
255
d = newAnalysisDiagnostic (
239
- "" , // TODO: precise checker
256
+ checkerErrorFormat ,
240
257
call ,
241
258
fn + " can be replaced with " + errMethodCall ,
242
259
[]analysis.SuggestedFix {
@@ -259,7 +276,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
259
276
}
260
277
neededPackages [fname ]["strconv" ] = struct {}{}
261
278
d = newAnalysisDiagnostic (
262
- "" , // TODO: precise checker
279
+ checkerBoolFormat ,
263
280
call ,
264
281
fn + " can be replaced with faster strconv.FormatBool" ,
265
282
[]analysis.SuggestedFix {
@@ -287,7 +304,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
287
304
}
288
305
neededPackages [fname ]["encoding/hex" ] = struct {}{}
289
306
d = newAnalysisDiagnostic (
290
- "" , // TODO: precise checker
307
+ checkerHexFormat ,
291
308
call ,
292
309
fn + " can be replaced with faster hex.EncodeToString" ,
293
310
[]analysis.SuggestedFix {
@@ -316,7 +333,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
316
333
}
317
334
neededPackages [fname ]["encoding/hex" ] = struct {}{}
318
335
d = newAnalysisDiagnostic (
319
- "" , // TODO: precise checker
336
+ checkerHexFormat ,
320
337
call ,
321
338
fn + " can be replaced with faster hex.EncodeToString" ,
322
339
[]analysis.SuggestedFix {
@@ -339,7 +356,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
339
356
}
340
357
neededPackages [fname ]["strconv" ] = struct {}{}
341
358
d = newAnalysisDiagnostic (
342
- "" , // TODO: precise checker
359
+ checkerIntegerFormat ,
343
360
call ,
344
361
fn + " can be replaced with faster strconv.Itoa" ,
345
362
[]analysis.SuggestedFix {
@@ -368,7 +385,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
368
385
}
369
386
neededPackages [fname ]["strconv" ] = struct {}{}
370
387
d = newAnalysisDiagnostic (
371
- "" , // TODO: precise checker
388
+ checkerIntegerFormat ,
372
389
call ,
373
390
fn + " can be replaced with faster strconv.Itoa" ,
374
391
[]analysis.SuggestedFix {
@@ -390,7 +407,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
390
407
}
391
408
neededPackages [fname ]["strconv" ] = struct {}{}
392
409
d = newAnalysisDiagnostic (
393
- "" , // TODO: precise checker
410
+ checkerIntegerFormat ,
394
411
call ,
395
412
fn + " can be replaced with faster strconv.FormatInt" ,
396
413
[]analysis.SuggestedFix {
@@ -424,7 +441,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
424
441
}
425
442
neededPackages [fname ]["strconv" ] = struct {}{}
426
443
d = newAnalysisDiagnostic (
427
- "" , // TODO: precise checker
444
+ checkerIntegerFormat ,
428
445
call ,
429
446
fn + " can be replaced with faster strconv.FormatUint" ,
430
447
[]analysis.SuggestedFix {
@@ -457,7 +474,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
457
474
}
458
475
neededPackages [fname ]["strconv" ] = struct {}{}
459
476
d = newAnalysisDiagnostic (
460
- "" , // TODO: precise checker
477
+ checkerIntegerFormat ,
461
478
call ,
462
479
fn + " can be replaced with faster strconv.FormatUint" ,
463
480
[]analysis.SuggestedFix {
@@ -492,7 +509,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
492
509
fname := pass .Fset .File (call .Pos ()).Name ()
493
510
removedFmtUsages [fname ]++
494
511
d = newAnalysisDiagnostic (
495
- "" , // TODO: precise checker
512
+ checkerStringFormat ,
496
513
call ,
497
514
fn + " can be replaced with string concatenation" ,
498
515
[]analysis.SuggestedFix {
@@ -571,7 +588,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
571
588
fix = fix + "\n \t \" " + k + `"`
572
589
}
573
590
pass .Report (* newAnalysisDiagnostic (
574
- "" , // TODO: precise checker
591
+ checkerFixImports ,
575
592
gd ,
576
593
"Fix imports" ,
577
594
[]analysis.SuggestedFix {
0 commit comments