@@ -19,9 +19,9 @@ use hir::def::Def;
1919use hir:: def_id:: { CrateNum , DefId , LocalDefId , LOCAL_CRATE } ;
2020use hir:: map:: Map ;
2121use 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 } ;
2525use rustc:: lint;
2626use rustc_data_structures:: sync:: Lrc ;
2727use 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 }
0 commit comments