1
0
mirror of https://github.com/mgechev/revive.git synced 2025-07-05 00:28:53 +02:00

fix #754 [rule.unhandled-error] change arguments to regexp (#757)

* fix #754 [rule.unhandled-error] change arguments to regexp

* Update unhandled-error-w-ignorelist.go

* Update unhandled-error_test.go

* Update unhandled-error-w-ignorelist.go

* adds config backward compatibility test

* fix #754 [rule.unhandled-error] change arguments to regexp

* fix #754 [rule.unhandled-error] change arguments to regexp

* fix #754 [rule.unhandled-error] change arguments to regexp

Co-authored-by: chavacava <salvadorcavadini+github@gmail.com>
This commit is contained in:
Buyanov Vladimir
2022-11-09 15:13:52 +03:00
committed by GitHub
parent 5f26378cc2
commit 7b1458a9cb
4 changed files with 142 additions and 18 deletions

View File

@ -4,6 +4,8 @@ import (
"fmt"
"go/ast"
"go/types"
"regexp"
"strings"
"sync"
"github.com/mgechev/revive/lint"
@ -11,24 +13,30 @@ import (
// UnhandledErrorRule lints given else constructs.
type UnhandledErrorRule struct {
ignoreList ignoreListType
ignoreList []*regexp.Regexp
sync.Mutex
}
type ignoreListType map[string]struct{}
func (r *UnhandledErrorRule) configure(arguments lint.Arguments) {
r.Lock()
if r.ignoreList == nil {
r.ignoreList = make(ignoreListType, len(arguments))
for _, arg := range arguments {
argStr, ok := arg.(string)
if !ok {
panic(fmt.Sprintf("Invalid argument to the unhandled-error rule. Expecting a string, got %T", arg))
}
r.ignoreList[argStr] = struct{}{}
argStr = strings.Trim(argStr, " ")
if argStr == "" {
panic("Invalid argument to the unhandled-error rule, expected regular expression must not be empty.")
}
exp, err := regexp.Compile(argStr)
if err != nil {
panic(fmt.Sprintf("Invalid argument to the unhandled-error rule: regexp %q does not compile: %v", argStr, err))
}
r.ignoreList = append(r.ignoreList, exp)
}
}
r.Unlock()
@ -60,7 +68,7 @@ func (*UnhandledErrorRule) Name() string {
}
type lintUnhandledErrors struct {
ignoreList ignoreListType
ignoreList []*regexp.Regexp
pkg *lint.Package
onFailure func(lint.Failure)
}
@ -102,8 +110,8 @@ 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 {
name := w.funcName(n)
if w.isIgnoredFunc(name) {
return
}
@ -111,10 +119,34 @@ func (w *lintUnhandledErrors) addFailure(n *ast.CallExpr) {
Category: "bad practice",
Confidence: 1,
Node: n,
Failure: fmt.Sprintf("Unhandled error in call to function %v", funcName),
Failure: fmt.Sprintf("Unhandled error in call to function %v", gofmt(n.Fun)),
})
}
func (w *lintUnhandledErrors) funcName(call *ast.CallExpr) string {
fn, ok := w.getFunc(call)
if !ok {
return gofmt(call.Fun)
}
name := fn.FullName()
name = strings.Replace(name, "(", "", -1)
name = strings.Replace(name, ")", "", -1)
name = strings.Replace(name, "*", "", -1)
return name
}
func (w *lintUnhandledErrors) isIgnoredFunc(funcName string) bool {
for _, pattern := range w.ignoreList {
if len(pattern.FindString(funcName)) == len(funcName) {
return true
}
}
return false
}
func (*lintUnhandledErrors) isTypeError(t *types.Named) bool {
const errorTypeName = "_.error"
@ -130,3 +162,17 @@ func (w *lintUnhandledErrors) returnsAnError(tt *types.Tuple) bool {
}
return false
}
func (w *lintUnhandledErrors) getFunc(call *ast.CallExpr) (*types.Func, bool) {
sel, ok := call.Fun.(*ast.SelectorExpr)
if !ok {
return nil, false
}
fn, ok := w.pkg.TypesInfo().ObjectOf(sel.Sel).(*types.Func)
if !ok {
return nil, false
}
return fn, true
}