Skip to content

Commit

Permalink
unwrap_used: Fix error message for unwrap_err when expect_used is all…
Browse files Browse the repository at this point in the history
…owed
  • Loading branch information
sgued committed Aug 17, 2022
1 parent c1e0435 commit ab91d5a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
6 changes: 3 additions & 3 deletions clippy_lints/src/methods/unwrap_used.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand All @@ -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,
);
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 9 additions & 1 deletion tests/ui/expect.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

3 changes: 2 additions & 1 deletion tests/ui/unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 9 additions & 1 deletion tests/ui/unwrap.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ab91d5a

Please sign in to comment.