1
0
mirror of https://github.com/mgechev/revive.git synced 2025-07-15 01:04:40 +02:00
Files
revive/rule/use_errors_new.go
chavacava 92f28cb5e1 refactor: moves code related to AST from rule.utils into astutils package (#1380)
Modifications summary:

* Moves AST-related functions from rule/utils.go to astutils/ast_utils.go (+ modifies function calls)
* Renames some of these AST-related functions
* Avoids instantiating a printer config at each call to astutils.GoFmt
* Uses astutils.IsIdent and astutils.IsPkgDotName when possible
2025-05-26 13:18:38 +02:00

62 lines
1.2 KiB
Go

package rule
import (
"go/ast"
"github.com/mgechev/revive/internal/astutils"
"github.com/mgechev/revive/lint"
)
// UseErrorsNewRule spots calls to fmt.Errorf that can be replaced by errors.New.
type UseErrorsNewRule struct{}
// Apply applies the rule to given file.
func (*UseErrorsNewRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure
walker := lintFmtErrorf{
onFailure: func(failure lint.Failure) {
failures = append(failures, failure)
},
}
ast.Walk(walker, file.AST)
return failures
}
// Name returns the rule name.
func (*UseErrorsNewRule) Name() string {
return "use-errors-new"
}
type lintFmtErrorf struct {
onFailure func(lint.Failure)
}
func (w lintFmtErrorf) Visit(n ast.Node) ast.Visitor {
funcCall, ok := n.(*ast.CallExpr)
if !ok {
return w // not a function call
}
isFmtErrorf := astutils.IsPkgDotName(funcCall.Fun, "fmt", "Errorf")
if !isFmtErrorf {
return w // not a call to fmt.Errorf
}
if len(funcCall.Args) > 1 {
return w // the use of fmt.Errorf is legit
}
// the call is of the form fmt.Errorf("...")
w.onFailure(lint.Failure{
Category: lint.FailureCategoryErrors,
Node: n,
Confidence: 1,
Failure: "replace fmt.Errorf by errors.New",
})
return w
}