unwrap_used: Fix error message for unwrap_err when expect_used is allowed

This commit is contained in:
Sosthène Guédon 2022-08-17 18:58:17 +02:00
parent c1e04352bd
commit ab91d5a540
5 changed files with 25 additions and 7 deletions

View file

@ -25,7 +25,7 @@ pub(super) fn check(
None
};
let method = if is_err { "unwrap_err" } else { "unwrap" };
let method_suffix = if is_err { "_err" } else { "" };
if allow_unwrap_in_tests && is_in_test_function(cx.tcx, expr.hir_id) {
return;
@ -35,7 +35,7 @@ pub(super) fn check(
let help = if is_lint_allowed(cx, EXPECT_USED, expr.hir_id) {
format!(
"if you don't want to handle the `{none_value}` case gracefully, consider \
using `expect()` to provide a better panic message"
using `expect{method_suffix}()` to provide a better panic message"
)
} else {
format!("if this value is {none_prefix}`{none_value}`, it will panic")
@ -45,7 +45,7 @@ pub(super) fn check(
cx,
lint,
expr.span,
&format!("used `{method}()` on `{kind}` value"),
&format!("used `unwrap{method_suffix}()` on `{kind}` value"),
None,
&help,
);

View file

@ -6,8 +6,9 @@ fn expect_option() {
}
fn expect_result() {
let res: Result<u8, ()> = Ok(0);
let res: Result<u8, u8> = Ok(0);
let _ = res.expect("");
let _ = res.expect_err("");
}
fn main() {

View file

@ -15,5 +15,13 @@ LL | let _ = res.expect("");
|
= help: if this value is an `Err`, it will panic
error: aborting due to 2 previous errors
error: used `expect_err()` on `a Result` value
--> $DIR/expect.rs:11:13
|
LL | let _ = res.expect_err("");
| ^^^^^^^^^^^^^^^^^^
|
= help: if this value is an `Ok`, it will panic
error: aborting due to 3 previous errors

View file

@ -6,8 +6,9 @@ fn unwrap_option() {
}
fn unwrap_result() {
let res: Result<u8, ()> = Ok(0);
let res: Result<u8, u8> = Ok(0);
let _ = res.unwrap();
let _ = res.unwrap_err();
}
fn main() {

View file

@ -15,5 +15,13 @@ LL | let _ = res.unwrap();
|
= help: if you don't want to handle the `Err` case gracefully, consider using `expect()` to provide a better panic message
error: aborting due to 2 previous errors
error: used `unwrap_err()` on `a Result` value
--> $DIR/unwrap.rs:11:13
|
LL | let _ = res.unwrap_err();
| ^^^^^^^^^^^^^^^^
|
= help: if you don't want to handle the `Ok` case gracefully, consider using `expect_err()` to provide a better panic message
error: aborting due to 3 previous errors