Skip to content

Commit fd28753

Browse files
committed
structured suggestions for single-use lifetime lint on fns and methods
It would be nice to demonstrate the shining correctness here with more run-rustfix tests than this, but unfortunately, that doesn't work with multipart suggestions yet (rust-lang#53934). While we're here, reword the zero-use lifetime suggestion to "elide the unused lifetime" instead of "remove it". (It's classier.)
1 parent 1982f18 commit fd28753

14 files changed

+168
-36
lines changed

src/librustc/middle/resolve_lifetime.rs

+102-17
Original file line numberDiff line numberDiff line change
@@ -1443,23 +1443,101 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
14431443
/// helper method to determine the span to remove when suggesting the
14441444
/// deletion of a lifetime
14451445
fn lifetime_deletion_span(&self, name: ast::Ident, generics: &hir::Generics) -> Option<Span> {
1446-
if generics.params.len() == 1 {
1447-
// if sole lifetime, remove the `<>` brackets
1448-
Some(generics.span)
1449-
} else {
1450-
generics.params.iter().enumerate().find_map(|(i, param)| {
1451-
if param.name.ident() == name {
1452-
// We also want to delete a leading or trailing comma
1453-
// as appropriate
1454-
if i >= generics.params.len() - 1 {
1455-
Some(generics.params[i - 1].span.shrink_to_hi().to(param.span))
1456-
} else {
1457-
Some(param.span.to(generics.params[i + 1].span.shrink_to_lo()))
1446+
generics.params.iter().enumerate().find_map(|(i, param)| {
1447+
if param.name.ident() == name {
1448+
let mut in_band = false;
1449+
if let hir::GenericParamKind::Lifetime { kind } = param.kind {
1450+
if let hir::LifetimeParamKind::InBand = kind {
1451+
in_band = true;
14581452
}
1453+
}
1454+
if in_band {
1455+
Some(param.span)
14591456
} else {
1460-
None
1457+
if generics.params.len() == 1 {
1458+
// if sole lifetime, remove the entire `<>` brackets
1459+
Some(generics.span)
1460+
} else {
1461+
// if removing within `<>` brackets, we also want to
1462+
// delete a leading or trailing comma as appropriate
1463+
if i >= generics.params.len() - 1 {
1464+
Some(generics.params[i - 1].span.shrink_to_hi().to(param.span))
1465+
} else {
1466+
Some(param.span.to(generics.params[i + 1].span.shrink_to_lo()))
1467+
}
1468+
}
14611469
}
1462-
})
1470+
} else {
1471+
None
1472+
}
1473+
})
1474+
}
1475+
1476+
// helper method to issue suggestions from `fn rah<'a>(&'a T)` to `fn rah(&T)`
1477+
fn suggest_eliding_single_use_lifetime(
1478+
&self, err: &mut DiagnosticBuilder<'_>, def_id: DefId, lifetime: &hir::Lifetime
1479+
) {
1480+
// FIXME: future work: also suggest `impl Foo<'_>` for `impl<'a> Foo<'a>`
1481+
let name = lifetime.name.ident();
1482+
let mut remove_decl = None;
1483+
if let Some(parent_def_id) = self.tcx.parent(def_id) {
1484+
if let Some(generics) = self.tcx.hir.get_generics(parent_def_id) {
1485+
remove_decl = self.lifetime_deletion_span(name, generics);
1486+
}
1487+
}
1488+
1489+
let mut remove_use = None;
1490+
let mut find_arg_use_span = |inputs: &hir::HirVec<hir::Ty>| {
1491+
for input in inputs {
1492+
if let hir::TyKind::Rptr(lt, _) = input.node {
1493+
if lt.name.ident() == name {
1494+
// include the trailing whitespace between the ampersand and the type name
1495+
let lt_through_ty_span = lifetime.span.to(input.span.shrink_to_hi());
1496+
remove_use = Some(
1497+
self.tcx.sess.source_map()
1498+
.span_until_non_whitespace(lt_through_ty_span)
1499+
);
1500+
break;
1501+
}
1502+
}
1503+
}
1504+
};
1505+
if let Node::Lifetime(hir_lifetime) = self.tcx.hir.get(lifetime.id) {
1506+
if let Some(parent) = self.tcx.hir.find(self.tcx.hir.get_parent(hir_lifetime.id)) {
1507+
match parent {
1508+
Node::Item(item) => {
1509+
if let hir::ItemKind::Fn(decl, _, _, _) = &item.node {
1510+
find_arg_use_span(&decl.inputs);
1511+
}
1512+
},
1513+
Node::ImplItem(impl_item) => {
1514+
if let hir::ImplItemKind::Method(sig, _) = &impl_item.node {
1515+
find_arg_use_span(&sig.decl.inputs);
1516+
}
1517+
}
1518+
_ => {}
1519+
}
1520+
}
1521+
}
1522+
1523+
if let (Some(decl_span), Some(use_span)) = (remove_decl, remove_use) {
1524+
// if both declaration and use deletion spans start at the same
1525+
// place ("start at" because the latter includes trailing
1526+
// whitespace), then this is an in-band lifetime
1527+
if decl_span.shrink_to_lo() == use_span.shrink_to_lo() {
1528+
err.span_suggestion_with_applicability(
1529+
use_span,
1530+
"elide the single-use lifetime",
1531+
String::new(),
1532+
Applicability::MachineApplicable,
1533+
);
1534+
} else {
1535+
err.multipart_suggestion_with_applicability(
1536+
"elide the single-use lifetime",
1537+
vec![(decl_span, String::new()), (use_span, String::new())],
1538+
Applicability::MachineApplicable,
1539+
);
1540+
}
14631541
}
14641542
}
14651543

@@ -1521,8 +1599,15 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
15211599
span,
15221600
&format!("lifetime parameter `{}` only used once", name),
15231601
);
1524-
err.span_label(span, "this lifetime...");
1525-
err.span_label(lifetime.span, "...is used only here");
1602+
1603+
if span == lifetime.span {
1604+
// spans are the same for in-band lifetime declarations
1605+
err.span_label(span, "this lifetime is only used here");
1606+
} else {
1607+
err.span_label(span, "this lifetime...");
1608+
err.span_label(lifetime.span, "...is used only here");
1609+
}
1610+
self.suggest_eliding_single_use_lifetime(&mut err, def_id, lifetime);
15261611
err.emit();
15271612
}
15281613
}
@@ -1555,7 +1640,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
15551640
if let Some(span) = unused_lt_span {
15561641
err.span_suggestion_with_applicability(
15571642
span,
1558-
"remove it",
1643+
"elide the unused lifetime",
15591644
String::new(),
15601645
Applicability::MachineApplicable,
15611646
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
11+
// run-rustfix
12+
13+
#![feature(in_band_lifetimes)]
14+
#![deny(single_use_lifetimes)]
15+
#![allow(dead_code)]
16+
#![allow(unused_variables)]
17+
18+
// Test that we DO warn when lifetime name is used only
19+
// once in a fn argument, even with in band lifetimes.
20+
21+
fn a(x: &u32, y: &u32) {
22+
//~^ ERROR `'a` only used once
23+
//~| ERROR `'b` only used once
24+
//~| HELP elide the single-use lifetime
25+
//~| HELP elide the single-use lifetime
26+
}
27+
28+
fn main() { }

src/test/ui/single-use-lifetime/one-use-in-fn-argument-in-band.rs

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// run-rustfix
12+
1113
#![feature(in_band_lifetimes)]
1214
#![deny(single_use_lifetimes)]
1315
#![allow(dead_code)]
@@ -19,6 +21,8 @@
1921
fn a(x: &'a u32, y: &'b u32) {
2022
//~^ ERROR `'a` only used once
2123
//~| ERROR `'b` only used once
24+
//~| HELP elide the single-use lifetime
25+
//~| HELP elide the single-use lifetime
2226
}
2327

2428
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
error: lifetime parameter `'a` only used once
2-
--> $DIR/one-use-in-fn-argument-in-band.rs:19:10
2+
--> $DIR/one-use-in-fn-argument-in-band.rs:21:10
33
|
44
LL | fn a(x: &'a u32, y: &'b u32) {
5-
| ^^
5+
| ^^-
66
| |
7-
| this lifetime...
8-
| ...is used only here
7+
| this lifetime is only used here
8+
| help: elide the single-use lifetime
99
|
1010
note: lint level defined here
11-
--> $DIR/one-use-in-fn-argument-in-band.rs:12:9
11+
--> $DIR/one-use-in-fn-argument-in-band.rs:14:9
1212
|
1313
LL | #![deny(single_use_lifetimes)]
1414
| ^^^^^^^^^^^^^^^^^^^^
1515

1616
error: lifetime parameter `'b` only used once
17-
--> $DIR/one-use-in-fn-argument-in-band.rs:19:22
17+
--> $DIR/one-use-in-fn-argument-in-band.rs:21:22
1818
|
1919
LL | fn a(x: &'a u32, y: &'b u32) {
20-
| ^^
20+
| ^^-
2121
| |
22-
| this lifetime...
23-
| ...is used only here
22+
| this lifetime is only used here
23+
| help: elide the single-use lifetime
2424

2525
error: aborting due to 2 previous errors
2626

src/test/ui/single-use-lifetime/one-use-in-fn-argument.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// once in a fn argument.
1717

1818
fn a<'a>(x: &'a u32) { //~ ERROR `'a` only used once
19+
//~^ HELP elide the single-use lifetime
1920
}
2021

2122
fn main() { }

src/test/ui/single-use-lifetime/one-use-in-fn-argument.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ note: lint level defined here
1111
|
1212
LL | #![deny(single_use_lifetimes)]
1313
| ^^^^^^^^^^^^^^^^^^^^
14+
help: elide the single-use lifetime
15+
|
16+
LL | fn a(x: &u32) { //~ ERROR `'a` only used once
17+
| -- --
1418

1519
error: aborting due to previous error
1620

src/test/ui/single-use-lifetime/one-use-in-inherent-method-argument.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct Foo<'f> {
2020

2121
impl<'f> Foo<'f> { //~ ERROR `'f` only used once
2222
fn inherent_a<'a>(&self, data: &'a u32) { //~ ERROR `'a` only used once
23+
//~^ HELP elide the single-use lifetime
2324
}
2425
}
2526

src/test/ui/single-use-lifetime/one-use-in-inherent-method-argument.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ note: lint level defined here
1111
|
1212
LL | #![deny(single_use_lifetimes)]
1313
| ^^^^^^^^^^^^^^^^^^^^
14+
help: elide the single-use lifetime
15+
|
16+
LL | fn inherent_a(&self, data: &u32) { //~ ERROR `'a` only used once
17+
| -- --
1418

1519
error: lifetime parameter `'f` only used once
1620
--> $DIR/one-use-in-inherent-method-argument.rs:21:6

src/test/ui/single-use-lifetime/one-use-in-trait-method-argument.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ impl<'f> Iterator for Foo<'f> {
2323
type Item = &'f u32;
2424

2525
fn next<'g>(&'g mut self) -> Option<Self::Item> { //~ ERROR `'g` only used once
26+
//~^ HELP elide the single-use lifetime
2627
None
2728
}
2829
}

src/test/ui/single-use-lifetime/one-use-in-trait-method-argument.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ note: lint level defined here
1111
|
1212
LL | #![deny(single_use_lifetimes)]
1313
| ^^^^^^^^^^^^^^^^^^^^
14+
help: elide the single-use lifetime
15+
|
16+
LL | fn next(&mut self) -> Option<Self::Item> { //~ ERROR `'g` only used once
17+
| ----
1418

1519
error: aborting due to previous error
1620

src/test/ui/single-use-lifetime/zero-uses-in-fn.fixed

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77

88
fn september() {}
99
//~^ ERROR lifetime parameter `'a` never used
10-
//~| HELP remove it
10+
//~| HELP elide the unused lifetime
1111

1212
fn october<'b, T>(s: &'b T) -> &'b T {
1313
//~^ ERROR lifetime parameter `'a` never used
14-
//~| HELP remove it
14+
//~| HELP elide the unused lifetime
1515
s
1616
}
1717

1818
fn november<'a>(s: &'a str) -> (&'a str) {
1919
//~^ ERROR lifetime parameter `'b` never used
20-
//~| HELP remove it
20+
//~| HELP elide the unused lifetime
2121
s
2222
}
2323

src/test/ui/single-use-lifetime/zero-uses-in-fn.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77

88
fn september<'a>() {}
99
//~^ ERROR lifetime parameter `'a` never used
10-
//~| HELP remove it
10+
//~| HELP elide the unused lifetime
1111

1212
fn october<'a, 'b, T>(s: &'b T) -> &'b T {
1313
//~^ ERROR lifetime parameter `'a` never used
14-
//~| HELP remove it
14+
//~| HELP elide the unused lifetime
1515
s
1616
}
1717

1818
fn november<'a, 'b>(s: &'a str) -> (&'a str) {
1919
//~^ ERROR lifetime parameter `'b` never used
20-
//~| HELP remove it
20+
//~| HELP elide the unused lifetime
2121
s
2222
}
2323

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: lifetime parameter `'a` never used
22
--> $DIR/zero-uses-in-fn.rs:8:14
33
|
44
LL | fn september<'a>() {}
5-
| -^^- help: remove it
5+
| -^^- help: elide the unused lifetime
66
|
77
note: lint level defined here
88
--> $DIR/zero-uses-in-fn.rs:5:9
@@ -16,15 +16,15 @@ error: lifetime parameter `'a` never used
1616
LL | fn october<'a, 'b, T>(s: &'b T) -> &'b T {
1717
| ^^--
1818
| |
19-
| help: remove it
19+
| help: elide the unused lifetime
2020

2121
error: lifetime parameter `'b` never used
2222
--> $DIR/zero-uses-in-fn.rs:18:17
2323
|
2424
LL | fn november<'a, 'b>(s: &'a str) -> (&'a str) {
2525
| --^^
2626
| |
27-
| help: remove it
27+
| help: elide the unused lifetime
2828

2929
error: aborting due to 3 previous errors
3030

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: lifetime parameter `'a` never used
22
--> $DIR/zero-uses-in-impl.rs:8:6
33
|
44
LL | impl<'a> Foo {} //~ ERROR `'a` never used
5-
| -^^- help: remove it
5+
| -^^- help: elide the unused lifetime
66
|
77
note: lint level defined here
88
--> $DIR/zero-uses-in-impl.rs:3:9

0 commit comments

Comments
 (0)