rust-lang/rust-clippy

Add lint for match `io::ErrorKind::Other`

Closed this issue · 2 comments

What it does

io::ErrorKind::Other is not recommended to be used in the match:

A custom error that does not fall under any other I/O error kind.

This can be used to construct your own Errors that do not match any ErrorKind.

This ErrorKind is not used by the standard library.

Errors from the standard library that do not fall under any of the I/O error kinds cannot be matched on, and will only match a wildcard (_) pattern. New ErrorKinds might be added in the future for some of those.

But Other is easy to be used wrongly.

It's better for us to warn of this usage.

As proposed in rust-lang/rust#86442 (comment)

Lint Name

match_io_error_other

Category

correctness

Advantage

  • Avoid breaking after the new ErrorKind variant introduced

Drawbacks

  • None

Example

let Err(e) = std::fs::rename("a", "b") {
    match e {
        std::io::ErrorKind::Other => {}
    }
}

Could be written as:

let Err(e) = std::fs::rename("a", "b") {
    match e {
        _ => {}
    }
}

I think this lint is not useful as suggested. io::ErrorKind::Other is already no longer used for uncategorized errors (that now rests with io:ErrorKind::Uncategorized), so there is no residual risk of breaking on new error kinds. Furthermore, io::ErrorKind::Other should, and often is, used for generating custom errors in 3rd-party libraries, and matching on those is actually a useful feature to have.

Furthermore, io::ErrorKind::Other should, and often is, used for generating custom errors in 3rd-party libraries, and matching on those is actually a useful feature to have.

I got it. Thanks for the explanation. Let's close.