1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-28 08:49:11 +02:00

ads-lost-err working version

This commit is contained in:
chavacava 2018-06-29 13:41:18 +02:00
parent 3dc86d87c8
commit fa8bea3d3b
5 changed files with 30 additions and 19 deletions

View File

@ -52,7 +52,7 @@ var allRules = append([]lint.Rule{
&rule.GetReturnRule{},
&rule.ModifiesParamRule{},
&rule.DeepExitRule{},
&rule.ADSNewErrRule{},
&rule.ADSLostErrRule{},
}, defaultRules...)
var allFormatters = []lint.Formatter{

9
fixtures/ads-lost-err.go Normal file
View File

@ -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())
}

View File

@ -1,7 +0,0 @@
package fixtures
import "errors"
func foo(a, b, c, d int) {
errors.New("aaa")
}

View File

@ -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
}

11
test/ads-lost-err_test.go Normal file
View File

@ -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{})
}