Skip to content

Commit 3fb249b

Browse files
committed
improve "try ignoring the field" diagnostic
Closes #95795
1 parent f03ce30 commit 3fb249b

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed

compiler/rustc_passes/src/liveness.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ use rustc_middle::ty::query::Providers;
9898
use rustc_middle::ty::{self, DefIdTree, RootVariableMinCaptureList, Ty, TyCtxt};
9999
use rustc_session::lint;
100100
use rustc_span::symbol::{kw, sym, Symbol};
101-
use rustc_span::Span;
101+
use rustc_span::{BytePos, Span};
102102

103103
use std::collections::VecDeque;
104104
use std::io;
@@ -1549,23 +1549,27 @@ impl<'tcx> Liveness<'_, 'tcx> {
15491549
.or_insert_with(|| (ln, var, vec![id_and_sp]));
15501550
});
15511551

1552+
let can_remove = matches!(&pat.kind, hir::PatKind::Struct(_, _, true));
1553+
15521554
for (_, (ln, var, hir_ids_and_spans)) in vars {
15531555
if self.used_on_entry(ln, var) {
15541556
let id = hir_ids_and_spans[0].0;
15551557
let spans =
15561558
hir_ids_and_spans.into_iter().map(|(_, _, ident_span)| ident_span).collect();
15571559
on_used_on_entry(spans, id, ln, var);
15581560
} else {
1559-
self.report_unused(hir_ids_and_spans, ln, var);
1561+
self.report_unused(hir_ids_and_spans, ln, var, can_remove);
15601562
}
15611563
}
15621564
}
15631565

1566+
#[tracing::instrument(skip(self), level = "INFO")]
15641567
fn report_unused(
15651568
&self,
15661569
hir_ids_and_spans: Vec<(HirId, Span, Span)>,
15671570
ln: LiveNode,
15681571
var: Variable,
1572+
can_remove: bool,
15691573
) {
15701574
let first_hir_id = hir_ids_and_spans[0].0;
15711575

@@ -1590,6 +1594,32 @@ impl<'tcx> Liveness<'_, 'tcx> {
15901594
.emit();
15911595
},
15921596
)
1597+
} else if can_remove {
1598+
self.ir.tcx.struct_span_lint_hir(
1599+
lint::builtin::UNUSED_VARIABLES,
1600+
first_hir_id,
1601+
hir_ids_and_spans.iter().map(|(_, pat_span, _)| *pat_span).collect::<Vec<_>>(),
1602+
|lint| {
1603+
let mut err = lint.build(&format!("unused variable: `{}`", name));
1604+
err.multipart_suggestion(
1605+
"try removing the field",
1606+
hir_ids_and_spans
1607+
.iter()
1608+
.map(|(_, pat_span, _)| {
1609+
let span = self
1610+
.ir
1611+
.tcx
1612+
.sess
1613+
.source_map()
1614+
.span_extend_to_next_char(*pat_span, ',', true);
1615+
(span.with_hi(BytePos(span.hi().0 + 1)), String::new())
1616+
})
1617+
.collect(),
1618+
Applicability::MachineApplicable,
1619+
);
1620+
err.emit();
1621+
},
1622+
);
15931623
} else {
15941624
let (shorthands, non_shorthands): (Vec<_>, Vec<_>) =
15951625
hir_ids_and_spans.iter().copied().partition(|(hir_id, _, ident_span)| {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// run-pass
2+
3+
#![allow(dead_code)]
4+
5+
struct Foo {
6+
foo: i32,
7+
bar: i32,
8+
baz: (),
9+
}
10+
11+
fn use_foo(x: Foo) -> (i32, i32) {
12+
let Foo { foo, bar, baz } = x; //~ WARNING unused variable: `baz`
13+
//~| help: try ignoring the field
14+
return (foo, bar);
15+
}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
warning: unused variable: `baz`
2+
--> $DIR/dont-try-removing-the-field.rs:12:25
3+
|
4+
LL | let Foo { foo, bar, baz } = x;
5+
| ^^^ help: try ignoring the field: `baz: _`
6+
|
7+
= note: `#[warn(unused_variables)]` on by default
8+
9+
warning: 1 warning emitted
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// run-pass
2+
3+
#![allow(dead_code)]
4+
5+
struct Foo {
6+
foo: i32,
7+
bar: (),
8+
baz: (),
9+
}
10+
11+
fn use_foo(x: Foo) -> i32 {
12+
let Foo { foo, bar, .. } = x; //~ WARNING unused variable: `bar`
13+
//~| help: try removing the field
14+
return foo;
15+
}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
warning: unused variable: `bar`
2+
--> $DIR/try-removing-the-field.rs:12:20
3+
|
4+
LL | let Foo { foo, bar, .. } = x;
5+
| ^^^-
6+
| |
7+
| help: try removing the field
8+
|
9+
= note: `#[warn(unused_variables)]` on by default
10+
11+
warning: 1 warning emitted
12+

0 commit comments

Comments
 (0)