@@ -1494,12 +1494,13 @@ impl<'tcx> Liveness<'_, 'tcx> {
1494
1494
// bindings, and we also consider the first pattern to be the "authoritative" set of ids.
1495
1495
// However, we should take the ids and spans of variables with the same name from the later
1496
1496
// patterns so the suggestions to prefix with underscores will apply to those too.
1497
- let mut vars: FxIndexMap < Symbol , ( LiveNode , Variable , Vec < ( HirId , Span ) > ) > = <_ >:: default ( ) ;
1497
+ let mut vars: FxIndexMap < Symbol , ( LiveNode , Variable , Vec < ( HirId , Span , Span ) > ) > =
1498
+ <_ >:: default ( ) ;
1498
1499
1499
1500
pat. each_binding ( |_, hir_id, pat_sp, ident| {
1500
1501
let ln = entry_ln. unwrap_or_else ( || self . live_node ( hir_id, pat_sp) ) ;
1501
1502
let var = self . variable ( hir_id, ident. span ) ;
1502
- let id_and_sp = ( hir_id, pat_sp) ;
1503
+ let id_and_sp = ( hir_id, pat_sp, ident . span ) ;
1503
1504
vars. entry ( self . ir . variable_name ( var) )
1504
1505
. and_modify ( |( .., hir_ids_and_spans) | hir_ids_and_spans. push ( id_and_sp) )
1505
1506
. or_insert_with ( || ( ln, var, vec ! [ id_and_sp] ) ) ;
@@ -1508,15 +1509,21 @@ impl<'tcx> Liveness<'_, 'tcx> {
1508
1509
for ( _, ( ln, var, hir_ids_and_spans) ) in vars {
1509
1510
if self . used_on_entry ( ln, var) {
1510
1511
let id = hir_ids_and_spans[ 0 ] . 0 ;
1511
- let spans = hir_ids_and_spans. into_iter ( ) . map ( |( _, sp) | sp) . collect ( ) ;
1512
+ let spans =
1513
+ hir_ids_and_spans. into_iter ( ) . map ( |( _, _, ident_span) | ident_span) . collect ( ) ;
1512
1514
on_used_on_entry ( spans, id, ln, var) ;
1513
1515
} else {
1514
1516
self . report_unused ( hir_ids_and_spans, ln, var) ;
1515
1517
}
1516
1518
}
1517
1519
}
1518
1520
1519
- fn report_unused ( & self , hir_ids_and_spans : Vec < ( HirId , Span ) > , ln : LiveNode , var : Variable ) {
1521
+ fn report_unused (
1522
+ & self ,
1523
+ hir_ids_and_spans : Vec < ( HirId , Span , Span ) > ,
1524
+ ln : LiveNode ,
1525
+ var : Variable ,
1526
+ ) {
1520
1527
let first_hir_id = hir_ids_and_spans[ 0 ] . 0 ;
1521
1528
1522
1529
if let Some ( name) = self . should_warn ( var) . filter ( |name| name != "self" ) {
@@ -1530,62 +1537,78 @@ impl<'tcx> Liveness<'_, 'tcx> {
1530
1537
self . ir . tcx . struct_span_lint_hir (
1531
1538
lint:: builtin:: UNUSED_VARIABLES ,
1532
1539
first_hir_id,
1533
- hir_ids_and_spans. into_iter ( ) . map ( |( _, sp) | sp) . collect :: < Vec < _ > > ( ) ,
1540
+ hir_ids_and_spans
1541
+ . into_iter ( )
1542
+ . map ( |( _, _, ident_span) | ident_span)
1543
+ . collect :: < Vec < _ > > ( ) ,
1534
1544
|lint| {
1535
1545
lint. build ( & format ! ( "variable `{}` is assigned to, but never used" , name) )
1536
1546
. note ( & format ! ( "consider using `_{}` instead" , name) )
1537
1547
. emit ( ) ;
1538
1548
} ,
1539
1549
)
1540
1550
} else {
1541
- self . ir . tcx . struct_span_lint_hir (
1542
- lint:: builtin:: UNUSED_VARIABLES ,
1543
- first_hir_id,
1544
- hir_ids_and_spans. iter ( ) . map ( |( _, sp) | * sp) . collect :: < Vec < _ > > ( ) ,
1545
- |lint| {
1546
- let mut err = lint. build ( & format ! ( "unused variable: `{}`" , name) ) ;
1547
-
1548
- let ( shorthands, non_shorthands) : ( Vec < _ > , Vec < _ > ) =
1549
- hir_ids_and_spans. into_iter ( ) . partition ( |( hir_id, span) | {
1550
- let var = self . variable ( * hir_id, * span) ;
1551
- self . ir . variable_is_shorthand ( var)
1552
- } ) ;
1553
-
1554
- let mut shorthands = shorthands
1555
- . into_iter ( )
1556
- . map ( |( _, span) | ( span, format ! ( "{}: _" , name) ) )
1557
- . collect :: < Vec < _ > > ( ) ;
1558
-
1559
- // If we have both shorthand and non-shorthand, prefer the "try ignoring
1560
- // the field" message, and suggest `_` for the non-shorthands. If we only
1561
- // have non-shorthand, then prefix with an underscore instead.
1562
- if !shorthands. is_empty ( ) {
1563
- shorthands. extend (
1564
- non_shorthands
1565
- . into_iter ( )
1566
- . map ( |( _, span) | ( span, "_" . to_string ( ) ) )
1567
- . collect :: < Vec < _ > > ( ) ,
1568
- ) ;
1551
+ let ( shorthands, non_shorthands) : ( Vec < _ > , Vec < _ > ) =
1552
+ hir_ids_and_spans. iter ( ) . copied ( ) . partition ( |( hir_id, _, ident_span) | {
1553
+ let var = self . variable ( * hir_id, * ident_span) ;
1554
+ self . ir . variable_is_shorthand ( var)
1555
+ } ) ;
1569
1556
1557
+ // If we have both shorthand and non-shorthand, prefer the "try ignoring
1558
+ // the field" message, and suggest `_` for the non-shorthands. If we only
1559
+ // have non-shorthand, then prefix with an underscore instead.
1560
+ if !shorthands. is_empty ( ) {
1561
+ let shorthands = shorthands
1562
+ . into_iter ( )
1563
+ . map ( |( _, pat_span, _) | ( pat_span, format ! ( "{}: _" , name) ) )
1564
+ . chain (
1565
+ non_shorthands
1566
+ . into_iter ( )
1567
+ . map ( |( _, pat_span, _) | ( pat_span, "_" . to_string ( ) ) ) ,
1568
+ )
1569
+ . collect :: < Vec < _ > > ( ) ;
1570
+
1571
+ self . ir . tcx . struct_span_lint_hir (
1572
+ lint:: builtin:: UNUSED_VARIABLES ,
1573
+ first_hir_id,
1574
+ hir_ids_and_spans
1575
+ . iter ( )
1576
+ . map ( |( _, pat_span, _) | * pat_span)
1577
+ . collect :: < Vec < _ > > ( ) ,
1578
+ |lint| {
1579
+ let mut err = lint. build ( & format ! ( "unused variable: `{}`" , name) ) ;
1570
1580
err. multipart_suggestion (
1571
1581
"try ignoring the field" ,
1572
1582
shorthands,
1573
1583
Applicability :: MachineApplicable ,
1574
1584
) ;
1575
- } else {
1585
+ err. emit ( )
1586
+ } ,
1587
+ ) ;
1588
+ } else {
1589
+ let non_shorthands = non_shorthands
1590
+ . into_iter ( )
1591
+ . map ( |( _, _, ident_span) | ( ident_span, format ! ( "_{}" , name) ) )
1592
+ . collect :: < Vec < _ > > ( ) ;
1593
+
1594
+ self . ir . tcx . struct_span_lint_hir (
1595
+ lint:: builtin:: UNUSED_VARIABLES ,
1596
+ first_hir_id,
1597
+ hir_ids_and_spans
1598
+ . iter ( )
1599
+ . map ( |( _, _, ident_span) | * ident_span)
1600
+ . collect :: < Vec < _ > > ( ) ,
1601
+ |lint| {
1602
+ let mut err = lint. build ( & format ! ( "unused variable: `{}`" , name) ) ;
1576
1603
err. multipart_suggestion (
1577
1604
"if this is intentional, prefix it with an underscore" ,
1578
- non_shorthands
1579
- . into_iter ( )
1580
- . map ( |( _, span) | ( span, format ! ( "_{}" , name) ) )
1581
- . collect :: < Vec < _ > > ( ) ,
1605
+ non_shorthands,
1582
1606
Applicability :: MachineApplicable ,
1583
1607
) ;
1584
- }
1585
-
1586
- err. emit ( )
1587
- } ,
1588
- ) ;
1608
+ err. emit ( )
1609
+ } ,
1610
+ ) ;
1611
+ }
1589
1612
}
1590
1613
}
1591
1614
}
0 commit comments