Skip to content

Commit

Permalink
analyzer: bail out early if errorf is false
Browse files Browse the repository at this point in the history
  • Loading branch information
catenacyber committed Nov 29, 2023
1 parent 7c6a551 commit d3ce5b2
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
)
switch {
case calledObj == fmtErrorfObj && len(call.Args) == 1:
fn = "fmt.Errorf"
verb = "%s"
value = call.Args[0]
if n.errorf {
fn = "fmt.Errorf"
verb = "%s"
value = call.Args[0]
} else {
return
}

case calledObj == fmtSprintObj && len(call.Args) == 1:
fn = "fmt.Sprint"
Expand Down Expand Up @@ -139,28 +143,25 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
if !ok {
neededPackages[fname] = make(map[string]bool)
}
removedFmtUsages[fname] = removedFmtUsages[fname] + 1
if fn == "fmt.Errorf" {
if n.errorf {
removedFmtUsages[fname] = removedFmtUsages[fname] + 1
neededPackages[fname]["errors"] = true
d = &analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Message: fn + " can be replaced with errors.New",
SuggestedFixes: []analysis.SuggestedFix{
{
Message: "Use errors.New",
TextEdits: []analysis.TextEdit{{
Pos: call.Pos(),
End: value.Pos(),
NewText: []byte("errors.New("),
}},
},
neededPackages[fname]["errors"] = true
d = &analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Message: fn + " can be replaced with errors.New",
SuggestedFixes: []analysis.SuggestedFix{
{
Message: "Use errors.New",
TextEdits: []analysis.TextEdit{{
Pos: call.Pos(),
End: value.Pos(),
NewText: []byte("errors.New("),
}},
},
}
},
}
} else {
removedFmtUsages[fname] = removedFmtUsages[fname] + 1
d = &analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Expand Down

0 comments on commit d3ce5b2

Please sign in to comment.