-
Notifications
You must be signed in to change notification settings - Fork 285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] New rule use-errors-new
(propose replacing fmt.Errorf
by errors.New
when possible)
#1136
Changes from all commits
77b1bf6
8d72c32
6f6316c
0c08023
76b64c0
60af015
182296f
8692e83
44fca17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package rule | ||
|
||
import ( | ||
"go/ast" | ||
|
||
"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 := isPkgDot(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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, not exactly We have crazy people out there fmt.Errorf("%s, "whatever") fmt.Errorf("%w", err) I created an issue on perfprint linter, as it's in their scope I think you can merge like this, with current code. My point can be addressed later There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussion moved on the PR |
||
} | ||
|
||
// the call is of the form fmt.Errorf("...") | ||
w.onFailure(lint.Failure{ | ||
Category: "errors", | ||
Node: n, | ||
Confidence: 1, | ||
Failure: "replace fmt.Errorf by errors.New", | ||
}) | ||
|
||
return w | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mgechev/revive/rule" | ||
) | ||
|
||
func TestUseErrorsNew(t *testing.T) { | ||
testRule(t, "use_errors_new", &rule.UseErrorsNewRule{}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package pkg | ||
|
||
import "fmt" | ||
|
||
func errorsNew() (int, error) { | ||
fmt.Errorf("repo cannot be nil") // MATCH /replace fmt.Errorf by errors.New/ | ||
errs := append(errs, fmt.Errorf("commit cannot be nil")) // MATCH /replace fmt.Errorf by errors.New/ | ||
fmt.Errorf("unable to load base repo: %w", err) | ||
fmt.Errorf("Failed to get full commit id for origin/%s: %w", pr.BaseBranch, err) | ||
|
||
return 0, fmt.Errorf(msg + "something") // MATCH /replace fmt.Errorf by errors.New/ | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I've said in the PR description, it is work in progress, I'll complete the PR (tests+doc) in the next days.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests and doc added