about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPietro Albini <pietro@pietroalbini.org>2019-03-08 09:41:47 +0100
committerGitHub <noreply@github.com>2019-03-08 09:41:47 +0100
commit3005b4d9181793f59b0e9ca113bb3951e0dfa33f (patch)
tree6921e95737691138d0243814babf6a50cc338e99
parent4fbeb11374b36b2b7c4fce7b046c2fbe9905e838 (diff)
parentdf852c0d793eaf0c31124de6c0020d21c07591e3 (diff)
downloadrust-3005b4d9181793f59b0e9ca113bb3951e0dfa33f.tar.gz
rust-3005b4d9181793f59b0e9ca113bb3951e0dfa33f.zip
Rollup merge of #58883 - estebank:unused-closure-arg, r=varkor
Suggest appropriate code for unused field when destructuring pattern

Fix #56472.
-rw-r--r--src/librustc/middle/liveness.rs15
-rw-r--r--src/test/ui/suggestions/unused-closure-argument.rs20
-rw-r--r--src/test/ui/suggestions/unused-closure-argument.stderr20
3 files changed, 54 insertions, 1 deletions
diff --git a/src/librustc/middle/liveness.rs b/src/librustc/middle/liveness.rs
index 76933a6e348..97f747c94a4 100644
--- a/src/librustc/middle/liveness.rs
+++ b/src/librustc/middle/liveness.rs
@@ -379,9 +379,22 @@ fn visit_fn<'a, 'tcx: 'a>(ir: &mut IrMaps<'a, 'tcx>,
     let body = ir.tcx.hir().body(body_id);
 
     for arg in &body.arguments {
+        let is_shorthand = match arg.pat.node {
+            crate::hir::PatKind::Struct(..) => true,
+            _ => false,
+        };
         arg.pat.each_binding(|_bm, hir_id, _x, ident| {
             debug!("adding argument {:?}", hir_id);
-            fn_maps.add_variable(Arg(hir_id, ident.name));
+            let var = if is_shorthand {
+                Local(LocalInfo {
+                    id: hir_id,
+                    name: ident.name,
+                    is_shorthand: true,
+                })
+            } else {
+                Arg(hir_id, ident.name)
+            };
+            fn_maps.add_variable(var);
         })
     };
 
diff --git a/src/test/ui/suggestions/unused-closure-argument.rs b/src/test/ui/suggestions/unused-closure-argument.rs
new file mode 100644
index 00000000000..677003ebf22
--- /dev/null
+++ b/src/test/ui/suggestions/unused-closure-argument.rs
@@ -0,0 +1,20 @@
+#![deny(unused_variables)]
+
+struct Point {
+    x: i32,
+    y: i32,
+}
+
+fn main() {
+    let points = vec!(Point { x: 1, y: 2 }, Point { x: 3, y: 4 });
+
+    let _: i32 = points.iter()
+        .map(|Point { x, y }| y)
+        //~^ ERROR unused variable
+        .sum();
+
+    let _: i32 = points.iter()
+        .map(|x| 4)
+        //~^ ERROR unused variable
+        .sum();
+}
diff --git a/src/test/ui/suggestions/unused-closure-argument.stderr b/src/test/ui/suggestions/unused-closure-argument.stderr
new file mode 100644
index 00000000000..5cfdd79659b
--- /dev/null
+++ b/src/test/ui/suggestions/unused-closure-argument.stderr
@@ -0,0 +1,20 @@
+error: unused variable: `x`
+  --> $DIR/unused-closure-argument.rs:12:23
+   |
+LL |         .map(|Point { x, y }| y)
+   |                       ^ help: try ignoring the field: `x: _`
+   |
+note: lint level defined here
+  --> $DIR/unused-closure-argument.rs:1:9
+   |
+LL | #![deny(unused_variables)]
+   |         ^^^^^^^^^^^^^^^^
+
+error: unused variable: `x`
+  --> $DIR/unused-closure-argument.rs:17:15
+   |
+LL |         .map(|x| 4)
+   |               ^ help: consider prefixing with an underscore: `_x`
+
+error: aborting due to 2 previous errors
+