Skip to content

Commit b1a137d

Browse files
committed
Auto merge of #54686 - zackmdavis:zero_life, r=nikomatsakis
structured suggestions for unused-lifetimes lint Regretfully, resolve_lifetime.rs is suffering from a bit of rightward-drift, but ![zero_life](https://user-images.githubusercontent.com/1076988/46253407-010e7880-c430-11e8-8faf-b9afc1405415.png) r? @nikomatsakis
2 parents 423d810 + efd7a31 commit b1a137d

File tree

6 files changed

+113
-41
lines changed

6 files changed

+113
-41
lines changed

src/librustc/middle/resolve_lifetime.rs

+42-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;
@@ -1398,6 +1398,30 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
13981398
self.xcrate_object_lifetime_defaults = this.xcrate_object_lifetime_defaults;
13991399
}
14001400

1401+
/// helper method to determine the span to remove when suggesting the
1402+
/// deletion of a lifetime
1403+
fn lifetime_deletion_span(&self, name: ast::Ident, generics: &hir::Generics) -> Option<Span> {
1404+
if generics.params.len() == 1 {
1405+
// if sole lifetime, remove the `<>` brackets
1406+
Some(generics.span)
1407+
} else {
1408+
generics.params.iter().enumerate()
1409+
.find_map(|(i, param)| {
1410+
if param.name.ident() == name {
1411+
// We also want to delete a leading or trailing comma
1412+
// as appropriate
1413+
if i >= generics.params.len() - 1 {
1414+
Some(generics.params[i-1].span.shrink_to_hi().to(param.span))
1415+
} else {
1416+
Some(param.span.to(generics.params[i+1].span.shrink_to_lo()))
1417+
}
1418+
} else {
1419+
None
1420+
}
1421+
})
1422+
}
1423+
}
1424+
14011425
fn check_uses_for_lifetimes_defined_by_scope(&mut self) {
14021426
let defined_by = match self.scope {
14031427
Scope::Binder { lifetimes, .. } => lifetimes,
@@ -1468,12 +1492,26 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
14681492
_ => None,
14691493
} {
14701494
debug!("id ={:?} span = {:?} name = {:?}", node_id, span, name);
1471-
self.tcx.struct_span_lint_node(
1495+
let mut err = self.tcx.struct_span_lint_node(
14721496
lint::builtin::UNUSED_LIFETIMES,
14731497
id,
14741498
span,
14751499
&format!("lifetime parameter `{}` never used", name)
1476-
).emit();
1500+
);
1501+
if let Some(parent_def_id) = self.tcx.parent(def_id) {
1502+
if let Some(generics) = self.tcx.hir.get_generics(parent_def_id) {
1503+
let unused_lt_span = self.lifetime_deletion_span(name, generics);
1504+
if let Some(span) = unused_lt_span {
1505+
err.span_suggestion_with_applicability(
1506+
span,
1507+
"remove it",
1508+
String::new(),
1509+
Applicability::MachineApplicable
1510+
);
1511+
}
1512+
}
1513+
}
1514+
err.emit();
14771515
}
14781516
}
14791517
}
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)