Skip to content

Commit efd7a31

Browse files
committed
in which rightward drift is opposed
Thanks to reviewers Tyler Mandry (for pointing out that this is ridiculous and we need a helper function), Niko Matsakis (for pointing out that the span-calculation code only has a couple free variables), and Esteban Küber (for pointing out `get_generics`).
1 parent b1b684e commit efd7a31

File tree

1 file changed

+33
-44
lines changed

1 file changed

+33
-44
lines changed

src/librustc/middle/resolve_lifetime.rs

+33-44
Original file line numberDiff line numberDiff line change
@@ -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,
@@ -1475,50 +1499,15 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
14751499
&format!("lifetime parameter `{}` never used", name)
14761500
);
14771501
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-
}
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+
);
15221511
}
15231512
}
15241513
}

0 commit comments

Comments
 (0)