Skip to content

Commit 41edee2

Browse files
kolyshkinpolyfloyd
andcommitted
fix: ignore unix errno values
All functions in golang.org/x/sys/unix always return raw errno values, which can be compared directly. There are about 100-130 errors, and about 350 functions in unix package. Listing them separately is hardly an option, so let's ignore all unix.E* errors that come from all functions in unix package. This functionality is made possible thanks to commit cd16050. Add a small test case (we can't really add the whole x/sys/unix under testdata, so use a stub implementation). Fixes: #10 Co-Authored-by: polyfloyd <floyd@polyfloyd.net> Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
1 parent 2b2a777 commit 41edee2

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

errorlint/allowed.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package errorlint
33
import (
44
"fmt"
55
"go/ast"
6+
"strings"
67
)
78

89
var allowedErrors = []struct {
@@ -71,7 +72,21 @@ var allowedErrors = []struct {
7172
{err: "io.EOF", fun: "(*strings.Reader).ReadRune"},
7273
}
7374

75+
var allowedErrorWildcards = []struct {
76+
err string
77+
fun string
78+
}{
79+
// golang.org/x/sys/unix
80+
{err: "golang.org/x/sys/unix.E", fun: "golang.org/x/sys/unix."},
81+
}
82+
7483
func isAllowedErrAndFunc(err, fun string) bool {
84+
for _, allow := range allowedErrorWildcards {
85+
if strings.HasPrefix(fun, allow.fun) && strings.HasPrefix(err, allow.err) {
86+
return true
87+
}
88+
}
89+
7590
for _, allow := range allowedErrors {
7691
if allow.fun == fun && allow.err == err {
7792
return true

errorlint/testdata/src/allowed/allowed.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
"io"
1111
"net/http"
1212
"os"
13+
"syscall"
14+
15+
"golang.org/x/sys/unix"
1316
)
1417

1518
func CompareErrIndirect(r io.Reader) {
@@ -215,3 +218,12 @@ func TarHeader(r io.Reader) {
215218
_ = header
216219
}
217220
}
221+
222+
func CompareUnixErrors() {
223+
if err := unix.Rmdir("somepath"); err != unix.ENOENT {
224+
fmt.Println(err)
225+
}
226+
if err := unix.Kill(1, syscall.SIGKILL); err != unix.EPERM {
227+
fmt.Println(err)
228+
}
229+
}

0 commit comments

Comments
 (0)