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:
parent
3dc86d87c8
commit
fa8bea3d3b
@ -52,7 +52,7 @@ var allRules = append([]lint.Rule{
|
|||||||
&rule.GetReturnRule{},
|
&rule.GetReturnRule{},
|
||||||
&rule.ModifiesParamRule{},
|
&rule.ModifiesParamRule{},
|
||||||
&rule.DeepExitRule{},
|
&rule.DeepExitRule{},
|
||||||
&rule.ADSNewErrRule{},
|
&rule.ADSLostErrRule{},
|
||||||
}, defaultRules...)
|
}, defaultRules...)
|
||||||
|
|
||||||
var allFormatters = []lint.Formatter{
|
var allFormatters = []lint.Formatter{
|
||||||
|
9
fixtures/ads-lost-err.go
Normal file
9
fixtures/ads-lost-err.go
Normal 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())
|
||||||
|
}
|
@ -1,7 +0,0 @@
|
|||||||
package fixtures
|
|
||||||
|
|
||||||
import "errors"
|
|
||||||
|
|
||||||
func foo(a, b, c, d int) {
|
|
||||||
errors.New("aaa")
|
|
||||||
}
|
|
@ -6,33 +6,33 @@ import (
|
|||||||
"github.com/mgechev/revive/lint"
|
"github.com/mgechev/revive/lint"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ADSNewErrRule lints program exit at functions other than main or init.
|
// ADSLostErrRule lints program exit at functions other than main or init.
|
||||||
type ADSNewErrRule struct{}
|
type ADSLostErrRule struct{}
|
||||||
|
|
||||||
// Apply applies the rule to given file.
|
// 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
|
var failures []lint.Failure
|
||||||
onFailure := func(failure lint.Failure) {
|
onFailure := func(failure lint.Failure) {
|
||||||
failures = append(failures, failure)
|
failures = append(failures, failure)
|
||||||
}
|
}
|
||||||
|
|
||||||
w := lintNewErr{onFailure, "errors", "New"}
|
w := lintLostErr{onFailure, "errors", "New"}
|
||||||
ast.Walk(w, file.AST)
|
ast.Walk(w, file.AST)
|
||||||
return failures
|
return failures
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name returns the rule name.
|
// Name returns the rule name.
|
||||||
func (r *ADSNewErrRule) Name() string {
|
func (r *ADSLostErrRule) Name() string {
|
||||||
return "ads-newerr"
|
return "ads-newerr"
|
||||||
}
|
}
|
||||||
|
|
||||||
type lintNewErr struct {
|
type lintLostErr struct {
|
||||||
onFailure func(lint.Failure)
|
onFailure func(lint.Failure)
|
||||||
targetPkg string
|
targetPkg string
|
||||||
targetFunc 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)
|
ce, ok := node.(*ast.CallExpr)
|
||||||
if !ok {
|
if !ok {
|
||||||
return w
|
return w
|
||||||
@ -58,7 +58,7 @@ func (w lintNewErr) Visit(node ast.Node) ast.Visitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type searchErr struct {
|
type searchErr struct {
|
||||||
w *lintNewErr
|
w *lintLostErr
|
||||||
fc *ast.CallExpr
|
fc *ast.CallExpr
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,8 +73,7 @@ func (s searchErr) Visit(node ast.Node) ast.Visitor {
|
|||||||
Confidence: 0.8,
|
Confidence: 0.8,
|
||||||
Node: s.fc,
|
Node: s.fc,
|
||||||
Category: "bad practice",
|
Category: "bad practice",
|
||||||
URL: "#ads-newerr",
|
Failure: "original error is lost, consider using errors.NewFromError",
|
||||||
Failure: "consider errors.Wrap instead of errors.New",
|
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -93,5 +92,4 @@ func getPkgFunc(ce *ast.CallExpr) (string, string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return id.Name, fc.Sel.Name
|
return id.Name, fc.Sel.Name
|
||||||
|
|
||||||
}
|
}
|
11
test/ads-lost-err_test.go
Normal file
11
test/ads-lost-err_test.go
Normal 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{})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user