Skip to content

Commit a591e72

Browse files
committed
Auto merge of rust-lang#9223 - sgued:unwrap-expect-used, r=giraffate
unwrap_used: Don't recommend using `expect` when the `expect_used` lint is not allowed Fixes rust-lang#9222 ``` changelog: [`unwrap_used`]: Don't recommend using `expect` when the `expect_used` lint is not allowed ```
2 parents a0ed687 + 23b4fe6 commit a591e72

File tree

7 files changed

+80
-18
lines changed

7 files changed

+80
-18
lines changed

clippy_lints/src/methods/expect_used.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
1212
let obj_ty = cx.typeck_results().expr_ty(recv).peel_refs();
1313

1414
let mess = if is_type_diagnostic_item(cx, obj_ty, sym::Option) {
15-
Some((EXPECT_USED, "an Option", "None"))
15+
Some((EXPECT_USED, "an Option", "None", ""))
1616
} else if is_type_diagnostic_item(cx, obj_ty, sym::Result) {
17-
Some((EXPECT_USED, "a Result", "Err"))
17+
Some((EXPECT_USED, "a Result", "Err", "an "))
1818
} else {
1919
None
2020
};
@@ -23,14 +23,14 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
2323
return;
2424
}
2525

26-
if let Some((lint, kind, none_value)) = mess {
26+
if let Some((lint, kind, none_value, none_prefix)) = mess {
2727
span_lint_and_help(
2828
cx,
2929
lint,
3030
expr.span,
31-
&format!("used `expect()` on `{}` value", kind,),
31+
&format!("used `expect()` on `{kind}` value"),
3232
None,
33-
&format!("if this value is an `{}`, it will panic", none_value,),
33+
&format!("if this value is {none_prefix}`{none_value}`, it will panic"),
3434
);
3535
}
3636
}

clippy_lints/src/methods/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@ declare_clippy_lint! {
204204
/// option.expect("more helpful message");
205205
/// result.expect("more helpful message");
206206
/// ```
207+
///
208+
/// If [expect_used](#expect_used) is enabled, instead:
209+
/// ```rust,ignore
210+
/// # let option = Some(1);
211+
/// # let result: Result<usize, ()> = Ok(1);
212+
/// option?;
213+
///
214+
/// // or
215+
///
216+
/// result?;
217+
/// ```
207218
#[clippy::version = "1.45.0"]
208219
pub UNWRAP_USED,
209220
restriction,
Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
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) {
1212
let obj_ty = cx.typeck_results().expr_ty(recv).peel_refs();
1313

1414
let mess = if is_type_diagnostic_item(cx, obj_ty, sym::Option) {
15-
Some((UNWRAP_USED, "an Option", "None"))
15+
Some((UNWRAP_USED, "an Option", "None", ""))
1616
} else if is_type_diagnostic_item(cx, obj_ty, sym::Result) {
17-
Some((UNWRAP_USED, "a Result", "Err"))
17+
Some((UNWRAP_USED, "a Result", "Err", "an "))
1818
} else {
1919
None
2020
};
@@ -23,18 +23,23 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
2323
return;
2424
}
2525

26-
if let Some((lint, kind, none_value)) = mess {
26+
if let Some((lint, kind, none_value, none_prefix)) = 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 {none_prefix}`{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-toml/expect_used/expect_used.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | let _ = opt.expect("");
55
| ^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::expect-used` implied by `-D warnings`
8-
= help: if this value is an `None`, it will panic
8+
= help: if this value is `None`, it will panic
99

1010
error: used `expect()` on `a Result` value
1111
--> $DIR/expect_used.rs:11:13

tests/ui/expect.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | let _ = opt.expect("");
55
| ^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::expect-used` implied by `-D warnings`
8-
= help: if this value is an `None`, it will panic
8+
= help: if this value is `None`, it will panic
99

1010
error: used `expect()` on `a Result` value
1111
--> $DIR/expect.rs:10:13

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 `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 `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)