go-errorlint
go-errorlint copied to clipboard
implement whilelist for switch
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.
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)
}
Yes, that should be a valid comparison. The allowlist must apply to switch statements too
Fixed in c0e6cac0ec994c118a788505f20fbb20d2b38c11