Skip to content

Commit ab91d5a

Browse files
committed
unwrap_used: Fix error message for unwrap_err when expect_used is allowed
1 parent c1e0435 commit ab91d5a

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

clippy_lints/src/methods/unwrap_used.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub(super) fn check(
2525
None
2626
};
2727

28-
let method = if is_err { "unwrap_err" } else { "unwrap" };
28+
let method_suffix = if is_err { "_err" } else { "" };
2929

3030
if allow_unwrap_in_tests && is_in_test_function(cx.tcx, expr.hir_id) {
3131
return;
@@ -35,7 +35,7 @@ pub(super) fn check(
3535
let help = if is_lint_allowed(cx, EXPECT_USED, expr.hir_id) {
3636
format!(
3737
"if you don't want to handle the `{none_value}` case gracefully, consider \
38-
using `expect()` to provide a better panic message"
38+
using `expect{method_suffix}()` to provide a better panic message"
3939
)
4040
} else {
4141
format!("if this value is {none_prefix}`{none_value}`, it will panic")
@@ -45,7 +45,7 @@ pub(super) fn check(
4545
cx,
4646
lint,
4747
expr.span,
48-
&format!("used `{method}()` on `{kind}` value"),
48+
&format!("used `unwrap{method_suffix}()` on `{kind}` value"),
4949
None,
5050
&help,
5151
);

tests/ui/expect.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ fn expect_option() {
66
}
77

88
fn expect_result() {
9-
let res: Result<u8, ()> = Ok(0);
9+
let res: Result<u8, u8> = Ok(0);
1010
let _ = res.expect("");
11+
let _ = res.expect_err("");
1112
}
1213

1314
fn main() {

tests/ui/expect.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,13 @@ LL | let _ = res.expect("");
1515
|
1616
= help: if this value is an `Err`, it will panic
1717

18-
error: aborting due to 2 previous errors
18+
error: used `expect_err()` on `a Result` value
19+
--> $DIR/expect.rs:11:13
20+
|
21+
LL | let _ = res.expect_err("");
22+
| ^^^^^^^^^^^^^^^^^^
23+
|
24+
= help: if this value is an `Ok`, it will panic
25+
26+
error: aborting due to 3 previous errors
1927

tests/ui/unwrap.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ fn unwrap_option() {
66
}
77

88
fn unwrap_result() {
9-
let res: Result<u8, ()> = Ok(0);
9+
let res: Result<u8, u8> = Ok(0);
1010
let _ = res.unwrap();
11+
let _ = res.unwrap_err();
1112
}
1213

1314
fn main() {

tests/ui/unwrap.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,13 @@ LL | let _ = res.unwrap();
1515
|
1616
= help: if you don't want to handle the `Err` case gracefully, consider using `expect()` to provide a better panic message
1717

18-
error: aborting due to 2 previous errors
18+
error: used `unwrap_err()` on `a Result` value
19+
--> $DIR/unwrap.rs:11:13
20+
|
21+
LL | let _ = res.unwrap_err();
22+
| ^^^^^^^^^^^^^^^^
23+
|
24+
= help: if you don't want to handle the `Ok` case gracefully, consider using `expect_err()` to provide a better panic message
25+
26+
error: aborting due to 3 previous errors
1927

0 commit comments

Comments
 (0)