diff --git a/config.go b/config.go index ad6c7ae..3bf5903 100644 --- a/config.go +++ b/config.go @@ -52,7 +52,7 @@ var allRules = append([]lint.Rule{ &rule.GetReturnRule{}, &rule.ModifiesParamRule{}, &rule.DeepExitRule{}, - &rule.ADSNewErrRule{}, + &rule.ADSLostErrRule{}, }, defaultRules...) var allFormatters = []lint.Formatter{ diff --git a/fixtures/ads-lost-err.go b/fixtures/ads-lost-err.go new file mode 100644 index 0000000..8d31192 --- /dev/null +++ b/fixtures/ads-lost-err.go @@ -0,0 +1,9 @@ +package fixtures + +import "errors" + +func foo(a, b, c, d int) { + errors.New("aaa") + errors.New(errors.InternalError, errors.MessageOption("nice error message "+err.Error())) // MATCH /original error is lost, consider using errors.NewFromError/ + errors.MessageOption("nice error message " + err.Error()) +} diff --git a/fixtures/ads-newerror.go b/fixtures/ads-newerror.go deleted file mode 100644 index b92e6d8..0000000 --- a/fixtures/ads-newerror.go +++ /dev/null @@ -1,7 +0,0 @@ -package fixtures - -import "errors" - -func foo(a, b, c, d int) { - errors.New("aaa") -} diff --git a/rule/ads-newerr.go b/rule/ads-lost-err.go similarity index 72% rename from rule/ads-newerr.go rename to rule/ads-lost-err.go index c130aa9..a8eeb9d 100644 --- a/rule/ads-newerr.go +++ b/rule/ads-lost-err.go @@ -6,33 +6,33 @@ import ( "github.com/mgechev/revive/lint" ) -// ADSNewErrRule lints program exit at functions other than main or init. -type ADSNewErrRule struct{} +// ADSLostErrRule lints program exit at functions other than main or init. +type ADSLostErrRule struct{} // Apply applies the rule to given file. -func (r *ADSNewErrRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { +func (r *ADSLostErrRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { var failures []lint.Failure onFailure := func(failure lint.Failure) { failures = append(failures, failure) } - w := lintNewErr{onFailure, "errors", "New"} + w := lintLostErr{onFailure, "errors", "New"} ast.Walk(w, file.AST) return failures } // Name returns the rule name. -func (r *ADSNewErrRule) Name() string { +func (r *ADSLostErrRule) Name() string { return "ads-newerr" } -type lintNewErr struct { +type lintLostErr struct { onFailure func(lint.Failure) targetPkg string targetFunc string } -func (w lintNewErr) Visit(node ast.Node) ast.Visitor { +func (w lintLostErr) Visit(node ast.Node) ast.Visitor { ce, ok := node.(*ast.CallExpr) if !ok { return w @@ -58,7 +58,7 @@ func (w lintNewErr) Visit(node ast.Node) ast.Visitor { } type searchErr struct { - w *lintNewErr + w *lintLostErr fc *ast.CallExpr } @@ -73,8 +73,7 @@ func (s searchErr) Visit(node ast.Node) ast.Visitor { Confidence: 0.8, Node: s.fc, Category: "bad practice", - URL: "#ads-newerr", - Failure: "consider errors.Wrap instead of errors.New", + Failure: "original error is lost, consider using errors.NewFromError", }) } @@ -93,5 +92,4 @@ func getPkgFunc(ce *ast.CallExpr) (string, string) { } return id.Name, fc.Sel.Name - } diff --git a/test/ads-lost-err_test.go b/test/ads-lost-err_test.go new file mode 100644 index 0000000..47cf6c1 --- /dev/null +++ b/test/ads-lost-err_test.go @@ -0,0 +1,11 @@ +package test + +import ( + "testing" + + "github.com/mgechev/revive/rule" +) + +func TestADSLostErr(t *testing.T) { + testRule(t, "ads-lost-err", &rule.ADSLostErrRule{}) +}