-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
match_wild_err_arm: Err(_) is not wild #3688
Comments
I don't get it either. Based on the logic in this lint, we should also warn on |
That enum Error {
InternalError,
WrongArguments,
FileNotFound,
...,
}
fn main() {
match run_my_program() {
Ok(_) => println!("yay"),
Err(_) => panic!("nay"),
}
} Would result in the error message match run_my_program() {
Ok(_) => println!("yay"),
Err(Error::InternalError) => panic!("oops"),
Err(Error::WrongArguments) => panic!("{}", help),
Err(Error::FileNotFound) => panic!("File {} does not exist", file),
...
} This is better, because it gives a custom error message dependent on the type of the error. Panicking on a known error is still probably something you don't want to do and you may want to handle errors with crates like That said, the documentation should be more specific about what the lint wants from the programmer. |
IMHO this isn't quite the same. If you write Maybe this lint can mention something like: On |
I can see that when the error is an enum it can be helpful. If that is the case, there are some false positives. For example enum Radix {
Digits(i32),
DigitsLetters(i32),
AltDigits(i32),
AltDigitsLetters(i32),
}
fn classify(radix: i32) -> Result<Radix, i32> {
match radix {
2..=10 => Ok(Radix::Digits(radix)),
11..=36 => Ok(Radix::DigitsLetters(radix)),
-10..=-2 => Ok(Radix::AltDigits(-radix)),
-36..=-11 => Ok(Radix::AltDigitsLetters(-radix)),
_ => Err(radix),
}
}
// Panics if magnitude of radix < 2 or > 36
fn foo(n: u32, radix: i32) {
match classify(radix) {
Ok(Radix::Digits(radix)) => print_digits(n, radix),
Ok(Radix::DigitsLetters(radix)) => print_digits_letters(n, radix),
Ok(Radix::AltDigits(radix)) => alt_print_digits(n, radix),
Ok(Radix::AltDigitsLetters(radix)) => alt_print_digits_letters(n, radix),
Err(_) => panic!("wrong argument"),
}
} In this case I'd say the warning is a false positive. So the only time the warning is helpful is when the error type is an enum. Why not warn only in that case then? It would make it easier to give a helpful message too, because then it would make sense to ask for matching each error variant separately. |
Actually, I'd go further and say that this lint should be pedantic, in addition to making it only warn for public enums. |
## Motivation Looks like Clippy is once again complaining about the `match_wild_err_arm` lint, presumably as a result of the Rust 1.42 release. This lint triggers on the `try_lock!` macro that `tracing-subscriber` uses to avoid double panics when a mutex is poisoned. In this case, the lint is something of a false positive here, since we _do_ actually have two different `Err(...)` arms; the differentiation between the two arms is not in the match pattern but in a guard. See rust-lang/rust-clippy#3688 for details on the lint. ## Solution I've refactored the code in question to use `if`/`else`, avoiding the lint. Signed-off-by: Eliza Weisman <[email protected]>
Agreed, this lint just failed my CI(https://travis-ci.org/github/elichai/stdio-override/jobs/689175682) and the code there is reasonable, I don't always want to use |
The match_wild_err_arm lint documentation compares
Err(_)
to Java withcatch(Exception)
, but it is nothing like it. While the Java version does catch all exceptions, the Rust version can only catch one error type.Even the warning message provided is misguided. It recommends matching each error separately, but there is only one error to match.
This makes this lint not really helpful. Or am I missing something?
The text was updated successfully, but these errors were encountered: