Skip to content

Commit

Permalink
chore: refactor Diagnostic declaration
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 authored and catenacyber committed Nov 21, 2024
1 parent f7c1bb9 commit a34cbe3
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 78 deletions.
156 changes: 78 additions & 78 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
removedFmtUsages[fname]++
if fn == "fmt.Errorf" {
neededPackages[fname]["errors"] = true
d = &analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Message: fn + " can be replaced with errors.New",
SuggestedFixes: []analysis.SuggestedFix{
d = newAnalysisDiagnostic(
"", // TODO: precise checker
call,
fn+" can be replaced with errors.New",
[]analysis.SuggestedFix{
{
Message: "Use errors.New",
TextEdits: []analysis.TextEdit{{
Expand All @@ -177,13 +177,13 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
}},
},
},
}
)
} else {
d = &analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Message: fn + " can be replaced with just using the string",
SuggestedFixes: []analysis.SuggestedFix{
d = newAnalysisDiagnostic(
"", // TODO: precise checker
call,
fn+" can be replaced with just using the string",
[]analysis.SuggestedFix{
{
Message: "Just use string value",
TextEdits: []analysis.TextEdit{{
Expand All @@ -193,19 +193,19 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
}},
},
},
}
)
}
case types.Implements(valueType, errIface) && oneOf(verb, "%v", "%s") && n.errError:
// known false positive if this error is nil
// fmt.Sprint(nil) does not panic like nil.Error() does
errMethodCall := formatNode(pass.Fset, value) + ".Error()"
fname := pass.Fset.File(call.Pos()).Name()
removedFmtUsages[fname]++
d = &analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Message: fn + " can be replaced with " + errMethodCall,
SuggestedFixes: []analysis.SuggestedFix{
d = newAnalysisDiagnostic(
"", // TODO: precise checker
call,
fn+" can be replaced with "+errMethodCall,
[]analysis.SuggestedFix{
{
Message: "Use " + errMethodCall,
TextEdits: []analysis.TextEdit{{
Expand All @@ -215,7 +215,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
}},
},
},
}
)

case isBasicType(valueType, types.Bool) && oneOf(verb, "%v", "%t"):
fname := pass.Fset.File(call.Pos()).Name()
Expand All @@ -225,11 +225,11 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
neededPackages[fname] = make(map[string]bool)
}
neededPackages[fname]["strconv"] = true
d = &analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Message: fn + " can be replaced with faster strconv.FormatBool",
SuggestedFixes: []analysis.SuggestedFix{
d = newAnalysisDiagnostic(
"", // TODO: precise checker
call,
fn+" can be replaced with faster strconv.FormatBool",
[]analysis.SuggestedFix{
{
Message: "Use strconv.FormatBool",
TextEdits: []analysis.TextEdit{{
Expand All @@ -239,7 +239,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
}},
},
},
}
)

case isArray && isBasicType(a.Elem(), types.Uint8) && oneOf(verb, "%x"):
if _, ok := value.(*ast.Ident); !ok {
Expand All @@ -254,11 +254,11 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
neededPackages[fname] = make(map[string]bool)
}
neededPackages[fname]["encoding/hex"] = true
d = &analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Message: fn + " can be replaced with faster hex.EncodeToString",
SuggestedFixes: []analysis.SuggestedFix{
d = newAnalysisDiagnostic(
"", // TODO: precise checker
call,
fn+" can be replaced with faster hex.EncodeToString",
[]analysis.SuggestedFix{
{
Message: "Use hex.EncodeToString",
TextEdits: []analysis.TextEdit{
Expand All @@ -275,7 +275,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
},
},
},
}
)
case isSlice && isBasicType(s.Elem(), types.Uint8) && oneOf(verb, "%x"):
fname := pass.Fset.File(call.Pos()).Name()
removedFmtUsages[fname]++
Expand All @@ -284,11 +284,11 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
neededPackages[fname] = make(map[string]bool)
}
neededPackages[fname]["encoding/hex"] = true
d = &analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Message: fn + " can be replaced with faster hex.EncodeToString",
SuggestedFixes: []analysis.SuggestedFix{
d = newAnalysisDiagnostic(
"", // TODO: precise checker
call,
fn+" can be replaced with faster hex.EncodeToString",
[]analysis.SuggestedFix{
{
Message: "Use hex.EncodeToString",
TextEdits: []analysis.TextEdit{{
Expand All @@ -298,7 +298,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
}},
},
},
}
)

