Skip to content

Commit 2f8fa01

Browse files
committed
Use identifier's span in unused lint
1 parent 573a697 commit 2f8fa01

File tree

1 file changed

+66
-43
lines changed

1 file changed

+66
-43
lines changed

compiler/rustc_passes/src/liveness.rs

+66-43
Original file line numberDiff line numberDiff line change
@@ -1494,12 +1494,13 @@ impl<'tcx> Liveness<'_, 'tcx> {
14941494
// bindings, and we also consider the first pattern to be the "authoritative" set of ids.
14951495
// However, we should take the ids and spans of variables with the same name from the later
14961496
// 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();
14981499

14991500
pat.each_binding(|_, hir_id, pat_sp, ident| {
15001501
let ln = entry_ln.unwrap_or_else(|| self.live_node(hir_id, pat_sp));
15011502
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);
15031504
vars.entry(self.ir.variable_name(var))
15041505
.and_modify(|(.., hir_ids_and_spans)| hir_ids_and_spans.push(id_and_sp))
15051506
.or_insert_with(|| (ln, var, vec![id_and_sp]));
@@ -1508,15 +1509,21 @@ impl<'tcx> Liveness<'_, 'tcx> {
15081509
for (_, (ln, var, hir_ids_and_spans)) in vars {
15091510
if self.used_on_entry(ln, var) {
15101511
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();
15121514
on_used_on_entry(spans, id, ln, var);
15131515
} else {
15141516
self.report_unused(hir_ids_and_spans, ln, var);
15151517
}
15161518
}
15171519
}
15181520

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+
) {
15201527
let first_hir_id = hir_ids_and_spans[0].0;
15211528

15221529
if let Some(name) = self.should_warn(var).filter(|name| name != "self") {
@@ -1530,62 +1537,78 @@ impl<'tcx> Liveness<'_, 'tcx> {
15301537
self.ir.tcx.struct_span_lint_hir(
15311538
lint::builtin::UNUSED_VARIABLES,
15321539
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<_>>(),
15341544
|lint| {
15351545
lint.build(&format!("variable `{}` is assigned to, but never used", name))
15361546
.note(&format!("consider using `_{}` instead", name))
15371547
.emit();
15381548
},
15391549
)
15401550
} 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+
});
15691556

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));
15701580
err.multipart_suggestion(
15711581
"try ignoring the field",
15721582
shorthands,
15731583
Applicability::MachineApplicable,
15741584
);
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));
15761603
err.multipart_suggestion(
15771604
"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,
15821606
Applicability::MachineApplicable,
15831607
);
1584-
}
1585-
1586-
err.emit()
1587-
},
1588-
);
1608+
err.emit()
1609+
},
1610+
);
1611+
}
15891612
}
15901613
}
15911614
}

0 commit comments

Comments
 (0)