mirror of
https://github.com/mgechev/revive.git
synced 2025-01-22 03:38:47 +02:00
adds blacklist to unhandled-error (#128)
* adds blacklist for unhandled-error * uses ignoreList in place of blackList
This commit is contained in:
parent
dbcb21608a
commit
c8ee35a500
@ -301,7 +301,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
|
||||
| [`import-shadowing`](./RULES_DESCRIPTIONS.md#import-shadowing) | n/a | Spots identifiers that shadow an import | no | no |
|
||||
| [`bare-return`](./RULES_DESCRIPTIONS#bare-return) | n/a | Warns on bare returns | no | no |
|
||||
| [`unused-receiver`](./RULES_DESCRIPTIONS.md#unused-receiver) | n/a | Suggests to rename or remove unused method receivers | no | no |
|
||||
| [`unhandled-error`](./RULES_DESCRIPTIONS.md#unhandled-error) | n/a | Warns on unhandled errors returned by funcion calls | no | yes |
|
||||
| [`unhandled-error`](./RULES_DESCRIPTIONS.md#unhandled-error) | []string | Warns on unhandled errors returned by funcion calls | no | yes |
|
||||
|
||||
## Configurable rules
|
||||
|
||||
|
@ -439,8 +439,14 @@ _Configuration_: N/A
|
||||
|
||||
_Description_: This rule warns when errors returned by a function are not explicitly handled on the caller side.
|
||||
|
||||
_Configuration_: N/A
|
||||
_Configuration_: function names to ignore
|
||||
|
||||
Example:
|
||||
|
||||
```toml
|
||||
[unhandled-error]
|
||||
arguments =["fmt.Printf", "myFunction"]
|
||||
```
|
||||
## unnecessary-stmt
|
||||
|
||||
_Description_: This rule suggests to remove redundant statements like a `break` at the end of a case block, for improving the code's readability.
|
||||
|
19
fixtures/unhandled-error-w-ignorelist.go
Normal file
19
fixtures/unhandled-error-w-ignorelist.go
Normal file
@ -0,0 +1,19 @@
|
||||
package fixtures
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func unhandledError1(a int) (int, error) {
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func unhandledError2() error {
|
||||
_, err := unhandledError1(1)
|
||||
unhandledError1(1)
|
||||
fmt.Fprintf(nil, "") // MATCH /Unhandled error in call to function fmt.Fprintf/
|
||||
os.Chdir("..")
|
||||
_ = os.Chdir("..")
|
||||
return err
|
||||
}
|
@ -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),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -3,9 +3,16 @@ package test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mgechev/revive/lint"
|
||||
"github.com/mgechev/revive/rule"
|
||||
)
|
||||
|
||||
func TestUnhandledError(t *testing.T) {
|
||||
testRule(t, "unhandled-error", &rule.UnhandledErrorRule{})
|
||||
}
|
||||
|
||||
func TestUnhandledErrorWithBlacklist(t *testing.T) {
|
||||
args := []interface{}{"os.Chdir", "unhandledError1"}
|
||||
|
||||
testRule(t, "unhandled-error-w-ignorelist", &rule.UnhandledErrorRule{}, &lint.RuleConfig{Arguments: args})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user