Skip to content

Commit bf84681

Browse files
mateusz834adonovan
authored andcommitted
go/analysis/passes/errorsas: add example of invalid errors.As use
Change-Id: I09ea4efab1631bc46f740d2e2bb5546a6a6a6922 Reviewed-on: https://go-review.googlesource.com/c/tools/+/773920 LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
1 parent 23921d1 commit bf84681

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

go/analysis/passes/errorsas/errorsas.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// The errorsas package defines an Analyzer that checks that the second argument to
6-
// errors.As is a pointer to a type implementing error.
5+
// Package errorsas defines an Analyzer that checks that the second argument to
6+
// [errors.As] is a pointer to a type implementing error.
77
package errorsas
88

99
import (
@@ -19,7 +19,12 @@ import (
1919
const Doc = `report passing non-pointer or non-error values to errors.As
2020
2121
The errorsas analyzer reports calls to errors.As where the type
22-
of the second argument is not a pointer to a type implementing error.`
22+
of the second argument is not a pointer to a type implementing error.
23+
For example:
24+
25+
var unwrappedErr net.DNSError
26+
errors.As(err, unwrappedErr) // should use &unwrappedErr, DNSError.Error has a pointer reciever
27+
`
2328

2429
var Analyzer = &analysis.Analyzer{
2530
Name: "errorsas",

gopls/doc/analyzers.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3139,7 +3139,10 @@ Package documentation: [embedlit](https://pkg.go.dev/golang.org/x/tools/go/analy
31393139
<a id='errorsas'></a>
31403140
## `errorsas`: report passing non-pointer or non-error values to errors.As
31413141

3142-
The errorsas analyzer reports calls to errors.As where the type of the second argument is not a pointer to a type implementing error.
3142+
The errorsas analyzer reports calls to errors.As where the type of the second argument is not a pointer to a type implementing error. For example:
3143+
3144+
var unwrappedErr net.DNSError
3145+
errors.As(err, unwrappedErr) // should use &unwrappedErr, DNSError.Error has a pointer reciever
31433146

31443147

31453148
Default: on.

gopls/internal/doc/api.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,7 @@
14621462
},
14631463
{
14641464
"Name": "\"errorsas\"",
1465-
"Doc": "report passing non-pointer or non-error values to errors.As\n\nThe errorsas analyzer reports calls to errors.As where the type\nof the second argument is not a pointer to a type implementing error.",
1465+
"Doc": "report passing non-pointer or non-error values to errors.As\n\nThe errorsas analyzer reports calls to errors.As where the type\nof the second argument is not a pointer to a type implementing error.\nFor example:\n\n\tvar unwrappedErr net.DNSError\n\terrors.As(err, unwrappedErr) // should use \u0026unwrappedErr, DNSError.Error has a pointer reciever\n",
14661466
"Default": "true",
14671467
"Status": ""
14681468
},
@@ -3421,7 +3421,7 @@
34213421
},
34223422
{
34233423
"Name": "errorsas",
3424-
"Doc": "report passing non-pointer or non-error values to errors.As\n\nThe errorsas analyzer reports calls to errors.As where the type\nof the second argument is not a pointer to a type implementing error.",
3424+
"Doc": "report passing non-pointer or non-error values to errors.As\n\nThe errorsas analyzer reports calls to errors.As where the type\nof the second argument is not a pointer to a type implementing error.\nFor example:\n\n\tvar unwrappedErr net.DNSError\n\terrors.As(err, unwrappedErr) // should use \u0026unwrappedErr, DNSError.Error has a pointer reciever\n",
34253425
"URL": "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/errorsas",
34263426
"Default": true
34273427
},

0 commit comments

Comments
 (0)