Skip to content

Commit

Permalink
fix: exposed AllowPair fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed May 2, 2024
1 parent fb8ec1e commit edc3520
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 96 deletions.
134 changes: 67 additions & 67 deletions errorlint/allowed.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,101 +8,101 @@ import (
)

type AllowPair struct {
err string
fun string
Err string
Fun string
}

var allowedErrors = []AllowPair{
// pkg/archive/tar
{err: "io.EOF", fun: "(*archive/tar.Reader).Next"},
{err: "io.EOF", fun: "(*archive/tar.Reader).Read"},
{Err: "io.EOF", Fun: "(*archive/tar.Reader).Next"},
{Err: "io.EOF", Fun: "(*archive/tar.Reader).Read"},
// pkg/bufio
{err: "io.EOF", fun: "(*bufio.Reader).Discard"},
{err: "io.EOF", fun: "(*bufio.Reader).Peek"},
{err: "io.EOF", fun: "(*bufio.Reader).Read"},
{err: "io.EOF", fun: "(*bufio.Reader).ReadByte"},
{err: "io.EOF", fun: "(*bufio.Reader).ReadBytes"},
{err: "io.EOF", fun: "(*bufio.Reader).ReadLine"},
{err: "io.EOF", fun: "(*bufio.Reader).ReadSlice"},
{err: "io.EOF", fun: "(*bufio.Reader).ReadString"},
{err: "io.EOF", fun: "(*bufio.Scanner).Scan"},
{Err: "io.EOF", Fun: "(*bufio.Reader).Discard"},
{Err: "io.EOF", Fun: "(*bufio.Reader).Peek"},
{Err: "io.EOF", Fun: "(*bufio.Reader).Read"},
{Err: "io.EOF", Fun: "(*bufio.Reader).ReadByte"},
{Err: "io.EOF", Fun: "(*bufio.Reader).ReadBytes"},
{Err: "io.EOF", Fun: "(*bufio.Reader).ReadLine"},
{Err: "io.EOF", Fun: "(*bufio.Reader).ReadSlice"},
{Err: "io.EOF", Fun: "(*bufio.Reader).ReadString"},
{Err: "io.EOF", Fun: "(*bufio.Scanner).Scan"},
// pkg/bytes
{err: "io.EOF", fun: "(*bytes.Buffer).Read"},
{err: "io.EOF", fun: "(*bytes.Buffer).ReadByte"},
{err: "io.EOF", fun: "(*bytes.Buffer).ReadBytes"},
{err: "io.EOF", fun: "(*bytes.Buffer).ReadRune"},
{err: "io.EOF", fun: "(*bytes.Buffer).ReadString"},
{err: "io.EOF", fun: "(*bytes.Reader).Read"},
{err: "io.EOF", fun: "(*bytes.Reader).ReadAt"},
{err: "io.EOF", fun: "(*bytes.Reader).ReadByte"},
{err: "io.EOF", fun: "(*bytes.Reader).ReadRune"},
{err: "io.EOF", fun: "(*bytes.Reader).ReadString"},
{Err: "io.EOF", Fun: "(*bytes.Buffer).Read"},
{Err: "io.EOF", Fun: "(*bytes.Buffer).ReadByte"},
{Err: "io.EOF", Fun: "(*bytes.Buffer).ReadBytes"},
{Err: "io.EOF", Fun: "(*bytes.Buffer).ReadRune"},
{Err: "io.EOF", Fun: "(*bytes.Buffer).ReadString"},
{Err: "io.EOF", Fun: "(*bytes.Reader).Read"},
{Err: "io.EOF", Fun: "(*bytes.Reader).ReadAt"},
{Err: "io.EOF", Fun: "(*bytes.Reader).ReadByte"},
{Err: "io.EOF", Fun: "(*bytes.Reader).ReadRune"},
{Err: "io.EOF", Fun: "(*bytes.Reader).ReadString"},
// pkg/database/sql
{err: "database/sql.ErrNoRows", fun: "(*database/sql.Row).Scan"},
{Err: "database/sql.ErrNoRows", Fun: "(*database/sql.Row).Scan"},
// pkg/debug/elf
{err: "io.EOF", fun: "debug/elf.Open"},
{err: "io.EOF", fun: "debug/elf.NewFile"},
{Err: "io.EOF", Fun: "debug/elf.Open"},
{Err: "io.EOF", Fun: "debug/elf.NewFile"},
// pkg/io
{err: "io.EOF", fun: "(io.ReadCloser).Read"},
{err: "io.EOF", fun: "(io.Reader).Read"},
{err: "io.EOF", fun: "(io.ReaderAt).ReadAt"},
{err: "io.EOF", fun: "(*io.LimitedReader).Read"},
{err: "io.EOF", fun: "(*io.SectionReader).Read"},
{err: "io.EOF", fun: "(*io.SectionReader).ReadAt"},
{err: "io.ErrClosedPipe", fun: "(*io.PipeWriter).Write"},
{err: "io.ErrShortBuffer", fun: "io.ReadAtLeast"},
{err: "io.ErrUnexpectedEOF", fun: "io.ReadAtLeast"},
{err: "io.EOF", fun: "io.ReadFull"},
{err: "io.ErrUnexpectedEOF", fun: "io.ReadFull"},
{Err: "io.EOF", Fun: "(io.ReadCloser).Read"},
{Err: "io.EOF", Fun: "(io.Reader).Read"},
{Err: "io.EOF", Fun: "(io.ReaderAt).ReadAt"},
{Err: "io.EOF", Fun: "(*io.LimitedReader).Read"},
{Err: "io.EOF", Fun: "(*io.SectionReader).Read"},
{Err: "io.EOF", Fun: "(*io.SectionReader).ReadAt"},
{Err: "io.ErrClosedPipe", Fun: "(*io.PipeWriter).Write"},
{Err: "io.ErrShortBuffer", Fun: "io.ReadAtLeast"},
{Err: "io.ErrUnexpectedEOF", Fun: "io.ReadAtLeast"},
{Err: "io.EOF", Fun: "io.ReadFull"},
{Err: "io.ErrUnexpectedEOF", Fun: "io.ReadFull"},
// pkg/net/http
{err: "net/http.ErrServerClosed", fun: "(*net/http.Server).ListenAndServe"},
{err: "net/http.ErrServerClosed", fun: "(*net/http.Server).ListenAndServeTLS"},
{err: "net/http.ErrServerClosed", fun: "(*net/http.Server).Serve"},
{err: "net/http.ErrServerClosed", fun: "(*net/http.Server).ServeTLS"},
{err: "net/http.ErrServerClosed", fun: "net/http.ListenAndServe"},
{err: "net/http.ErrServerClosed", fun: "net/http.ListenAndServeTLS"},
{err: "net/http.ErrServerClosed", fun: "net/http.Serve"},
{err: "net/http.ErrServerClosed", fun: "net/http.ServeTLS"},
{Err: "net/http.ErrServerClosed", Fun: "(*net/http.Server).ListenAndServe"},
{Err: "net/http.ErrServerClosed", Fun: "(*net/http.Server).ListenAndServeTLS"},
{Err: "net/http.ErrServerClosed", Fun: "(*net/http.Server).Serve"},
{Err: "net/http.ErrServerClosed", Fun: "(*net/http.Server).ServeTLS"},
{Err: "net/http.ErrServerClosed", Fun: "net/http.ListenAndServe"},
{Err: "net/http.ErrServerClosed", Fun: "net/http.ListenAndServeTLS"},
{Err: "net/http.ErrServerClosed", Fun: "net/http.Serve"},
{Err: "net/http.ErrServerClosed", Fun: "net/http.ServeTLS"},
// pkg/os
{err: "io.EOF", fun: "(*os.File).Read"},
{err: "io.EOF", fun: "(*os.File).ReadAt"},
{err: "io.EOF", fun: "(*os.File).ReadDir"},
{err: "io.EOF", fun: "(*os.File).Readdir"},
{err: "io.EOF", fun: "(*os.File).Readdirnames"},
{Err: "io.EOF", Fun: "(*os.File).Read"},
{Err: "io.EOF", Fun: "(*os.File).ReadAt"},
{Err: "io.EOF", Fun: "(*os.File).ReadDir"},
{Err: "io.EOF", Fun: "(*os.File).Readdir"},
{Err: "io.EOF", Fun: "(*os.File).Readdirnames"},
// pkg/strings
{err: "io.EOF", fun: "(*strings.Reader).Read"},
{err: "io.EOF", fun: "(*strings.Reader).ReadAt"},
{err: "io.EOF", fun: "(*strings.Reader).ReadByte"},
{err: "io.EOF", fun: "(*strings.Reader).ReadRune"},
{Err: "io.EOF", Fun: "(*strings.Reader).Read"},
{Err: "io.EOF", Fun: "(*strings.Reader).ReadAt"},
{Err: "io.EOF", Fun: "(*strings.Reader).ReadByte"},
{Err: "io.EOF", Fun: "(*strings.Reader).ReadRune"},
// pkg/context
{err: "context.DeadlineExceeded", fun: "(context.Context).Err"},
{err: "context.Canceled", fun: "(context.Context).Err"},
{Err: "context.DeadlineExceeded", Fun: "(context.Context).Err"},
{Err: "context.Canceled", Fun: "(context.Context).Err"},
// pkg/encoding/json
{err: "io.EOF", fun: "(*encoding/json.Decoder).Decode"},
{Err: "io.EOF", Fun: "(*encoding/json.Decoder).Decode"},
// pkg/encoding/csv
{err: "io.EOF", fun: "(*encoding/csv.Reader).Read"},
{Err: "io.EOF", Fun: "(*encoding/csv.Reader).Read"},
// pkg/mime/multipart
{err: "io.EOF", fun: "(*mime/multipart.Reader).NextPart"},
{err: "io.EOF", fun: "(*mime/multipart.Reader).NextRawPart"},
{err: "mime/multipart.ErrMessageTooLarge", fun: "(*mime/multipart.Reader).ReadForm"},
{Err: "io.EOF", Fun: "(*mime/multipart.Reader).NextPart"},
{Err: "io.EOF", Fun: "(*mime/multipart.Reader).NextRawPart"},
{Err: "mime/multipart.ErrMessageTooLarge", Fun: "(*mime/multipart.Reader).ReadForm"},
}

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

