From 0abef6e9ecb84abbb13f782a96b8ce932107c545 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Mon, 20 May 2019 15:54:34 -0400 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Michael Matloob --- go/analysis/passes/errorsas/errorsas.go | 3 +++ go/analysis/passes/errorsas/testdata/src/a/a.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/go/analysis/passes/errorsas/errorsas.go b/go/analysis/passes/errorsas/errorsas.go index 337a61d8..c411466c 100644 --- a/go/analysis/passes/errorsas/errorsas.go +++ b/go/analysis/passes/errorsas/errorsas.go @@ -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") } diff --git a/go/analysis/passes/errorsas/testdata/src/a/a.go b/go/analysis/passes/errorsas/testdata/src/a/a.go index bebf2679..d13dee26 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 @@ 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()) }