about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-08-15 03:21:47 +0000
committerbors <bors@rust-lang.org>2023-08-15 03:21:47 +0000
commitd7e751006cb3691d1384b74196a9cb45447acfa8 (patch)
tree5adf3ef635340f054eb909666885c41edba73774
parentffaa32b7b646c208f20c827655bb98ff9868852e (diff)
parentc44b35e1c37f819327861b9f4d77e1469b33f457 (diff)
downloadrust-d7e751006cb3691d1384b74196a9cb45447acfa8.tar.gz
rust-d7e751006cb3691d1384b74196a9cb45447acfa8.zip
Auto merge of #113679 - chenyukang:yukang-fix-lint-113459, r=cjgillot
Match scrutinee need necessary parentheses for structs

Fixes #113459
-rw-r--r--compiler/rustc_lint/src/unused.rs18
-rw-r--r--tests/ui/lint/lint-struct-necessary.rs31
-rw-r--r--tests/ui/lint/lint-struct-necessary.stderr19
3 files changed, 68 insertions, 0 deletions
diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs
index 3db6b302790..6041f80753b 100644
--- a/compiler/rustc_lint/src/unused.rs
+++ b/compiler/rustc_lint/src/unused.rs
@@ -666,6 +666,24 @@ trait UnusedDelimLint {
         if !followed_by_block {
             return false;
         }
+
+        // Check if we need parens for `match &( Struct { feild:  }) {}`.
+        {
+            let mut innermost = inner;
+            loop {
+                innermost = match &innermost.kind {
+                    ExprKind::AddrOf(_, _, expr) => expr,
+                    _ => {
+                        if parser::contains_exterior_struct_lit(&innermost) {
+                            return true;
+                        } else {
+                            break;
+                        }
+                    }
+                }
+            }
+        }
+
         let mut innermost = inner;
         loop {
             innermost = match &innermost.kind {
diff --git a/tests/ui/lint/lint-struct-necessary.rs b/tests/ui/lint/lint-struct-necessary.rs
new file mode 100644
index 00000000000..8bc3c12054a
--- /dev/null
+++ b/tests/ui/lint/lint-struct-necessary.rs
@@ -0,0 +1,31 @@
+#![allow(dead_code)]
+#![deny(unused_parens)]
+
+enum State {
+    Waiting { start_at: u64 }
+}
+struct Foo {}
+
+fn main() {
+    let e = &mut State::Waiting { start_at: 0u64 };
+    match (&mut State::Waiting { start_at: 0u64 }) {
+        _ => {}
+    }
+
+    match (e) {
+        //~^ ERROR unnecessary parentheses around `match` scrutinee expression
+        _ => {}
+    }
+
+    match &(State::Waiting { start_at: 0u64 }) {
+        _ => {}
+    }
+
+    match (State::Waiting { start_at: 0u64 }) {
+        _ => {}
+    }
+
+    match (&&Foo {}) {
+        _ => {}
+    }
+}
diff --git a/tests/ui/lint/lint-struct-necessary.stderr b/tests/ui/lint/lint-struct-necessary.stderr
new file mode 100644
index 00000000000..eb65a9e98c6
--- /dev/null
+++ b/tests/ui/lint/lint-struct-necessary.stderr
@@ -0,0 +1,19 @@
+error: unnecessary parentheses around `match` scrutinee expression
+  --> $DIR/lint-struct-necessary.rs:15:11
+   |
+LL |     match (e) {
+   |           ^ ^
+   |
+note: the lint level is defined here
+  --> $DIR/lint-struct-necessary.rs:2:9
+   |
+LL | #![deny(unused_parens)]
+   |         ^^^^^^^^^^^^^
+help: remove these parentheses
+   |
+LL -     match (e) {
+LL +     match e {
+   |
+
+error: aborting due to previous error
+