about summary refs log tree commit diff
diff options
context:
space:
mode:
authorrelaxcn <chen2035@foxmail.com>2025-07-12 01:44:55 +0800
committerrelaxcn <chen2035@foxmail.com>2025-07-12 01:44:55 +0800
commit1d1b97d514d5ca0f7effb6657acbdc647821b738 (patch)
tree6c9ba8ba41a4d6abb7f9f8b2f2899e0bf7b9e27e
parente2270bb9e582465099fbad550be13f6892339f42 (diff)
downloadrust-1d1b97d514d5ca0f7effb6657acbdc647821b738.tar.gz
rust-1d1b97d514d5ca0f7effb6657acbdc647821b738.zip
fix false negative of `expect_used`
-rw-r--r--clippy_lints/src/methods/mod.rs2
-rw-r--r--tests/ui/expect.rs19
-rw-r--r--tests/ui/expect.stderr34
3 files changed, 53 insertions, 2 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs
index f2dabdd3438..fc3363b0662 100644
--- a/clippy_lints/src/methods/mod.rs
+++ b/clippy_lints/src/methods/mod.rs
@@ -5546,7 +5546,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