diff options
| author | Jason Newcomb <jsnewcomb@pm.me> | 2025-07-14 07:52:48 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-14 07:52:48 +0000 |
| commit | d9acd919ad6441af2711306e5ec0242329bf5f4c (patch) | |
| tree | 62fb1c48b6ee11806dbfcfd41219860db498c1e0 | |
| parent | 43f18915e9cef28bd605d0545016a5ee4394e34d (diff) | |
| parent | 1d1b97d514d5ca0f7effb6657acbdc647821b738 (diff) | |
fix false negative of `expect_used` (#15253)
Fixes: rust-lang/rust-clippy#15247 changelog: Fix the false negative of [`expect_used`] when metting `Option::ok().expect`.
| -rw-r--r-- | clippy_lints/src/methods/mod.rs | 2 | ||||
| -rw-r--r-- | tests/ui/expect.rs | 19 | ||||
| -rw-r--r-- | tests/ui/expect.stderr | 34 |
3 files changed, 53 insertions, 2 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 2bb2db3bd78..be75178dcc8 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -5557,7 +5557,7 @@ impl Methods { // Handle method calls whose receiver and arguments may come from expansion if let ExprKind::MethodCall(path, recv, args, _call_span) = expr.kind { match (path.ident.name, args) { - (sym::expect, [_]) if !matches!(method_call(recv), Some((sym::ok | sym::err, _, [], _, _))) => { + (sym::expect, [_]) => { unwrap_expect_used::check( cx, expr, diff --git a/tests/ui/expect.rs b/tests/ui/expect.rs index 8f7379f0021..1ab01ecfcfe 100644 --- a/tests/ui/expect.rs +++ b/tests/ui/expect.rs @@ -16,7 +16,26 @@ fn expect_result() { //~^ expect_used } +#[allow(clippy::ok_expect)] +#[allow(clippy::err_expect)] +fn issue_15247() { + let x: Result<u8, u8> = Err(0); + x.ok().expect("Huh"); + //~^ expect_used + + { x.ok() }.expect("..."); + //~^ expect_used + + let y: Result<u8, u8> = Ok(0); + y.err().expect("Huh"); + //~^ expect_used + + { y.err() }.expect("..."); + //~^ expect_used +} + fn main() { expect_option(); expect_result(); + issue_15247(); } diff --git a/tests/ui/expect.stderr b/tests/ui/expect.stderr index 70cf3072003..353fb776531 100644 --- a/tests/ui/expect.stderr +++ b/tests/ui/expect.stderr @@ -24,5 +24,37 @@ LL | let _ = res.expect_err(""); | = note: if this value is an `Ok`, it will panic -error: aborting due to 3 previous errors +error: used `expect()` on an `Option` value + --> tests/ui/expect.rs:23:5 + | +LL | x.ok().expect("Huh"); + | ^^^^^^^^^^^^^^^^^^^^ + | + = note: if this value is `None`, it will panic + +error: used `expect()` on an `Option` value + --> tests/ui/expect.rs:26:5 + | +LL | { x.ok() }.expect("..."); + | ^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: if this value is `None`, it will panic + +error: used `expect()` on an `Option` value + --> tests/ui/expect.rs:30:5 + | +LL | y.err().expect("Huh"); + | ^^^^^^^^^^^^^^^^^^^^^ + | + = note: if this value is `None`, it will panic + +error: used `expect()` on an `Option` value + --> tests/ui/expect.rs:33:5 + | +LL | { y.err() }.expect("..."); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: if this value is `None`, it will panic + +error: aborting due to 7 previous errors |
