errorsas: handle single-actual case

Handle the case where there is syntactically only one actual argument
passed to errors.As.

Following the unmarshal check, we ignore this case.

Change-Id: Ia7d77d5b3c9eb5416b37a141104c9ad7ed290b5f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/178159
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Jonathan Amsterdam 2019-05-20 15:54:34 -04:00
parent b1dcc6b189
commit 0abef6e9ec
2 changed files with 6 additions and 0 deletions

View File

@ -47,6 +47,9 @@ func run(pass *analysis.Pass) (interface{}, error) {
if fn == nil {
return // not a static call
}
if len(call.Args) < 2 {
return // not enough arguments, e.g. called with return values of another function
}
if fn.FullName() == "errors.As" && !pointerToInterfaceOrError(pass, call.Args[1]) {
pass.Reportf(call.Pos(), "second argument to errors.As must be a pointer to an interface or a type implementing error")
}

View File

@ -18,6 +18,8 @@ type iface interface {
m()
}
func two() (error, interface{}) { return nil, nil }
func _() {
var (
e error
@ -37,4 +39,5 @@ func _() {
errors.As(nil, m) // want `second argument to errors.As must be a pointer to an interface or a type implementing error`
errors.As(nil, f) // want `second argument to errors.As must be a pointer to an interface or a type implementing error`
errors.As(nil, &i) // want `second argument to errors.As must be a pointer to an interface or a type implementing error`
errors.As(two())
}