about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-12-29 09:23:03 +0000
committerbors <bors@rust-lang.org>2022-12-29 09:23:03 +0000
commit22af8fe6837ac71afc8ae2db956ba6b9b41ec1e1 (patch)
treee0a428919268336fc54d147bd7b892988da33119
parent266eef77011234af6e612cf2bf22441112f1f90e (diff)
parent3b6bbf7d16dd8b42fadf8dcdbad4d4d872a3e88c (diff)
downloadrust-22af8fe6837ac71afc8ae2db956ba6b9b41ec1e1.tar.gz
rust-22af8fe6837ac71afc8ae2db956ba6b9b41ec1e1.zip
Auto merge of #10060 - alex-semenyuk:match_single_binding_fix, r=llogiq
 Fix [match_single_binding] suggestion introduced an extra semicolon

Fix #9725

---

changelog: [`match_single_binding`]: suggestion no longer introduces unneeded semicolons
[#10060](https://github.com/rust-lang/rust-clippy/pull/10060)
<!-- changelog_checked -->
-rw-r--r--clippy_lints/src/matches/match_single_binding.rs18
-rw-r--r--tests/ui/match_single_binding.fixed13
-rw-r--r--tests/ui/match_single_binding.rs14
-rw-r--r--tests/ui/match_single_binding.stderr27
4 files changed, 58 insertions, 14 deletions
diff --git a/clippy_lints/src/matches/match_single_binding.rs b/clippy_lints/src/matches/match_single_binding.rs
index 1bf8d4e96ad..c94a1f76330 100644
--- a/clippy_lints/src/matches/match_single_binding.rs
+++ b/clippy_lints/src/matches/match_single_binding.rs
@@ -31,19 +31,11 @@ pub(crate) fn check<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[Arm<'_>], e
     };
 
     // Do we need to add ';' to suggestion ?
-    match match_body.kind {
-        ExprKind::Block(block, _) => {
-            // macro + expr_ty(body) == ()
-            if block.span.from_expansion() && cx.typeck_results().expr_ty(match_body).is_unit() {
-                snippet_body.push(';');
-            }
-        },
-        _ => {
-            // expr_ty(body) == ()
-            if cx.typeck_results().expr_ty(match_body).is_unit() {
-                snippet_body.push(';');
-            }
-        },
+    if let ExprKind::Block(block, _) = match_body.kind {
+        // macro + expr_ty(body) == ()
+        if block.span.from_expansion() && cx.typeck_results().expr_ty(match_body).is_unit() {
+            snippet_body.push(';');
+        }
     }
 
     let mut applicability = Applicability::MaybeIncorrect;
diff --git a/tests/ui/match_single_binding.fixed b/tests/ui/match_single_binding.fixed
index a6e315e4773..6cfb6661a03 100644
--- a/tests/ui/match_single_binding.fixed
+++ b/tests/ui/match_single_binding.fixed
@@ -133,3 +133,16 @@ fn issue_9575() {
         println!("Needs curlies");
     };
 }
+
+#[allow(dead_code)]
+fn issue_9725(r: Option<u32>) {
+    let x = r;
+    match x {
+        Some(_) => {
+            println!("Some");
+        },
+        None => {
+            println!("None");
+        },
+    };
+}
diff --git a/tests/ui/match_single_binding.rs b/tests/ui/match_single_binding.rs
index cecbd703e56..f188aeb5f2f 100644
--- a/tests/ui/match_single_binding.rs
+++ b/tests/ui/match_single_binding.rs
@@ -148,3 +148,17 @@ fn issue_9575() {
         _ => println!("Needs curlies"),
     };
 }
+
+#[allow(dead_code)]
+fn issue_9725(r: Option<u32>) {
+    match r {
+        x => match x {
+            Some(_) => {
+                println!("Some");
+            },
+            None => {
+                println!("None");
+            },
+        },
+    };
+}
diff --git a/tests/ui/match_single_binding.stderr b/tests/ui/match_single_binding.stderr
index 2b9ec7ee702..e960d64ad2b 100644
--- a/tests/ui/match_single_binding.stderr
+++ b/tests/ui/match_single_binding.stderr
@@ -213,5 +213,30 @@ LL +         println!("Needs curlies");
 LL ~     };
    |
 
-error: aborting due to 14 previous errors
+error: this match could be written as a `let` statement
+  --> $DIR/match_single_binding.rs:154:5
+   |
+LL | /     match r {
+LL | |         x => match x {
+LL | |             Some(_) => {
+LL | |                 println!("Some");
+...  |
+LL | |         },
+LL | |     };
+   | |_____^
+   |
+help: consider using a `let` statement
+   |
+LL ~     let x = r;
+LL +     match x {
+LL +         Some(_) => {
+LL +             println!("Some");
+LL +         },
+LL +         None => {
+LL +             println!("None");
+LL +         },
+LL ~     };
+   |
+
+error: aborting due to 15 previous errors