Skip to content

Commit 9c3fa4d

Browse files
committed
Point at signature on unused lint
1 parent 14039a4 commit 9c3fa4d

File tree

5 files changed

+102
-16
lines changed

5 files changed

+102
-16
lines changed

src/librustc/middle/dead.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,22 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
553553

554554
fn visit_item(&mut self, item: &'tcx hir::Item) {
555555
if self.should_warn_about_item(item) {
556+
// For items that have a definition with a signature followed by a
557+
// block, point only at the signature.
558+
let span = match item.node {
559+
hir::ItemFn(..) |
560+
hir::ItemMod(..) |
561+
hir::ItemEnum(..) |
562+
hir::ItemStruct(..) |
563+
hir::ItemUnion(..) |
564+
hir::ItemTrait(..) |
565+
hir::ItemDefaultImpl(..) |
566+
hir::ItemImpl(..) => self.tcx.sess.codemap().def_span(item.span),
567+
_ => item.span,
568+
};
556569
self.warn_dead_code(
557570
item.id,
558-
item.span,
571+
span,
559572
item.name,
560573
item.node.descriptive_variant()
561574
);
@@ -570,8 +583,7 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
570583
g: &'tcx hir::Generics,
571584
id: ast::NodeId) {
572585
if self.should_warn_about_variant(&variant.node) {
573-
self.warn_dead_code(variant.node.data.id(), variant.span,
574-
variant.node.name, "variant");
586+
self.warn_dead_code(variant.node.data.id(), variant.span, variant.node.name, "variant");
575587
} else {
576588
intravisit::walk_variant(self, variant, g, id);
577589
}
@@ -596,15 +608,17 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
596608
match impl_item.node {
597609
hir::ImplItemKind::Const(_, body_id) => {
598610
if !self.symbol_is_live(impl_item.id, None) {
599-
self.warn_dead_code(impl_item.id, impl_item.span,
600-
impl_item.name, "associated const");
611+
self.warn_dead_code(impl_item.id,
612+
impl_item.span,
613+
impl_item.name,
614+
"associated const");
601615
}
602616
self.visit_nested_body(body_id)
603617
}
604618
hir::ImplItemKind::Method(_, body_id) => {
605619
if !self.symbol_is_live(impl_item.id, None) {
606-
self.warn_dead_code(impl_item.id, impl_item.span,
607-
impl_item.name, "method");
620+
let span = self.tcx.sess.codemap().def_span(impl_item.span);
621+
self.warn_dead_code(impl_item.id, span, impl_item.name, "method");
608622
}
609623
self.visit_nested_body(body_id)
610624
}

src/test/ui-fulldeps/lint-plugin-cmdline-allow.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ warning: function is never used: `lintme`
22
--> $DIR/lint-plugin-cmdline-allow.rs:20:1
33
|
44
20 | fn lintme() { }
5-
| ^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^
66
|
77
note: lint level defined here
88
--> $DIR/lint-plugin-cmdline-allow.rs:17:9

src/test/ui/path-lookahead.stderr

+4-8
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ warning: unnecessary parentheses around `return` value
99
warning: function is never used: `with_parens`
1010
--> $DIR/path-lookahead.rs:17:1
1111
|
12-
17 | / fn with_parens<T: ToString>(arg: T) -> String { //~WARN function is never used: `with_parens`
13-
18 | | return (<T as ToString>::to_string(&arg)); //~WARN unnecessary parentheses around `return` value
14-
19 | | }
15-
| |_^
12+
17 | fn with_parens<T: ToString>(arg: T) -> String { //~WARN function is never used: `with_parens`
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1614
|
1715
note: lint level defined here
1816
--> $DIR/path-lookahead.rs:13:9
@@ -24,8 +22,6 @@ note: lint level defined here
2422
warning: function is never used: `no_parens`
2523
--> $DIR/path-lookahead.rs:21:1
2624
|
27-
21 | / fn no_parens<T: ToString>(arg: T) -> String { //~WARN function is never used: `no_parens`
28-
22 | | return <T as ToString>::to_string(&arg);
29-
23 | | }
30-
| |_^
25+
21 | fn no_parens<T: ToString>(arg: T) -> String { //~WARN function is never used: `no_parens`
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3127

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// run-pass
12+
13+
#![warn(unused)]
14+
15+
enum Enum {
16+
A,
17+
B,
18+
C,
19+
D,
20+
}
21+
22+
struct Struct {
23+
a: usize,
24+
b: usize,
25+
c: usize,
26+
d: usize,
27+
}
28+
29+
fn func() -> usize {
30+
3
31+
}
32+
33+
fn
34+
func_complete_span()
35+
-> usize
36+
{
37+
3
38+
}
39+
40+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
warning: enum is never used: `Enum`
2+
--> $DIR/unused-warning-point-at-signature.rs:15:1
3+
|
4+
15 | enum Enum {
5+
| ^^^^^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/unused-warning-point-at-signature.rs:13:9
9+
|
10+
13 | #![warn(unused)]
11+
| ^^^^^^
12+
= note: #[warn(dead_code)] implied by #[warn(unused)]
13+
14+
warning: struct is never used: `Struct`
15+
--> $DIR/unused-warning-point-at-signature.rs:22:1
16+
|
17+
22 | struct Struct {
18+
| ^^^^^^^^^^^^^
19+
20+
warning: function is never used: `func`
21+
--> $DIR/unused-warning-point-at-signature.rs:29:1
22+
|
23+
29 | fn func() -> usize {
24+
| ^^^^^^^^^^^^^^^^^^
25+
26+
warning: function is never used: `func_complete_span`
27+
--> $DIR/unused-warning-point-at-signature.rs:33:1
28+
|
29+
33 | / fn
30+
34 | | func_complete_span()
31+
35 | | -> usize
32+
36 | | {
33+
37 | | 3
34+
38 | | }
35+
| |_^
36+

0 commit comments

Comments
 (0)