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>
diff --git a/go/analysis/passes/errorsas/errorsas.go b/go/analysis/passes/errorsas/errorsas.go
index 337a61d..c411466 100644
--- a/go/analysis/passes/errorsas/errorsas.go
+++ b/go/analysis/passes/errorsas/errorsas.go
@@ -47,6 +47,9 @@
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")
}
diff --git a/go/analysis/passes/errorsas/testdata/src/a/a.go b/go/analysis/passes/errorsas/testdata/src/a/a.go
index bebf267..d13dee2 100644
--- a/go/analysis/passes/errorsas/testdata/src/a/a.go
+++ b/go/analysis/passes/errorsas/testdata/src/a/a.go
@@ -18,6 +18,8 @@
m()
}
+func two() (error, interface{}) { return nil, nil }
+
func _() {
var (
e error
@@ -37,4 +39,5 @@
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())
}