@@ -19,9 +19,9 @@ use hir::def::Def;
19
19
use hir:: def_id:: { CrateNum , DefId , LocalDefId , LOCAL_CRATE } ;
20
20
use hir:: map:: Map ;
21
21
use hir:: { GenericArg , GenericParam , ItemLocalId , LifetimeName , ParamName , Node } ;
22
- use ty:: { self , TyCtxt , GenericParamDefKind } ;
22
+ use ty:: { self , TyCtxt , DefIdTree , GenericParamDefKind } ;
23
23
24
- use errors:: DiagnosticBuilder ;
24
+ use errors:: { Applicability , DiagnosticBuilder } ;
25
25
use rustc:: lint;
26
26
use rustc_data_structures:: sync:: Lrc ;
27
27
use session:: Session ;
@@ -1468,12 +1468,61 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
1468
1468
_ => None ,
1469
1469
} {
1470
1470
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 (
1472
1472
lint:: builtin:: UNUSED_LIFETIMES ,
1473
1473
id,
1474
1474
span,
1475
1475
& 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 ( ) ;
1477
1526
}
1478
1527
}
1479
1528
}
0 commit comments