case isBasicType(valueType, types.Int8, types.Int16, types.Int32) && oneOf(verb, "%v", "%d") && n.intConv:
fname := pass.Fset.File(call.Pos()).Name()
Expand All @@ -308,11 +308,11 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
neededPackages[fname] = make(map[string]bool)
}
neededPackages[fname]["strconv"] = true
d = &analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Message: fn + " can be replaced with faster strconv.Itoa",
SuggestedFixes: []analysis.SuggestedFix{
d = newAnalysisDiagnostic(
"", // TODO: precise checker
call,
fn+" can be replaced with faster strconv.Itoa",
[]analysis.SuggestedFix{
{
Message: "Use strconv.Itoa",
TextEdits: []analysis.TextEdit{
Expand All @@ -329,7 +329,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
},
},
},
}
)
case isBasicType(valueType, types.Int) && oneOf(verb, "%v", "%d"):
fname := pass.Fset.File(call.Pos()).Name()
removedFmtUsages[fname]++
Expand All @@ -338,11 +338,11 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
neededPackages[fname] = make(map[string]bool)
}
neededPackages[fname]["strconv"] = true
d = &analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Message: fn + " can be replaced with faster strconv.Itoa",
SuggestedFixes: []analysis.SuggestedFix{
d = newAnalysisDiagnostic(
"", // TODO: precise checker
call,
fn+" can be replaced with faster strconv.Itoa",
[]analysis.SuggestedFix{
{
Message: "Use strconv.Itoa",
TextEdits: []analysis.TextEdit{{
Expand All @@ -352,7 +352,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
}},
},
},
}
)
case isBasicType(valueType, types.Int64) && oneOf(verb, "%v", "%d"):
fname := pass.Fset.File(call.Pos()).Name()
removedFmtUsages[fname]++
Expand All @@ -361,11 +361,11 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
neededPackages[fname] = make(map[string]bool)
}
neededPackages[fname]["strconv"] = true
d = &analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Message: fn + " can be replaced with faster strconv.FormatInt",
SuggestedFixes: []analysis.SuggestedFix{
d = newAnalysisDiagnostic(
"", // TODO: precise checker
call,
fn+" can be replaced with faster strconv.FormatInt",
[]analysis.SuggestedFix{
{
Message: "Use strconv.FormatInt",
TextEdits: []analysis.TextEdit{
Expand All @@ -382,7 +382,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
},
},
},
}
)

case isBasicType(valueType, types.Uint8, types.Uint16, types.Uint32, types.Uint) && oneOf(verb, "%v", "%d", "%x") && n.intConv:
base := []byte("), 10")
Expand All @@ -396,11 +396,11 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
neededPackages[fname] = make(map[string]bool)
}
neededPackages[fname]["strconv"] = true
d = &analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Message: fn + " can be replaced with faster strconv.FormatUint",
SuggestedFixes: []analysis.SuggestedFix{
d = newAnalysisDiagnostic(
"", // TODO: precise checker
call,
fn+" can be replaced with faster strconv.FormatUint",
[]analysis.SuggestedFix{
{
Message: "Use strconv.FormatUint",
TextEdits: []analysis.TextEdit{
Expand All @@ -417,7 +417,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
},
},
},
}
)
case isBasicType(valueType, types.Uint64) && oneOf(verb, "%v", "%d", "%x"):
base := []byte(", 10")
if verb == "%x" {
Expand All @@ -430,11 +430,11 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
neededPackages[fname] = make(map[string]bool)
}
neededPackages[fname]["strconv"] = true
d = &analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Message: fn + " can be replaced with faster strconv.FormatUint",
SuggestedFixes: []analysis.SuggestedFix{
d = newAnalysisDiagnostic(
"", // TODO: precise checker
call,
fn+" can be replaced with faster strconv.FormatUint",
[]analysis.SuggestedFix{
{
Message: "Use strconv.FormatUint",
TextEdits: []analysis.TextEdit{
Expand All @@ -451,7 +451,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
},
},
},
}
)
case isBasicType(valueType, types.String) && fn == "fmt.Sprintf" && isConcatable(verb):
var fix string
if strings.HasSuffix(verb, "%s") {
Expand All @@ -465,11 +465,11 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
}
fname := pass.Fset.File(call.Pos()).Name()
removedFmtUsages[fname]++
d = &analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Message: fn + " can be replaced with string concatenation",
SuggestedFixes: []analysis.SuggestedFix{
d = newAnalysisDiagnostic(
"", // TODO: precise checker
call,
fn+" can be replaced with string concatenation",
[]analysis.SuggestedFix{
{
Message: "Use string concatenation",
TextEdits: []analysis.TextEdit{{
Expand All @@ -479,7 +479,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
}},
},
},
}
)
}

if d != nil {
Expand Down Expand Up @@ -545,11 +545,11 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
for _, k := range keys {
fix = fix + "\n\t\"" + k + `"`
}
pass.Report(analysis.Diagnostic{
Pos: gd.Pos(),
End: gd.End(),
Message: "Fix imports",
SuggestedFixes: []analysis.SuggestedFix{
pass.Report(*newAnalysisDiagnostic(
"", // TODO: precise checker
gd,
"Fix imports",
[]analysis.SuggestedFix{
{
Message: "Fix imports",
TextEdits: []analysis.TextEdit{{
Expand All @@ -558,7 +558,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
NewText: []byte(fix),
}},
},
}})
}))
}
})
}
Expand Down
25 changes: 25 additions & 0 deletions analyzer/diagnostic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package analyzer

import (
"golang.org/x/tools/go/analysis"
)

func newAnalysisDiagnostic(
checker string,
analysisRange analysis.Range,
message string,
suggestedFixes []analysis.SuggestedFix,
) *analysis.Diagnostic {
d := analysis.Diagnostic{
Pos: analysisRange.Pos(),
End: analysisRange.End(),
SuggestedFixes: suggestedFixes,
}
if checker != "" {
d.Category = checker
d.Message = checker + ": " + message
} else {
d.Message = message
}
return &d
}

0 comments on commit a34cbe3

Please sign in to comment.