@@ -477,6 +477,48 @@ pub enum BuiltinLintDiagnostics {
477
477
RedundantImport ( Vec < ( Span , bool ) > , ast:: Ident ) ,
478
478
}
479
479
480
+ pub ( crate ) fn add_elided_lifetime_in_path_suggestion (
481
+ sess : & Session ,
482
+ db : & mut DiagnosticBuilder < ' _ > ,
483
+ n : usize ,
484
+ path_span : Span ,
485
+ incl_angl_brckt : bool ,
486
+ insertion_span : Span ,
487
+ anon_lts : String ,
488
+ ) {
489
+ let ( replace_span, suggestion) = if incl_angl_brckt {
490
+ ( insertion_span, anon_lts)
491
+ } else {
492
+ // When possible, prefer a suggestion that replaces the whole
493
+ // `Path<T>` expression with `Path<'_, T>`, rather than inserting `'_, `
494
+ // at a point (which makes for an ugly/confusing label)
495
+ if let Ok ( snippet) = sess. source_map ( ) . span_to_snippet ( path_span) {
496
+ // But our spans can get out of whack due to macros; if the place we think
497
+ // we want to insert `'_` isn't even within the path expression's span, we
498
+ // should bail out of making any suggestion rather than panicking on a
499
+ // subtract-with-overflow or string-slice-out-out-bounds (!)
500
+ // FIXME: can we do better?
501
+ if insertion_span. lo ( ) . 0 < path_span. lo ( ) . 0 {
502
+ return ;
503
+ }
504
+ let insertion_index = ( insertion_span. lo ( ) . 0 - path_span. lo ( ) . 0 ) as usize ;
505
+ if insertion_index > snippet. len ( ) {
506
+ return ;
507
+ }
508
+ let ( before, after) = snippet. split_at ( insertion_index) ;
509
+ ( path_span, format ! ( "{}{}{}" , before, anon_lts, after) )
510
+ } else {
511
+ ( insertion_span, anon_lts)
512
+ }
513
+ } ;
514
+ db. span_suggestion (
515
+ replace_span,
516
+ & format ! ( "indicate the anonymous lifetime{}" , if n >= 2 { "s" } else { "" } ) ,
517
+ suggestion,
518
+ Applicability :: MachineApplicable
519
+ ) ;
520
+ }
521
+
480
522
impl BuiltinLintDiagnostics {
481
523
pub fn run ( self , sess : & Session , db : & mut DiagnosticBuilder < ' _ > ) {
482
524
match self {
@@ -521,36 +563,14 @@ impl BuiltinLintDiagnostics {
521
563
BuiltinLintDiagnostics :: ElidedLifetimesInPaths (
522
564
n, path_span, incl_angl_brckt, insertion_span, anon_lts
523
565
) => {
524
- let ( replace_span, suggestion) = if incl_angl_brckt {
525
- ( insertion_span, anon_lts)
526
- } else {
527
- // When possible, prefer a suggestion that replaces the whole
528
- // `Path<T>` expression with `Path<'_, T>`, rather than inserting `'_, `
529
- // at a point (which makes for an ugly/confusing label)
530
- if let Ok ( snippet) = sess. source_map ( ) . span_to_snippet ( path_span) {
531
- // But our spans can get out of whack due to macros; if the place we think
532
- // we want to insert `'_` isn't even within the path expression's span, we
533
- // should bail out of making any suggestion rather than panicking on a
534
- // subtract-with-overflow or string-slice-out-out-bounds (!)
535
- // FIXME: can we do better?
536
- if insertion_span. lo ( ) . 0 < path_span. lo ( ) . 0 {
537
- return ;
538
- }
539
- let insertion_index = ( insertion_span. lo ( ) . 0 - path_span. lo ( ) . 0 ) as usize ;
540
- if insertion_index > snippet. len ( ) {
541
- return ;
542
- }
543
- let ( before, after) = snippet. split_at ( insertion_index) ;
544
- ( path_span, format ! ( "{}{}{}" , before, anon_lts, after) )
545
- } else {
546
- ( insertion_span, anon_lts)
547
- }
548
- } ;
549
- db. span_suggestion (
550
- replace_span,
551
- & format ! ( "indicate the anonymous lifetime{}" , if n >= 2 { "s" } else { "" } ) ,
552
- suggestion,
553
- Applicability :: MachineApplicable
566
+ add_elided_lifetime_in_path_suggestion (
567
+ sess,
568
+ db,
569
+ n,
570
+ path_span,
571
+ incl_angl_brckt,
572
+ insertion_span,
573
+ anon_lts,
554
574
) ;
555
575
}
556
576
BuiltinLintDiagnostics :: UnknownCrateTypes ( span, note, sugg) => {
0 commit comments