Skip to content

Commit d9acd91

Browse files
authored
fix false negative of expect_used (#15253)
Fixes: #15247 changelog: Fix the false negative of [`expect_used`] when metting `Option::ok().expect`.
2 parents 43f1891 + 1d1b97d commit d9acd91

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5557,7 +5557,7 @@ impl Methods {
55575557
// Handle method calls whose receiver and arguments may come from expansion
55585558
if let ExprKind::MethodCall(path, recv, args, _call_span) = expr.kind {
55595559
match (path.ident.name, args) {
5560-
(sym::expect, [_]) if !matches!(method_call(recv), Some((sym::ok | sym::err, _, [], _, _))) => {
5560+
(sym::expect, [_]) => {
55615561
unwrap_expect_used::check(
55625562
cx,
55635563
expr,

tests/ui/expect.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,26 @@ fn expect_result() {
1616
//~^ expect_used
1717
}
1818

19+
#[allow(clippy::ok_expect)]
20+
#[allow(clippy::err_expect)]
21+
fn issue_15247() {
22+
let x: Result<u8, u8> = Err(0);
23+
x.ok().expect("Huh");
24+
//~^ expect_used
25+
26+
{ x.ok() }.expect("...");
27+
//~^ expect_used
28+
29+
let y: Result<u8, u8> = Ok(0);
30+
y.err().expect("Huh");
31+
//~^ expect_used
32+
33+
{ y.err() }.expect("...");
34+
//~^ expect_used
35+
}
36+
1937
fn main() {
2038
expect_option();
2139
expect_result();
40+
issue_15247();
2241
}

tests/ui/expect.stderr

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,37 @@ LL | let _ = res.expect_err("");
2424
|
2525
= note: if this value is an `Ok`, it will panic
2626

27-
error: aborting due to 3 previous errors
27+
error: used `expect()` on an `Option` value
28+
--> tests/ui/expect.rs:23:5
29+
|
30+
LL | x.ok().expect("Huh");
31+
| ^^^^^^^^^^^^^^^^^^^^
32+
|
33+
= note: if this value is `None`, it will panic
34+
35+
error: used `expect()` on an `Option` value
36+
--> tests/ui/expect.rs:26:5
37+
|
38+
LL | { x.ok() }.expect("...");
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^
40+
|
41+
= note: if this value is `None`, it will panic
42+
43+
error: used `expect()` on an `Option` value
44+
--> tests/ui/expect.rs:30:5
45+
|
46+
LL | y.err().expect("Huh");
47+
| ^^^^^^^^^^^^^^^^^^^^^
48+
|
49+
= note: if this value is `None`, it will panic
50+
51+
error: used `expect()` on an `Option` value
52+
--> tests/ui/expect.rs:33:5
53+
|
54+
LL | { y.err() }.expect("...");
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
56+
|
57+
= note: if this value is `None`, it will panic
58+
59+
error: aborting due to 7 previous errors
2860

0 commit comments

Comments
 (0)