Skip to content

Commit 6ee03e2

Browse files
committed
unwrap_used: Stop recommending using expect when the expect_used lint is not allowed
1 parent 05a51e5 commit 6ee03e2

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

clippy_lints/src/methods/unwrap_used.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::is_in_test_function;
32
use clippy_utils::ty::is_type_diagnostic_item;
3+
use clippy_utils::{is_in_test_function, is_lint_allowed};
44
use rustc_hir as hir;
55
use rustc_lint::LateContext;
66
use rustc_span::sym;
77

8-
use super::UNWRAP_USED;
8+
use super::{EXPECT_USED, UNWRAP_USED};
99

1010
/// lint use of `unwrap()` for `Option`s and `Result`s
1111
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, allow_unwrap_in_tests: bool) {
@@ -24,17 +24,22 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
2424
}
2525

2626
if let Some((lint, kind, none_value)) = mess {
27+
let help = if is_lint_allowed(cx, EXPECT_USED, expr.hir_id) {
28+
format!(
29+
"if you don't want to handle the `{none_value}` case gracefully, consider \
30+
using `expect()` to provide a better panic message"
31+
)
32+
} else {
33+
format!("if this value is an `{none_value}`, it will panic")
34+
};
35+
2736
span_lint_and_help(
2837
cx,
2938
lint,
3039
expr.span,
31-
&format!("used `unwrap()` on `{}` value", kind,),
40+
&format!("used `unwrap()` on `{kind}` value"),
3241
None,
33-
&format!(
34-
"if you don't want to handle the `{}` case gracefully, consider \
35-
using `expect()` to provide a better panic message",
36-
none_value,
37-
),
42+
&help,
3843
);
3944
}
4045
}

tests/ui/unwrap_expect_used.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![warn(clippy::unwrap_used, clippy::expect_used)]
2+
3+
fn main() {
4+
Some(3).unwrap();
5+
Some(3).expect("Hello world!");
6+
7+
let a: Result<i32, i32> = Ok(3);
8+
a.unwrap();
9+
a.expect("Hello world!");
10+
}

tests/ui/unwrap_expect_used.stderr

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error: used `unwrap()` on `an Option` value
2+
--> $DIR/unwrap_expect_used.rs:4:5
3+
|
4+
LL | Some(3).unwrap();
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::unwrap-used` implied by `-D warnings`
8+
= help: if this value is an `None`, it will panic
9+
10+
error: used `expect()` on `an Option` value
11+
--> $DIR/unwrap_expect_used.rs:5:5
12+
|
13+
LL | Some(3).expect("Hello world!");
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: `-D clippy::expect-used` implied by `-D warnings`
17+
= help: if this value is an `None`, it will panic
18+
19+
error: used `unwrap()` on `a Result` value
20+
--> $DIR/unwrap_expect_used.rs:8:5
21+
|
22+
LL | a.unwrap();
23+
| ^^^^^^^^^^
24+
|
25+
= help: if this value is an `Err`, it will panic
26+
27+
error: used `expect()` on `a Result` value
28+
--> $DIR/unwrap_expect_used.rs:9:5
29+
|
30+
LL | a.expect("Hello world!");
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^
32+
|
33+
= help: if this value is an `Err`, it will panic
34+
35+
error: aborting due to 4 previous errors
36+

0 commit comments

Comments
 (0)