From 6ec21348967beecfbf19b5b9bfdaf0834850ee11 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Thu, 21 Nov 2024 21:40:46 +0100 Subject: [PATCH] chore: define newFasterFunctionReplacementDiagnostic and newFunctionReplacementDiagnostic Signed-off-by: Matthieu MOREL --- analyzer/analyzer.go | 220 ++++++++++++++++++----------------------- analyzer/diagnostic.go | 40 ++++++++ 2 files changed, 135 insertions(+), 125 deletions(-) diff --git a/analyzer/analyzer.go b/analyzer/analyzer.go index 6e9d1b0..61b5ec7 100644 --- a/analyzer/analyzer.go +++ b/analyzer/analyzer.go @@ -163,18 +163,16 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) { removedFmtUsages[fname]++ if fn == "fmt.Errorf" { neededPackages[fname]["errors"] = true - d = newAnalysisDiagnostic( + d = newFunctionReplacementDiagnostic( "", // TODO: precise checker call, - fn+" can be replaced with errors.New", - []analysis.SuggestedFix{ + fn, + "errors.New", + []analysis.TextEdit{ { - Message: "Use errors.New", - TextEdits: []analysis.TextEdit{{ - Pos: call.Pos(), - End: value.Pos(), - NewText: []byte("errors.New("), - }}, + Pos: call.Pos(), + End: value.Pos(), + NewText: []byte("errors.New("), }, }, ) @@ -201,18 +199,16 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) { errMethodCall := formatNode(pass.Fset, value) + ".Error()" fname := pass.Fset.File(call.Pos()).Name() removedFmtUsages[fname]++ - d = newAnalysisDiagnostic( + d = newFunctionReplacementDiagnostic( "", // TODO: precise checker call, - fn+" can be replaced with "+errMethodCall, - []analysis.SuggestedFix{ + fn, + errMethodCall, + []analysis.TextEdit{ { - Message: "Use " + errMethodCall, - TextEdits: []analysis.TextEdit{{ - Pos: call.Pos(), - End: call.End(), - NewText: []byte(errMethodCall), - }}, + Pos: call.Pos(), + End: call.End(), + NewText: []byte(errMethodCall), }, }, ) @@ -225,18 +221,16 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) { neededPackages[fname] = make(map[string]bool) } neededPackages[fname]["strconv"] = true - d = newAnalysisDiagnostic( + d = newFasterFunctionReplacementDiagnostic( "", // TODO: precise checker call, - fn+" can be replaced with faster strconv.FormatBool", - []analysis.SuggestedFix{ + fn, + "strconv.FormatBool", + []analysis.TextEdit{ { - Message: "Use strconv.FormatBool", - TextEdits: []analysis.TextEdit{{ - Pos: call.Pos(), - End: value.Pos(), - NewText: []byte("strconv.FormatBool("), - }}, + Pos: call.Pos(), + End: value.Pos(), + NewText: []byte("strconv.FormatBool("), }, }, ) @@ -254,25 +248,21 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) { neededPackages[fname] = make(map[string]bool) } neededPackages[fname]["encoding/hex"] = true - d = newAnalysisDiagnostic( + d = newFasterFunctionReplacementDiagnostic( "", // TODO: precise checker call, - fn+" can be replaced with faster hex.EncodeToString", - []analysis.SuggestedFix{ + fn, + "hex.EncodeToString", + []analysis.TextEdit{ { - Message: "Use hex.EncodeToString", - TextEdits: []analysis.TextEdit{ - { - Pos: call.Pos(), - End: value.Pos(), - NewText: []byte("hex.EncodeToString("), - }, - { - Pos: value.End(), - End: value.End(), - NewText: []byte("[:]"), - }, - }, + Pos: call.Pos(), + End: value.Pos(), + NewText: []byte("hex.EncodeToString("), + }, + { + Pos: value.End(), + End: value.End(), + NewText: []byte("[:]"), }, }, ) @@ -284,18 +274,16 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) { neededPackages[fname] = make(map[string]bool) } neededPackages[fname]["encoding/hex"] = true - d = newAnalysisDiagnostic( + d = newFasterFunctionReplacementDiagnostic( "", // TODO: precise checker call, - fn+" can be replaced with faster hex.EncodeToString", - []analysis.SuggestedFix{ + fn, + "hex.EncodeToString", + []analysis.TextEdit{ { - Message: "Use hex.EncodeToString", - TextEdits: []analysis.TextEdit{{ - Pos: call.Pos(), - End: value.Pos(), - NewText: []byte("hex.EncodeToString("), - }}, + Pos: call.Pos(), + End: value.Pos(), + NewText: []byte("hex.EncodeToString("), }, }, ) @@ -308,25 +296,21 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) { neededPackages[fname] = make(map[string]bool) } neededPackages[fname]["strconv"] = true - d = newAnalysisDiagnostic( + d = newFasterFunctionReplacementDiagnostic( "", // TODO: precise checker call, - fn+" can be replaced with faster strconv.Itoa", - []analysis.SuggestedFix{ + fn, + "strconv.Itoa", + []analysis.TextEdit{ { - Message: "Use strconv.Itoa", - TextEdits: []analysis.TextEdit{ - { - Pos: call.Pos(), - End: value.Pos(), - NewText: []byte("strconv.Itoa(int("), - }, - { - Pos: value.End(), - End: value.End(), - NewText: []byte(")"), - }, - }, + Pos: call.Pos(), + End: value.Pos(), + NewText: []byte("strconv.Itoa(int("), + }, + { + Pos: value.End(), + End: value.End(), + NewText: []byte(")"), }, }, ) @@ -338,18 +322,16 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) { neededPackages[fname] = make(map[string]bool) } neededPackages[fname]["strconv"] = true - d = newAnalysisDiagnostic( + d = newFasterFunctionReplacementDiagnostic( "", // TODO: precise checker call, - fn+" can be replaced with faster strconv.Itoa", - []analysis.SuggestedFix{ + fn, + "strconv.Itoa", + []analysis.TextEdit{ { - Message: "Use strconv.Itoa", - TextEdits: []analysis.TextEdit{{ - Pos: call.Pos(), - End: value.Pos(), - NewText: []byte("strconv.Itoa("), - }}, + Pos: call.Pos(), + End: value.Pos(), + NewText: []byte("strconv.Itoa("), }, }, ) @@ -361,25 +343,21 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) { neededPackages[fname] = make(map[string]bool) } neededPackages[fname]["strconv"] = true - d = newAnalysisDiagnostic( + d = newFasterFunctionReplacementDiagnostic( "", // TODO: precise checker call, - fn+" can be replaced with faster strconv.FormatInt", - []analysis.SuggestedFix{ + fn, + "strconv.FormatInt", + []analysis.TextEdit{ { - Message: "Use strconv.FormatInt", - TextEdits: []analysis.TextEdit{ - { - Pos: call.Pos(), - End: value.Pos(), - NewText: []byte("strconv.FormatInt("), - }, - { - Pos: value.End(), - End: value.End(), - NewText: []byte(", 10"), - }, - }, + Pos: call.Pos(), + End: value.Pos(), + NewText: []byte("strconv.FormatInt("), + }, + { + Pos: value.End(), + End: value.End(), + NewText: []byte(", 10"), }, }, ) @@ -396,25 +374,21 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) { neededPackages[fname] = make(map[string]bool) } neededPackages[fname]["strconv"] = true - d = newAnalysisDiagnostic( + d = newFasterFunctionReplacementDiagnostic( "", // TODO: precise checker call, - fn+" can be replaced with faster strconv.FormatUint", - []analysis.SuggestedFix{ + fn, + "strconv.FormatUint", + []analysis.TextEdit{ { - Message: "Use strconv.FormatUint", - TextEdits: []analysis.TextEdit{ - { - Pos: call.Pos(), - End: value.Pos(), - NewText: []byte("strconv.FormatUint(uint64("), - }, - { - Pos: value.End(), - End: value.End(), - NewText: base, - }, - }, + Pos: call.Pos(), + End: value.Pos(), + NewText: []byte("strconv.FormatUint(uint64("), + }, + { + Pos: value.End(), + End: value.End(), + NewText: base, }, }, ) @@ -430,25 +404,21 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) { neededPackages[fname] = make(map[string]bool) } neededPackages[fname]["strconv"] = true - d = newAnalysisDiagnostic( + d = newFasterFunctionReplacementDiagnostic( "", // TODO: precise checker call, - fn+" can be replaced with faster strconv.FormatUint", - []analysis.SuggestedFix{ + fn, + "strconv.FormatUint", + []analysis.TextEdit{ { - Message: "Use strconv.FormatUint", - TextEdits: []analysis.TextEdit{ - { - Pos: call.Pos(), - End: value.Pos(), - NewText: []byte("strconv.FormatUint("), - }, - { - Pos: value.End(), - End: value.End(), - NewText: base, - }, - }, + Pos: call.Pos(), + End: value.Pos(), + NewText: []byte("strconv.FormatUint("), + }, + { + Pos: value.End(), + End: value.End(), + NewText: base, }, }, ) diff --git a/analyzer/diagnostic.go b/analyzer/diagnostic.go index 9384c6e..539010f 100644 --- a/analyzer/diagnostic.go +++ b/analyzer/diagnostic.go @@ -23,3 +23,43 @@ func newAnalysisDiagnostic( } return &d } + +func newFasterFunctionReplacementDiagnostic( + checker string, + analysisRange analysis.Range, + function string, + suggestedFunction string, + textEdits []analysis.TextEdit, +) *analysis.Diagnostic { + return newAnalysisDiagnostic( + checker, + analysisRange, + function+" can be replaced with faster "+suggestedFunction, + []analysis.SuggestedFix{ + { + Message: "Use " + suggestedFunction, + TextEdits: textEdits, + }, + }, + ) +} + +func newFunctionReplacementDiagnostic( + checker string, + analysisRange analysis.Range, + function string, + suggestedFunction string, + textEdits []analysis.TextEdit, +) *analysis.Diagnostic { + return newAnalysisDiagnostic( + checker, + analysisRange, + function+" can be replaced with "+suggestedFunction, + []analysis.SuggestedFix{ + { + Message: "Use " + suggestedFunction, + TextEdits: textEdits, + }, + }, + ) +}