about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-09-29 07:05:10 +0000
committerbors <bors@rust-lang.org>2017-09-29 07:05:10 +0000
commit0253d98382fa351d53880605a346d0ea2e941a07 (patch)
tree5c37fb31890fd3fc19861124cf9f2a7bb8599822
parenta379780fb667714bf2f8abeb93cebeaf3c8c3672 (diff)
parent9c3fa4d3ef46317dc812dbd43c8c43355dc7c726 (diff)
downloadrust-0253d98382fa351d53880605a346d0ea2e941a07.tar.gz
rust-0253d98382fa351d53880605a346d0ea2e941a07.zip
Auto merge of #44847 - estebank:unused-signature, r=nikomatsakis
Point at signature on unused lint

```
warning: struct is never used: `Struct`
  --> $DIR/unused-warning-point-at-signature.rs:22:1
   |
22 | struct Struct {
   | ^^^^^^^^^^^^^
```

Fix #33961.
-rw-r--r--src/librustc/middle/dead.rs28
-rw-r--r--src/test/ui-fulldeps/lint-plugin-cmdline-allow.stderr2
-rw-r--r--src/test/ui/path-lookahead.stderr12
-rw-r--r--src/test/ui/span/unused-warning-point-at-signature.rs40
-rw-r--r--src/test/ui/span/unused-warning-point-at-signature.stderr36
5 files changed, 102 insertions, 16 deletions
diff --git a/src/librustc/middle/dead.rs b/src/librustc/middle/dead.rs
index 66a425a2d47..a9d9f6f28ec 100644
--- a/src/librustc/middle/dead.rs
+++ b/src/librustc/middle/dead.rs
@@ -553,9 +553,22 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
 
     fn visit_item(&mut self, item: &'tcx hir::Item) {
         if self.should_warn_about_item(item) {
+            // For items that have a definition with a signature followed by a
+            // block, point only at the signature.
+            let span = match item.node {
+                hir::ItemFn(..) |
+                hir::ItemMod(..) |
+                hir::ItemEnum(..) |
+                hir::ItemStruct(..) |
+                hir::ItemUnion(..) |
+                hir::ItemTrait(..) |
+                hir::ItemDefaultImpl(..) |
+                hir::ItemImpl(..) => self.tcx.sess.codemap().def_span(item.span),
+                _ => item.span,
+            };
             self.warn_dead_code(
                 item.id,
-                item.span,
+                span,
                 item.name,
                 item.node.descriptive_variant()
             );
@@ -570,8 +583,7 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
                      g: &'tcx hir::Generics,
                      id: ast::NodeId) {
         if self.should_warn_about_variant(&variant.node) {
-            self.warn_dead_code(variant.node.data.id(), variant.span,
-                                variant.node.name, "variant");
+            self.warn_dead_code(variant.node.data.id(), variant.span, variant.node.name, "variant");
         } else {
             intravisit::walk_variant(self, variant, g, id);
         }
@@ -596,15 +608,17 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
         match impl_item.node {
             hir::ImplItemKind::Const(_, body_id) => {
                 if !self.symbol_is_live(impl_item.id, None) {
-                    self.warn_dead_code(impl_item.id, impl_item.span,
-                                        impl_item.name, "associated const");
+                    self.warn_dead_code(impl_item.id,
+                                        impl_item.span,
+                                        impl_item.name,
+                                        "associated const");
                 }
                 self.visit_nested_body(body_id)
             }
             hir::ImplItemKind::Method(_, body_id) => {
                 if !self.symbol_is_live(impl_item.id, None) {
-                    self.warn_dead_code(impl_item.id, impl_item.span,
-                                        impl_item.name, "method");
+                    let span = self.tcx.sess.codemap().def_span(impl_item.span);
+                    self.warn_dead_code(impl_item.id, span, impl_item.name, "method");
                 }
                 self.visit_nested_body(body_id)
             }
diff --git a/src/test/ui-fulldeps/lint-plugin-cmdline-allow.stderr b/src/test/ui-fulldeps/lint-plugin-cmdline-allow.stderr
index 7c9c4e99039..0cc3a5b6bf3 100644
--- a/src/test/ui-fulldeps/lint-plugin-cmdline-allow.stderr
+++ b/src/test/ui-fulldeps/lint-plugin-cmdline-allow.stderr
@@ -2,7 +2,7 @@ warning: function is never used: `lintme`
   --> $DIR/lint-plugin-cmdline-allow.rs:20:1
    |
 20 | fn lintme() { }
-   | ^^^^^^^^^^^^^^^
+   | ^^^^^^^^^^^
    |
 note: lint level defined here
   --> $DIR/lint-plugin-cmdline-allow.rs:17:9
diff --git a/src/test/ui/path-lookahead.stderr b/src/test/ui/path-lookahead.stderr
index 9936a1eb81e..1d4ab35046b 100644
--- a/src/test/ui/path-lookahead.stderr
+++ b/src/test/ui/path-lookahead.stderr
@@ -9,10 +9,8 @@ warning: unnecessary parentheses around `return` value
 warning: function is never used: `with_parens`
   --> $DIR/path-lookahead.rs:17:1
    |
-17 | / fn with_parens<T: ToString>(arg: T) -> String { //~WARN function is never used: `with_parens`
-18 | |   return (<T as ToString>::to_string(&arg)); //~WARN unnecessary parentheses around `return` value
-19 | | }
-   | |_^
+17 | fn with_parens<T: ToString>(arg: T) -> String { //~WARN function is never used: `with_parens`
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
 note: lint level defined here
   --> $DIR/path-lookahead.rs:13:9
@@ -24,8 +22,6 @@ note: lint level defined here
 warning: function is never used: `no_parens`
   --> $DIR/path-lookahead.rs:21:1
    |
-21 | / fn no_parens<T: ToString>(arg: T) -> String { //~WARN function is never used: `no_parens`
-22 | |   return <T as ToString>::to_string(&arg);
-23 | | }
-   | |_^
+21 | fn no_parens<T: ToString>(arg: T) -> String { //~WARN function is never used: `no_parens`
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
diff --git a/src/test/ui/span/unused-warning-point-at-signature.rs b/src/test/ui/span/unused-warning-point-at-signature.rs
new file mode 100644
index 00000000000..eb659d08da0
--- /dev/null
+++ b/src/test/ui/span/unused-warning-point-at-signature.rs
@@ -0,0 +1,40 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// run-pass
+
+#![warn(unused)]
+
+enum Enum {
+    A,
+    B,
+    C,
+    D,
+}
+
+struct Struct {
+    a: usize,
+    b: usize,
+    c: usize,
+    d: usize,
+}
+
+fn func() -> usize {
+    3
+}
+
+fn
+func_complete_span()
+-> usize
+{
+    3
+}
+
+fn main() {}
diff --git a/src/test/ui/span/unused-warning-point-at-signature.stderr b/src/test/ui/span/unused-warning-point-at-signature.stderr
new file mode 100644
index 00000000000..8e658670e9c
--- /dev/null
+++ b/src/test/ui/span/unused-warning-point-at-signature.stderr
@@ -0,0 +1,36 @@
+warning: enum is never used: `Enum`
+  --> $DIR/unused-warning-point-at-signature.rs:15:1
+   |
+15 | enum Enum {
+   | ^^^^^^^^^
+   |
+note: lint level defined here
+  --> $DIR/unused-warning-point-at-signature.rs:13:9
+   |
+13 | #![warn(unused)]
+   |         ^^^^^^
+   = note: #[warn(dead_code)] implied by #[warn(unused)]
+
+warning: struct is never used: `Struct`
+  --> $DIR/unused-warning-point-at-signature.rs:22:1
+   |
+22 | struct Struct {
+   | ^^^^^^^^^^^^^
+
+warning: function is never used: `func`
+  --> $DIR/unused-warning-point-at-signature.rs:29:1
+   |
+29 | fn func() -> usize {
+   | ^^^^^^^^^^^^^^^^^^
+
+warning: function is never used: `func_complete_span`
+  --> $DIR/unused-warning-point-at-signature.rs:33:1
+   |
+33 | / fn
+34 | | func_complete_span()
+35 | | -> usize
+36 | | {
+37 | |     3
+38 | | }
+   | |_^
+