Skip to content

Commit b1b684e

Browse files
committed
structured suggestions for unused-lifetimes lint
1 parent b9adc33 commit b1b684e

File tree

6 files changed

+124
-41
lines changed

6 files changed

+124
-41
lines changed

src/librustc/middle/resolve_lifetime.rs

+53-4
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ use hir::def::Def;
1919
use hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
2020
use hir::map::Map;
2121
use hir::{GenericArg, GenericParam, ItemLocalId, LifetimeName, ParamName, Node};
22-
use ty::{self, TyCtxt, GenericParamDefKind};
22+
use ty::{self, TyCtxt, DefIdTree, GenericParamDefKind};
2323

24-
use errors::DiagnosticBuilder;
24+
use errors::{Applicability, DiagnosticBuilder};
2525
use rustc::lint;
2626
use rustc_data_structures::sync::Lrc;
2727
use session::Session;
@@ -1468,12 +1468,61 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
14681468
_ => None,
14691469
} {
14701470
debug!("id ={:?} span = {:?} name = {:?}", node_id, span, name);
1471-
self.tcx.struct_span_lint_node(
1471+
let mut err = self.tcx.struct_span_lint_node(
14721472
lint::builtin::UNUSED_LIFETIMES,
14731473
id,
14741474
span,
14751475
&format!("lifetime parameter `{}` never used", name)
1476-
).emit();
1476+
);
1477+
if let Some(parent_def_id) = self.tcx.parent(def_id) {
1478+
if let Some(node_id) = self.tcx.hir.as_local_node_id(parent_def_id) {
1479+
if let Some(Node::Item(hir_item)) = self.tcx.hir.find(node_id) {
1480+
match hir_item.node {
1481+
hir::ItemKind::Fn(_, _, ref generics, _) |
1482+
hir::ItemKind::Impl(_, _, _, ref generics, _, _, _) => {
1483+
let unused_lt_span = if generics.params.len() == 1 {
1484+
// if sole lifetime, remove the `<>` brackets
1485+
Some(generics.span)
1486+
} else {
1487+
generics.params.iter().enumerate()
1488+
.find_map(|(i, param)| {
1489+
if param.name.ident() == name {
1490+
// We also want to delete a leading or
1491+
// trailing comma as appropriate
1492+
if i >= generics.params.len() - 1 {
1493+
Some(
1494+
generics.params[i-1]
1495+
.span.shrink_to_hi()
1496+
.to(param.span)
1497+
)
1498+
} else {
1499+
Some(
1500+
param.span.to(
1501+
generics.params[i+1]
1502+
.span.shrink_to_lo()
1503+
)
1504+
)
1505+
}
1506+
} else {
1507+
None
1508+
}
1509+
})
1510+
};
1511+
if let Some(span) = unused_lt_span {
1512+
err.span_suggestion_with_applicability(
1513+
span,
1514+
"remove it",
1515+
String::new(),
1516+
Applicability::MachineApplicable
1517+
);
1518+
}
1519+
},
1520+
_ => {}
1521+
}
1522+
}
1523+
}
1524+
}
1525+
err.emit();
14771526
}
14781527
}
14791528
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// run-rustfix
2+
3+
// Test that we DO warn when lifetime name is not used at all.
4+
5+
#![deny(unused_lifetimes)]
6+
#![allow(dead_code, unused_variables)]
7+
8+
fn september() {}
9+
//~^ ERROR lifetime parameter `'a` never used
10+
//~| HELP remove it
11+
12+
fn october<'b, T>(s: &'b T) -> &'b T {
13+
//~^ ERROR lifetime parameter `'a` never used
14+
//~| HELP remove it
15+
s
16+
}
17+
18+
fn november<'a>(s: &'a str) -> (&'a str) {
19+
//~^ ERROR lifetime parameter `'b` never used
20+
//~| HELP remove it
21+
s
22+
}
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
// Copyright 2016 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.
1+
// run-rustfix
102

113
// Test that we DO warn when lifetime name is not used at all.
124

135
#![deny(unused_lifetimes)]
14-
#![allow(dead_code)]
15-
#![allow(unused_variables)]
6+
#![allow(dead_code, unused_variables)]
167

17-
fn d<'a>() { } //~ ERROR `'a` never used
8+
fn september<'a>() {}
9+
//~^ ERROR lifetime parameter `'a` never used
10+
//~| HELP remove it
1811

19-
fn main() { }
12+
fn october<'a, 'b, T>(s: &'b T) -> &'b T {
13+
//~^ ERROR lifetime parameter `'a` never used
14+
//~| HELP remove it
15+
s
16+
}
17+
18+
fn november<'a, 'b>(s: &'a str) -> (&'a str) {
19+
//~^ ERROR lifetime parameter `'b` never used
20+
//~| HELP remove it
21+
s
22+
}
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
error: lifetime parameter `'a` never used
2-
--> $DIR/zero-uses-in-fn.rs:17:6
2+
--> $DIR/zero-uses-in-fn.rs:8:14
33
|
4-
LL | fn d<'a>() { } //~ ERROR `'a` never used
5-
| ^^
4+
LL | fn september<'a>() {}
5+
| -^^- help: remove it
66
|
77
note: lint level defined here
8-
--> $DIR/zero-uses-in-fn.rs:13:9
8+
--> $DIR/zero-uses-in-fn.rs:5:9
99
|
1010
LL | #![deny(unused_lifetimes)]
1111
| ^^^^^^^^^^^^^^^^
1212

13-
error: aborting due to previous error
13+
error: lifetime parameter `'a` never used
14+
--> $DIR/zero-uses-in-fn.rs:12:12
15+
|
16+
LL | fn october<'a, 'b, T>(s: &'b T) -> &'b T {
17+
| ^^--
18+
| |
19+
| help: remove it
20+
21+
error: lifetime parameter `'b` never used
22+
--> $DIR/zero-uses-in-fn.rs:18:17
23+
|
24+
LL | fn november<'a, 'b>(s: &'a str) -> (&'a str) {
25+
| --^^
26+
| |
27+
| help: remove it
28+
29+
error: aborting due to 3 previous errors
1430

Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
1-
// Copyright 2016 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-
111
// Test that we DO warn when lifetime name is not used at all.
122

133
#![deny(unused_lifetimes)]
14-
#![allow(dead_code)]
15-
#![allow(unused_variables)]
4+
#![allow(dead_code, unused_variables)]
165

17-
struct Foo { }
6+
struct Foo {}
187

19-
impl<'a> Foo { } //~ ERROR `'a` never used
8+
impl<'a> Foo {} //~ ERROR `'a` never used
209

21-
fn main() { }
10+
fn main() {}

src/test/ui/single-use-lifetime/zero-uses-in-impl.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: lifetime parameter `'a` never used
2-
--> $DIR/zero-uses-in-impl.rs:19:6
2+
--> $DIR/zero-uses-in-impl.rs:8:6
33
|
4-
LL | impl<'a> Foo { } //~ ERROR `'a` never used
5-
| ^^
4+
LL | impl<'a> Foo {} //~ ERROR `'a` never used
5+
| -^^- help: remove it
66
|
77
note: lint level defined here
8-
--> $DIR/zero-uses-in-impl.rs:13:9
8+
--> $DIR/zero-uses-in-impl.rs:3:9
99
|
1010
LL | #![deny(unused_lifetimes)]
1111
| ^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)