Skip to content

Commit

Permalink
Update analyzer.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mmorel-35 committed Nov 20, 2024
1 parent 9154da9 commit 07192e5
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ import (
"golang.org/x/tools/go/analysis"
)

const (
checkerIntFormat string = "int-format"
checkerErrError string = "err-error"
checkerErrorf string = "errorf"
checkerSprintf1 string = "sprintf1"
checkerFiximports string = "fiximports"
checkerStrconcat string = "strconcat"
)

type perfSprint struct {
intConv bool
errError bool
Expand Down Expand Up @@ -54,11 +45,11 @@ func New() *analysis.Analyzer {
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
r.Flags.BoolVar(&n.intConv, "int-conversion", true, "optimizes even if it requires an int or uint type cast")
r.Flags.BoolVar(&n.errError, checkerErrError, false, "optimizes into err.Error() even if it is only equivalent for non-nil errors")
r.Flags.BoolVar(&n.errorf, checkerErrorf, true, "optimizes fmt.Errorf")
r.Flags.BoolVar(&n.sprintf1, checkerSprintf1, true, "optimizes fmt.Sprintf with only one argument")
r.Flags.BoolVar(&n.fiximports, checkerFiximports, true, "fix needed imports from other fixes")
r.Flags.BoolVar(&n.strconcat, checkerStrconcat, true, "optimizes into strings concatenation")
r.Flags.BoolVar(&n.errError, "err-error", false, "optimizes into err.Error() even if it is only equivalent for non-nil errors")
r.Flags.BoolVar(&n.errorf, "errorf", true, "optimizes fmt.Errorf")
r.Flags.BoolVar(&n.sprintf1, "sprintf1", true, "optimizes fmt.Sprintf with only one argument")
r.Flags.BoolVar(&n.fiximports, "fiximports", true, "fix needed imports from other fixes")
r.Flags.BoolVar(&n.strconcat, "strconcat", true, "optimizes into strings concatenation")
return r
}

Expand Down Expand Up @@ -181,7 +172,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
if fn == "fmt.Errorf" {
neededPackages[fname]["errors"] = true
d = newAnalysisDiagnostic(
checkerErrorf,
"", // TODO: precise checker
call,
fn+" can be replaced with errors.New",
[]analysis.SuggestedFix{
Expand Down Expand Up @@ -219,7 +210,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
fname := pass.Fset.File(call.Pos()).Name()
removedFmtUsages[fname]++
d = newAnalysisDiagnostic(
checkerErrError,
"", // TODO: precise checker
call,
fn+" can be replaced with "+errMethodCall,
[]analysis.SuggestedFix{
Expand Down Expand Up @@ -326,7 +317,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
}
neededPackages[fname]["strconv"] = true
d = newAnalysisDiagnostic(
checkerIntFormat,
"", // TODO: precise checker
call,
fn+" can be replaced with faster strconv.Itoa",
[]analysis.SuggestedFix{
Expand Down Expand Up @@ -414,7 +405,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
}
neededPackages[fname]["strconv"] = true
d = newAnalysisDiagnostic(
checkerIntFormat,
"", // TODO: precise checker
call,
fn+" can be replaced with faster strconv.FormatUint",
[]analysis.SuggestedFix{
Expand Down Expand Up @@ -483,7 +474,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
fname := pass.Fset.File(call.Pos()).Name()
removedFmtUsages[fname]++
d = newAnalysisDiagnostic(
checkerStrconcat,
"", // TODO: precise checker
call,
fn+" can be replaced with string concatenation",
[]analysis.SuggestedFix{
Expand Down Expand Up @@ -563,7 +554,7 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
fix = fix + "\n\t\"" + k + `"`
}
pass.Report(*newAnalysisDiagnostic(
checkerFiximports,
"", // TODO: precise checker
gd,
"Fix imports",
[]analysis.SuggestedFix{
Expand Down

0 comments on commit 07192e5

Please sign in to comment.