1
0
mirror of https://github.com/mgechev/revive.git synced 2025-07-15 01:04:40 +02:00

adds blacklist to unhandled-error (#128)

* adds blacklist for unhandled-error

* uses ignoreList in place of blackList
This commit is contained in:
SalvadorC
2019-04-30 04:56:12 +02:00
committed by Minko Gechev
parent dbcb21608a
commit c8ee35a500
5 changed files with 59 additions and 7 deletions

View File

@ -11,12 +11,26 @@ import (
// UnhandledErrorRule lints given else constructs.
type UnhandledErrorRule struct{}
type ignoreListType map[string]struct{}
// Apply applies the rule to given file.
func (r *UnhandledErrorRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
func (r *UnhandledErrorRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure {
var failures []lint.Failure
ignoreList := make(ignoreListType, len(args))
for _, arg := range args {
argStr, ok := arg.(string)
if !ok {
panic(fmt.Sprintf("Invalid argument to the unhandled-error rule. Expecting a string, got %T", arg))
}
ignoreList[argStr] = struct{}{}
}
walker := &lintUnhandledErrors{
pkg: file.Pkg,
ignoreList: ignoreList,
pkg: file.Pkg,
onFailure: func(failure lint.Failure) {
failures = append(failures, failure)
},
@ -34,8 +48,9 @@ func (r *UnhandledErrorRule) Name() string {
}
type lintUnhandledErrors struct {
pkg *lint.Package
onFailure func(lint.Failure)
ignoreList ignoreListType
pkg *lint.Package
onFailure func(lint.Failure)
}
// Visit looks for statements that are function calls.
@ -75,11 +90,16 @@ func (w *lintUnhandledErrors) Visit(node ast.Node) ast.Visitor {
}
func (w *lintUnhandledErrors) addFailure(n *ast.CallExpr) {
funcName := gofmt(n.Fun)
if _, mustIgnore := w.ignoreList[funcName]; mustIgnore {
return
}
w.onFailure(lint.Failure{
Category: "bad practice",
Confidence: 1,
Node: n,
Failure: fmt.Sprintf("Unhandled error in call to function %v", gofmt(n.Fun)),
Failure: fmt.Sprintf("Unhandled error in call to function %v", funcName),
})
}