func allowedMapAppend(ap []AllowPair) {
for _, pair := range ap {
if _, ok := allowedErrorsMap[pair.err]; !ok {
allowedErrorsMap[pair.err] = make(map[string]struct{})
if _, ok := allowedErrorsMap[pair.Err]; !ok {
allowedErrorsMap[pair.Err] = make(map[string]struct{})
}
allowedErrorsMap[pair.err][pair.fun] = struct{}{}
allowedErrorsMap[pair.Err][pair.Fun] = struct{}{}
}
}

var allowedErrorWildcards = []AllowPair{
// pkg/syscall
{err: "syscall.E", fun: "syscall."},
{Err: "syscall.E", Fun: "syscall."},
// golang.org/x/sys/unix
{err: "golang.org/x/sys/unix.E", fun: "golang.org/x/sys/unix."},
{Err: "golang.org/x/sys/unix.E", Fun: "golang.org/x/sys/unix."},
}

func allowedWildcardAppend(ap []AllowPair) {
Expand All @@ -117,7 +117,7 @@ func isAllowedErrAndFunc(err, fun string) bool {
}

for _, allow := range allowedErrorWildcards {
if strings.HasPrefix(fun, allow.fun) && strings.HasPrefix(err, allow.err) {
if strings.HasPrefix(fun, allow.Fun) && strings.HasPrefix(err, allow.Err) {
return true
}
}
Expand Down
29 changes: 0 additions & 29 deletions errorlint/analysis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,6 @@ func TestAllowedComparisons(t *testing.T) {
analysistest.Run(t, analysistest.TestData(), analyzer, "allowed")
}

func TestOptions(t *testing.T) {
testCases := []struct {
desc string
opt Option
pattern string
}{
{
desc: "WithAllowedErrors",
opt: WithAllowedErrors([]AllowPair{
{err: "io.EOF", fun: "example.com/pkg.Read"},
}),
pattern: "options/withAllowedErrors",
},
{
desc: "WithAllowedWildcard",
opt: WithAllowedWildcard([]AllowPair{
{err: "example.com/pkg.ErrMagic", fun: "example.com/pkg.Magic"},
}),
pattern: "options/withAllowedWildcard",
},
}
for _, tt := range testCases {
t.Run(tt.desc, func(t *testing.T) {
analyzer := NewAnalyzer(tt.opt)
analysistest.Run(t, analysistest.TestData(), analyzer, tt.pattern)
})
}
}

func TestIssueRegressions(t *testing.T) {
analyzer := NewAnalyzer()
analysistest.Run(t, analysistest.TestData(), analyzer, "issues")
Expand Down
38 changes: 38 additions & 0 deletions errorlint/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package errorlint_test

import (
"testing"

"github.com/polyfloyd/go-errorlint/errorlint"
"golang.org/x/tools/go/analysis/analysistest"
)

func TestOption(t *testing.T) {
testCases := []struct {
desc string
opt errorlint.Option
pattern string
}{
{
desc: "WithAllowedErrors",
opt: errorlint.WithAllowedErrors([]errorlint.AllowPair{
{Err: "io.EOF", Fun: "example.com/pkg.Read"},
}),
pattern: "options/withAllowedErrors",
},
{
desc: "WithAllowedWildcard",
opt: errorlint.WithAllowedWildcard([]errorlint.AllowPair{
{Err: "example.com/pkg.ErrMagic", Fun: "example.com/pkg.Magic"},
}),
pattern: "options/withAllowedWildcard",
},
}

for _, tt := range testCases {
t.Run(tt.desc, func(t *testing.T) {
analyzer := errorlint.NewAnalyzer(tt.opt)
analysistest.Run(t, analysistest.TestData(), analyzer, tt.pattern)
})
}
}

0 comments on commit edc3520

Please sign in to comment.