From 06b78c05fd9f227b4879fe4eaea7c44d8dfe2b82 Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Tue, 21 Nov 2023 13:02:18 +0100 Subject: [PATCH] analyzer: fix case for errorf=false --- analyzer/analyzer.go | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/analyzer/analyzer.go b/analyzer/analyzer.go index d117e82..6b52a4b 100644 --- a/analyzer/analyzer.go +++ b/analyzer/analyzer.go @@ -133,29 +133,32 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) { switch { case isBasicType(valueType, types.String) && oneOf(verb, "%v", "%s"): fname := pass.Fset.File(call.Pos()).Name() - removedFmtUsages[fname] = removedFmtUsages[fname] + 1 _, ok := neededPackages[fname] if !ok { neededPackages[fname] = make(map[string]bool) } 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{ - { - Message: "Use errors.New", - TextEdits: []analysis.TextEdit{{ - Pos: call.Pos(), - End: value.Pos(), - NewText: []byte("errors.New("), - }}, + 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("), + }}, + }, }, - }, + } } } else { + removedFmtUsages[fname] = removedFmtUsages[fname] + 1 d = &analysis.Diagnostic{ Pos: call.Pos(), End: call.End(),