about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorAlejandra González <blyxyas@gmail.com>2025-05-19 22:12:43 +0000
committerGitHub <noreply@github.com>2025-05-19 22:12:43 +0000
commit66697e84b19c117b308eb1c02edcd6e6f299e30d (patch)
treeb38d56771794cab02dde6e51d2d5ff525613e829 /tests
parentb87e90b28f474c64a753e65b81a973f7b1a15d52 (diff)
parent82f8b1ccd09e94a6f3cb880de0d4ca99fd7bb9aa (diff)
downloadrust-66697e84b19c117b308eb1c02edcd6e6f299e30d.tar.gz
rust-66697e84b19c117b308eb1c02edcd6e6f299e30d.zip
`needless_match`: do not pretend that `return` is not significant in an expression (#14757)
A `return` in an expression makes it divergent and cannot be removed
blindly. While this stripping might have been introduced as a way to
catch more cases, it was improperly used, and no tests exhibit a failure
when this special handling is removed.

changelog: [`needless_match`]: do not strip `return` as it might make
the `if let` or `match` divergent in some cases

Fixes rust-lang/rust-clippy#14754
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/needless_match.fixed12
-rw-r--r--tests/ui/needless_match.rs15
-rw-r--r--tests/ui/needless_match.stderr12
3 files changed, 38 insertions, 1 deletions
diff --git a/tests/ui/needless_match.fixed b/tests/ui/needless_match.fixed
index b2c2bbfaa36..41acf44023f 100644
--- a/tests/ui/needless_match.fixed
+++ b/tests/ui/needless_match.fixed
@@ -301,4 +301,16 @@ pub fn issue13574() -> Option<()> {
     None
 }
 
+fn issue14754(t: Result<i32, &'static str>) -> Result<i32, &'static str> {
+    let _ = match t {
+        Ok(v) => Ok::<_, &'static str>(v),
+        err @ Err(_) => return err,
+    };
+    println!("Still here");
+    let x = t;
+    //~^^^^ needless_match
+    println!("Still here");
+    x
+}
+
 fn main() {}
diff --git a/tests/ui/needless_match.rs b/tests/ui/needless_match.rs
index 1cb670edc60..936653b961b 100644
--- a/tests/ui/needless_match.rs
+++ b/tests/ui/needless_match.rs
@@ -364,4 +364,19 @@ pub fn issue13574() -> Option<()> {
     None
 }
 
+fn issue14754(t: Result<i32, &'static str>) -> Result<i32, &'static str> {
+    let _ = match t {
+        Ok(v) => Ok::<_, &'static str>(v),
+        err @ Err(_) => return err,
+    };
+    println!("Still here");
+    let x = match t {
+        Ok(v) => Ok::<_, &'static str>(v),
+        err @ Err(_) => err,
+    };
+    //~^^^^ needless_match
+    println!("Still here");
+    x
+}
+
 fn main() {}
diff --git a/tests/ui/needless_match.stderr b/tests/ui/needless_match.stderr
index 719b0ef8846..5195ecdfa55 100644
--- a/tests/ui/needless_match.stderr
+++ b/tests/ui/needless_match.stderr
@@ -151,5 +151,15 @@ LL | |             None
 LL | |         }
    | |_________^ help: replace it with: `A`
 
-error: aborting due to 14 previous errors
+error: this match expression is unnecessary
+  --> tests/ui/needless_match.rs:373:13
+   |
+LL |       let x = match t {
+   |  _____________^
+LL | |         Ok(v) => Ok::<_, &'static str>(v),
+LL | |         err @ Err(_) => err,
+LL | |     };
+   | |_____^ help: replace it with: `t`
+
+error: aborting due to 15 previous errors