Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: define newFasterFunctionReplacementDiagnostic and newFunctionR…
Browse files Browse the repository at this point in the history
…eplacementDiagnostic

Signed-off-by: Matthieu MOREL <[email protected]>
mmorel-35 committed Nov 21, 2024
1 parent a34cbe3 commit e61e308
Showing 3 changed files with 136 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
linters:
enable:
- gofmt
- gofumpt
220 changes: 95 additions & 125 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
@@ -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,
},
},
)
40 changes: 40 additions & 0 deletions analyzer/diagnostic.go
Original file line number Diff line number Diff line change
@@ -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 e61e308

Please sign in to comment.