errorsas: ignore empty interface target
No longer report a problem if target's type is interface{}. This avoids false positives like ``` var e error var i interface{} = &e ... errors.As(..., i) ... ``` Change-Id: Ibf6e7163147248305130a5e650f92b80e34a44de Reviewed-on: https://go-review.googlesource.com/c/tools/+/175717 Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org> Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
parent
26e35f15ed
commit
b1dcc6b189
|
@ -56,9 +56,13 @@ func run(pass *analysis.Pass) (interface{}, error) {
|
||||||
|
|
||||||
var errorType = types.Universe.Lookup("error").Type().Underlying().(*types.Interface)
|
var errorType = types.Universe.Lookup("error").Type().Underlying().(*types.Interface)
|
||||||
|
|
||||||
// pointerToInterfaceOrError reports whether the type of e is a pointer to an interface or a type implementing error.
|
// pointerToInterfaceOrError reports whether the type of e is a pointer to an interface or a type implementing error,
|
||||||
|
// or is the empty interface.
|
||||||
func pointerToInterfaceOrError(pass *analysis.Pass, e ast.Expr) bool {
|
func pointerToInterfaceOrError(pass *analysis.Pass, e ast.Expr) bool {
|
||||||
t := pass.TypesInfo.Types[e].Type
|
t := pass.TypesInfo.Types[e].Type
|
||||||
|
if it, ok := t.Underlying().(*types.Interface); ok && it.NumMethods() == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
pt, ok := t.Underlying().(*types.Pointer)
|
pt, ok := t.Underlying().(*types.Pointer)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
|
|
|
@ -20,15 +20,17 @@ type iface interface {
|
||||||
|
|
||||||
func _() {
|
func _() {
|
||||||
var (
|
var (
|
||||||
e error
|
e error
|
||||||
m myError
|
m myError
|
||||||
i int
|
i int
|
||||||
f iface
|
f iface
|
||||||
|
ei interface{}
|
||||||
)
|
)
|
||||||
errors.As(nil, &e)
|
errors.As(nil, &e) // *error
|
||||||
errors.As(nil, &m)
|
errors.As(nil, &m) // *T where T implemements error
|
||||||
errors.As(nil, &f)
|
errors.As(nil, &f) // *interface
|
||||||
errors.As(nil, perr())
|
errors.As(nil, perr()) // *error, via a call
|
||||||
|
errors.As(nil, ei) // empty interface
|
||||||
|
|
||||||
errors.As(nil, nil) // want `second argument to errors.As must be a pointer to an interface or a type implementing error`
|
errors.As(nil, nil) // want `second argument to errors.As must be a pointer to an interface or a type implementing error`
|
||||||
errors.As(nil, e) // want `second argument to errors.As must be a pointer to an interface or a type implementing error`
|
errors.As(nil, e) // want `second argument to errors.As must be a pointer to an interface or a type implementing error`
|
||||||
|
|
Loading…
Reference in New Issue