Skip to content

Commit 4e2b5e6

Browse files
ccoVeillecatenacyber
authored andcommitted
feat: add checker name in the diagnostic message
This will help people to understand where the diagnostic is coming from. But also, to easily understand what checker could be deactivated if they want to. The change is fully compatible with the current tests. It explains why the tests are not updated in this commit.
1 parent 5b89c6e commit 4e2b5e6

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

analyzer/analyzer.go

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ func newPerfSprint() *perfSprint {
5454
}
5555
}
5656

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+
5772
func New() *analysis.Analyzer {
5873
n := newPerfSprint()
5974
r := &analysis.Analyzer{
@@ -63,17 +78,19 @@ func New() *analysis.Analyzer {
6378
Run: n.run,
6479
Requires: []*analysis.Analyzer{inspect.Analyzer},
6580
}
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")
6783
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")
6985
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")
7086
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")
7491
r.Flags.BoolVar(&n.strFormat.sprintf1, "sprintf1", n.strFormat.sprintf1, "optimizes fmt.Sprintf with only one argument")
7592
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")
7794

7895
return r
7996
}
@@ -198,7 +215,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
198215
if fn == "fmt.Errorf" && n.errFormat.enabled {
199216
neededPackages[fname]["errors"] = struct{}{}
200217
d = newAnalysisDiagnostic(
201-
"", // TODO: precise checker
218+
checkerErrorFormat,
202219
call,
203220
fn+" can be replaced with errors.New",
204221
[]analysis.SuggestedFix{
@@ -214,7 +231,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
214231
)
215232
} else if fn != "fmt.Errorf" && n.strFormat.enabled {
216233
d = newAnalysisDiagnostic(
217-
"", // TODO: precise checker
234+
checkerStringFormat,
218235
call,
219236
fn+" can be replaced with just using the string",
220237
[]analysis.SuggestedFix{
@@ -236,7 +253,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
236253
fname := pass.Fset.File(call.Pos()).Name()
237254
removedFmtUsages[fname]++
238255
d = newAnalysisDiagnostic(
239-
"", // TODO: precise checker
256+
checkerErrorFormat,
240257
call,
241258
fn+" can be replaced with "+errMethodCall,
242259
[]analysis.SuggestedFix{
@@ -259,7 +276,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
259276
}
260277
neededPackages[fname]["strconv"] = struct{}{}
261278
d = newAnalysisDiagnostic(
262-
"", // TODO: precise checker
279+
checkerBoolFormat,
263280
call,
264281
fn+" can be replaced with faster strconv.FormatBool",
265282
[]analysis.SuggestedFix{
@@ -287,7 +304,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
287304
}
288305
neededPackages[fname]["encoding/hex"] = struct{}{}
289306
d = newAnalysisDiagnostic(
290-
"", // TODO: precise checker
307+
checkerHexFormat,
291308
call,
292309
fn+" can be replaced with faster hex.EncodeToString",
293310
[]analysis.SuggestedFix{
@@ -316,7 +333,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
316333
}
317334
neededPackages[fname]["encoding/hex"] = struct{}{}
318335
d = newAnalysisDiagnostic(
319-
"", // TODO: precise checker
336+
checkerHexFormat,
320337
call,
321338
fn+" can be replaced with faster hex.EncodeToString",
322339
[]analysis.SuggestedFix{
@@ -339,7 +356,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
339356
}
340357
neededPackages[fname]["strconv"] = struct{}{}
341358
d = newAnalysisDiagnostic(
342-
"", // TODO: precise checker
359+
checkerIntegerFormat,
343360
call,
344361
fn+" can be replaced with faster strconv.Itoa",
345362
[]analysis.SuggestedFix{
@@ -368,7 +385,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
368385
}
369386
neededPackages[fname]["strconv"] = struct{}{}
370387
d = newAnalysisDiagnostic(
371-
"", // TODO: precise checker
388+
checkerIntegerFormat,
372389
call,
373390
fn+" can be replaced with faster strconv.Itoa",
374391
[]analysis.SuggestedFix{
@@ -390,7 +407,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
390407
}
391408
neededPackages[fname]["strconv"] = struct{}{}
392409
d = newAnalysisDiagnostic(
393-
"", // TODO: precise checker
410+
checkerIntegerFormat,
394411
call,
395412
fn+" can be replaced with faster strconv.FormatInt",
396413
[]analysis.SuggestedFix{
@@ -424,7 +441,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
424441
}
425442
neededPackages[fname]["strconv"] = struct{}{}
426443
d = newAnalysisDiagnostic(
427-
"", // TODO: precise checker
444+
checkerIntegerFormat,
428445
call,
429446
fn+" can be replaced with faster strconv.FormatUint",
430447
[]analysis.SuggestedFix{
@@ -457,7 +474,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
457474
}
458475
neededPackages[fname]["strconv"] = struct{}{}
459476
d = newAnalysisDiagnostic(
460-
"", // TODO: precise checker
477+
checkerIntegerFormat,
461478
call,
462479
fn+" can be replaced with faster strconv.FormatUint",
463480
[]analysis.SuggestedFix{
@@ -492,7 +509,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
492509
fname := pass.Fset.File(call.Pos()).Name()
493510
removedFmtUsages[fname]++
494511
d = newAnalysisDiagnostic(
495-
"", // TODO: precise checker
512+
checkerStringFormat,
496513
call,
497514
fn+" can be replaced with string concatenation",
498515
[]analysis.SuggestedFix{
@@ -571,7 +588,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
571588
fix = fix + "\n\t\"" + k + `"`
572589
}
573590
pass.Report(*newAnalysisDiagnostic(
574-
"", // TODO: precise checker
591+
checkerFixImports,
575592
gd,
576593
"Fix imports",
577594
[]analysis.SuggestedFix{

0 commit comments

Comments
 (0)