Skip to content

Commit

Permalink
fix: ignore unix errno values
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin and polyfloyd committed Aug 2, 2023
1 parent 2b2a777 commit 6464b7e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions errorlint/allowed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package errorlint
import (
"fmt"
"go/ast"
"strings"
)

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

var allowedErrorWildcards = []struct {
err string
fun string
}{
// golang.org/x/sys/unix
{err: "golang.org/x/sys/unix.E", fun: "golang.org/x/sys/unix."},
}

func isAllowedErrAndFunc(err, fun string) bool {
for _, allow := range allowedErrorWildcards {
if strings.HasPrefix(fun, allow.fun) && strings.HasPrefix(err, allow.err) {
return true
}
}

for _, allow := range allowedErrors {
if allow.fun == fun && allow.err == err {
return true
Expand Down
12 changes: 12 additions & 0 deletions errorlint/testdata/src/allowed/allowed.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"io"
"net/http"
"os"
"syscall"

"golang.org/x/sys/unix"
)

func CompareErrIndirect(r io.Reader) {
Expand Down Expand Up @@ -215,3 +218,12 @@ func TarHeader(r io.Reader) {
_ = header
}
}

func CompareUnixErrors() {
if err := unix.Rmdir("somepath"); err != unix.ENOENT {
fmt.Println(err)
}
if err := unix.Kill(1, syscall.SIGKILL); err != unix.EPERM {
fmt.Println(err)
}
}

0 comments on commit 6464b7e

Please sign in to comment.