Skip to content

Commit

Permalink
chore: define newFasterFunctionReplacementDiagnostic and newFunctionR…
Browse files Browse the repository at this point in the history
…eplacementDiagnostic

Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Nov 21, 2024
1 parent a34cbe3 commit 6ec2134
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 125 deletions.
220 changes: 95 additions & 125 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("),
},
},
)
Expand All @@ -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),
},
},
)
Expand All @@ -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("),
},
},
)
Expand All @@ -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("[:]"),
},
},
)
Expand All @@ -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("),
},
},
)
Expand All @@ -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(")"),
},
},
)
Expand All @@ -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("),
},
},
)
Expand All @@ -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"),
},
},
)
Expand All @@ -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,
},
},
)
Expand All @@ -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,
},
},
)
Expand Down
40 changes: 40 additions & 0 deletions analyzer/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
)
}

0 comments on commit 6ec2134

Please sign in to comment.