Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning about comparisons for indirect errors #55

Closed
kolyshkin opened this issue Aug 25, 2023 · 4 comments
Closed

Warning about comparisons for indirect errors #55

kolyshkin opened this issue Aug 25, 2023 · 4 comments

Comments

@kolyshkin
Copy link
Contributor

The following code produces no warnings (since v1.4.4, which has #10 fixed).

func CompareUnixErrors() {
        if err := unix.Rmdir("somepath"); err != unix.ENOENT {
                fmt.Println(err)
        }
}

but if we wrap unix.Rmdir into a function:

func rmdir(path string) error {
        return unix.Rmdir(path)
}

func CompareUnixErrors() {
        if err := rmdir("somepath"); err != unix.ENOENT {
                fmt.Println(err)
        }
}

we still get the warning:

unexpected diagnostic: comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error

@kolyshkin
Copy link
Contributor Author

kolyshkin commented Aug 25, 2023

This might or might not be a dup of #49.

Update: it is not.

kolyshkin added a commit to kolyshkin/selinux that referenced this issue Aug 25, 2023
golangci-lint v1.54.2 comes with errorlint v1.4.4, which contains
the fix [1] whitelisting all errno comparisons for errors coming from
x/sys/unix. Thus, these annotations are no longer necessary. Remove
those.

Except, the errors that do not come directly from a function in unix
package still need to be annotated (see [2]).

[1] polyfloyd/go-errorlint#47
[2] polyfloyd/go-errorlint#55
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
@kolyshkin
Copy link
Contributor Author

This is not specific to unix errors. Here is another example, for io.EOF:

func read(r io.Reader) error {
        var buf [1]byte
        _, err := r.Read(buf[:])
        return err
}

func CompareIoEOFIndirectly(r io.Reader) {
        err := read(r)
        if err != io.EOF {
                fmt.Println(err)
        }
}

@kolyshkin kolyshkin changed the title Warning about comparison for indirect unix error Warning about comparisons for indirect errors Aug 25, 2023
@polyfloyd
Copy link
Owner

Hi!

I have thought about this while developing the linter, but considered this to be too far enough from the original problem and too complex to solve.

As you know, the linter uses a list of permitted function/error combinations to decide whether a raise an issue. But when an error is returned inside a function not part of this allowlist this of course does not work. A solution that achieves what you are suggesting would have to look inside the function being called and build a set of all errors being produced and match it against the list. At this point, I would suggest to use errors.Is to check for errors from such functions. In fact, it might even be desirable to wrap the error anyway to provide context from the wrapping function.

Another solution would be a feature to for custom additions to the allowlist so project specific functions known to be safe could be allowed.

@kolyshkin
Copy link
Contributor Author

@polyfloyd I guess you are right -- any such function can in fact wrap an error. I do not consider this a bug, thus closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants