about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2021-02-24 18:08:37 -0800
committerEsteban Küber <esteban@kuber.com.ar>2021-02-24 18:08:37 -0800
commitfb24a10ad39e2ed1a312c015b926cdb52ad1cef9 (patch)
tree89a79b0a7b955663c4fa4701815d9b2486f8cbfe
parenta8486b64b0c87dabd045453b6c81500015d122d6 (diff)
downloadrust-fb24a10ad39e2ed1a312c015b926cdb52ad1cef9.tar.gz
rust-fb24a10ad39e2ed1a312c015b926cdb52ad1cef9.zip
Properly account for non-shorthand pattern field in unused variable lint
Fix #82488
-rw-r--r--compiler/rustc_passes/src/liveness.rs15
-rw-r--r--src/test/ui/lint/unused_variables-issue-82488.fixed16
-rw-r--r--src/test/ui/lint/unused_variables-issue-82488.rs16
-rw-r--r--src/test/ui/lint/unused_variables-issue-82488.stderr14
4 files changed, 56 insertions, 5 deletions
diff --git a/compiler/rustc_passes/src/liveness.rs b/compiler/rustc_passes/src/liveness.rs
index c11dc231d48..82f19770a12 100644
--- a/compiler/rustc_passes/src/liveness.rs
+++ b/compiler/rustc_passes/src/liveness.rs
@@ -367,12 +367,17 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
     }
 
     fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
-        let is_shorthand = matches!(param.pat.kind, rustc_hir::PatKind::Struct(..));
         param.pat.each_binding(|_bm, hir_id, _x, ident| {
-            let var = if is_shorthand {
-                Local(LocalInfo { id: hir_id, name: ident.name, is_shorthand: true })
-            } else {
-                Param(hir_id, ident.name)
+            let var = match param.pat.kind {
+                rustc_hir::PatKind::Struct(_, fields, _) => Local(LocalInfo {
+                    id: hir_id,
+                    name: ident.name,
+                    is_shorthand: fields
+                        .iter()
+                        .find(|f| f.ident == ident)
+                        .map_or(false, |f| f.is_shorthand),
+                }),
+                _ => Param(hir_id, ident.name),
             };
             self.add_variable(var);
         });
diff --git a/src/test/ui/lint/unused_variables-issue-82488.fixed b/src/test/ui/lint/unused_variables-issue-82488.fixed
new file mode 100644
index 00000000000..3cb2c90d0d3
--- /dev/null
+++ b/src/test/ui/lint/unused_variables-issue-82488.fixed
@@ -0,0 +1,16 @@
+// run-rustfix
+#![deny(unused_variables)]
+
+struct Point {
+    x: u32,
+    y: u32
+}
+
+fn process_point(Point { x, y: _renamed }: Point) {
+//~^ ERROR unused variable: `renamed`
+    let _ = x;
+}
+
+fn main() {
+    process_point(Point { x: 0, y: 0 });
+}
diff --git a/src/test/ui/lint/unused_variables-issue-82488.rs b/src/test/ui/lint/unused_variables-issue-82488.rs
new file mode 100644
index 00000000000..007b0799bbb
--- /dev/null
+++ b/src/test/ui/lint/unused_variables-issue-82488.rs
@@ -0,0 +1,16 @@
+// run-rustfix
+#![deny(unused_variables)]
+
+struct Point {
+    x: u32,
+    y: u32
+}
+
+fn process_point(Point { x, y: renamed }: Point) {
+//~^ ERROR unused variable: `renamed`
+    let _ = x;
+}
+
+fn main() {
+    process_point(Point { x: 0, y: 0 });
+}
diff --git a/src/test/ui/lint/unused_variables-issue-82488.stderr b/src/test/ui/lint/unused_variables-issue-82488.stderr
new file mode 100644
index 00000000000..dce03a0f738
--- /dev/null
+++ b/src/test/ui/lint/unused_variables-issue-82488.stderr
@@ -0,0 +1,14 @@
+error: unused variable: `renamed`
+  --> $DIR/unused_variables-issue-82488.rs:9:32
+   |
+LL | fn process_point(Point { x, y: renamed }: Point) {
+   |                                ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_renamed`
+   |
+note: the lint level is defined here
+  --> $DIR/unused_variables-issue-82488.rs:2:9
+   |
+LL | #![deny(unused_variables)]
+   |         ^^^^^^^^^^^^^^^^
+
+error: aborting due to previous error
+