go-errorlint icon indicating copy to clipboard operation
go-errorlint copied to clipboard

implement whilelist for switch

Open kolyshkin opened this issue 2 years ago • 2 comments

The following legitimate comparison using switch statement

func SwitchOnUnixErrors() {
       err := unix.Rmdir("somepath")
       switch err {
       case unix.ENOENT:
               return
       case unix.EPERM:
               return
       }
       fmt.Println(err)
}

results in a warning:

switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors

Apparently, whitelist of allowed direct comparisons, as implemented in allowed.go, is not used when analysing switch statements.

kolyshkin avatar Aug 25 '23 01:08 kolyshkin

Another example:

func SwitchIoEOF(r io.Reader) {
        var buf [4096]byte
        _, err := r.Read(buf[:])
        switch err {
        case nil:
                return
        case io.EOF:
                return
        }
        fmt.Println(err)
}

kolyshkin avatar Aug 25 '23 01:08 kolyshkin

Yes, that should be a valid comparison. The allowlist must apply to switch statements too

polyfloyd avatar Sep 02 '23 16:09 polyfloyd

Fixed in c0e6cac0ec994c118a788505f20fbb20d2b38c11

polyfloyd avatar May 30 '24 11:05 polyfloyd