Skip to content

Commit 0cfe8ce

Browse files
psydvlpolyfloyd
authored andcommitted
feat: extends Analyzer with custom allowed errors via functional options
1 parent d1fbedb commit 0cfe8ce

File tree

7 files changed

+110
-9
lines changed

7 files changed

+110
-9
lines changed

errorlint/allowed.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
"strings"
88
)
99

10-
var allowedErrors = []struct {
10+
type AllowPair struct {
1111
err string
1212
fun string
13-
}{
13+
}
14+
15+
var allowedErrors = []AllowPair{
1416
// pkg/archive/tar
1517
{err: "io.EOF", fun: "(*archive/tar.Reader).Next"},
1618
{err: "io.EOF", fun: "(*archive/tar.Reader).Read"},
@@ -87,25 +89,30 @@ var allowedErrors = []struct {
8789

8890
var allowedErrorsMap = make(map[string]map[string]struct{})
8991

90-
func init() {
91-
for _, pair := range allowedErrors {
92+
func allowedMapAppend(ap []AllowPair) {
93+
for _, pair := range ap {
9294
if _, ok := allowedErrorsMap[pair.err]; !ok {
9395
allowedErrorsMap[pair.err] = make(map[string]struct{})
9496
}
9597
allowedErrorsMap[pair.err][pair.fun] = struct{}{}
9698
}
9799
}
98100

99-
var allowedErrorWildcards = []struct {
100-
err string
101-
fun string
102-
}{
101+
func init() {
102+
allowedMapAppend(allowedErrors)
103+
}
104+
105+
var allowedErrorWildcards = []AllowPair{
103106
// pkg/syscall
104107
{err: "syscall.E", fun: "syscall."},
105108
// golang.org/x/sys/unix
106109
{err: "golang.org/x/sys/unix.E", fun: "golang.org/x/sys/unix."},
107110
}
108111

112+
func allowedWildcardAppend(ap []AllowPair) {
113+
allowedErrorWildcards = append(allowedErrorWildcards, ap...)
114+
}
115+
109116
func isAllowedErrAndFunc(err, fun string) bool {
110117
if allowedFuncs, allowErr := allowedErrorsMap[err]; allowErr {
111118
if _, allow := allowedFuncs[fun]; allow {

errorlint/analysis.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import (
99
"golang.org/x/tools/go/analysis"
1010
)
1111

12-
func NewAnalyzer() *analysis.Analyzer {
12+
func NewAnalyzer(opts ...Option) *analysis.Analyzer {
13+
for _, o := range opts {
14+
o()
15+
}
1316
return &analysis.Analyzer{
1417
Name: "errorlint",
1518
Doc: "Source code linter for Go software that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13.",

errorlint/analysis_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,35 @@ func TestAllowedComparisons(t *testing.T) {
3939
analysistest.Run(t, analysistest.TestData(), analyzer, "allowed")
4040
}
4141

42+
func TestOptions(t *testing.T) {
43+
testCases := []struct {
44+
desc string
45+
opt Option
46+
pattern string
47+
}{
48+
{
49+
desc: "WithAllowedErrors",
50+
opt: WithAllowedErrors([]AllowPair{
51+
{err: "io.EOF", fun: "example.com/pkg.Read"},
52+
}),
53+
pattern: "options/withAllowedErrors",
54+
},
55+
{
56+
desc: "WithAllowedWildcard",
57+
opt: WithAllowedWildcard([]AllowPair{
58+
{err: "example.com/pkg.ErrMagic", fun: "example.com/pkg.Magic"},
59+
}),
60+
pattern: "options/withAllowedWildcard",
61+
},
62+
}
63+
for _, tt := range testCases {
64+
t.Run(tt.desc, func(t *testing.T) {
65+
analyzer := NewAnalyzer(tt.opt)
66+
analysistest.Run(t, analysistest.TestData(), analyzer, tt.pattern)
67+
})
68+
}
69+
}
70+
4271
func TestIssueRegressions(t *testing.T) {
4372
analyzer := NewAnalyzer()
4473
analysistest.Run(t, analysistest.TestData(), analyzer, "issues")

errorlint/options.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package errorlint
2+
3+
type Option func()
4+
5+
func WithAllowedErrors(ap []AllowPair) Option {
6+
return func() {
7+
allowedMapAppend(ap)
8+
}
9+
}
10+
11+
func WithAllowedWildcard(ap []AllowPair) Option {
12+
return func() {
13+
allowedWildcardAppend(ap)
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package pkg
2+
3+
import (
4+
"errors"
5+
"io"
6+
)
7+
8+
func Read(io.Reader) error {
9+
return io.EOF
10+
}
11+
12+
var (
13+
ErrMagicOne = errors.New("magic")
14+
)
15+
16+
func MagicOne() error {
17+
return ErrMagicOne
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package testdata
2+
3+
import (
4+
"fmt"
5+
"io"
6+
7+
"example.com/pkg"
8+
)
9+
10+
func CustomPackageCompare(r io.Reader) {
11+
err := pkg.Read(r)
12+
if err == io.EOF {
13+
fmt.Println(err)
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package testdata
2+
3+
import (
4+
"fmt"
5+
6+
"example.com/pkg"
7+
)
8+
9+
func Magic() {
10+
err := pkg.MagicOne()
11+
if err != pkg.ErrMagicOne {
12+
fmt.Println(err)
13+
}
14+
}

0 commit comments

Comments
 (0)