Skip to content

Commit 3a023e7

Browse files
committed
collapse dead code warnings into a single diagnostic
add comments in `store_dead_field_or_variant` support multiple log level add a item ident label fix ui tests fix a ui test fix a rustdoc ui test use let chain refactor: remove `store_dead_field_or_variant` fix a tiny bug
1 parent da27551 commit 3a023e7

File tree

74 files changed

+565
-338
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+565
-338
lines changed

Diff for: Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4286,6 +4286,7 @@ dependencies = [
42864286
name = "rustc_passes"
42874287
version = "0.0.0"
42884288
dependencies = [
4289+
"itertools",
42894290
"rustc_ast",
42904291
"rustc_ast_pretty",
42914292
"rustc_attr",

Diff for: compiler/rustc_passes/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
tracing = "0.1"
8+
itertools = "0.10.1"
89
rustc_middle = { path = "../rustc_middle" }
910
rustc_attr = { path = "../rustc_attr" }
1011
rustc_data_structures = { path = "../rustc_data_structures" }

Diff for: compiler/rustc_passes/src/dead.rs

+181-70
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
// closely. The idea is that all reachable symbols are live, codes called
33
// from live codes are live, and everything else is dead.
44

5+
use itertools::Itertools;
56
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
6-
use rustc_errors::pluralize;
7+
use rustc_errors::{pluralize, MultiSpan};
78
use rustc_hir as hir;
89
use rustc_hir::def::{CtorOf, DefKind, Res};
910
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -164,9 +165,10 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
164165
if let (Res::Local(id_l), Res::Local(id_r)) = (
165166
typeck_results.qpath_res(qpath_l, lhs.hir_id),
166167
typeck_results.qpath_res(qpath_r, rhs.hir_id),
167-
) && id_l == id_r
168-
{
169-
return true;
168+
) {
169+
if id_l == id_r {
170+
return true;
171+
}
170172
}
171173
return false;
172174
}
@@ -269,13 +271,10 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
269271
}
270272

271273
fn visit_node(&mut self, node: Node<'tcx>) {
272-
match node {
273-
Node::ImplItem(hir::ImplItem { def_id, .. })
274-
if self.should_ignore_item(def_id.to_def_id()) =>
275-
{
276-
return;
277-
}
278-
_ => (),
274+
if let Node::ImplItem(hir::ImplItem { def_id, .. }) = node
275+
&& self.should_ignore_item(def_id.to_def_id())
276+
{
277+
return;
279278
}
280279

281280
let had_repr_c = self.repr_has_repr_c;
@@ -624,11 +623,17 @@ fn live_symbols_and_ignored_derived_traits<'tcx>(
624623
(symbol_visitor.live_symbols, symbol_visitor.ignored_derived_traits)
625624
}
626625

626+
struct DeadVariant {
627+
hir_id: hir::HirId,
628+
span: Span,
629+
name: Symbol,
630+
level: lint::Level,
631+
}
632+
627633
struct DeadVisitor<'tcx> {
628634
tcx: TyCtxt<'tcx>,
629635
live_symbols: &'tcx FxHashSet<LocalDefId>,
630636
ignored_derived_traits: &'tcx FxHashMap<LocalDefId, Vec<(DefId, DefId)>>,
631-
ignored_struct_def: FxHashSet<LocalDefId>,
632637
}
633638

634639
impl<'tcx> DeadVisitor<'tcx> {
@@ -686,57 +691,119 @@ impl<'tcx> DeadVisitor<'tcx> {
686691
false
687692
}
688693

694+
fn warn_multiple_dead_codes(
695+
&self,
696+
dead_codes: &[(hir::HirId, Span, Symbol)],
697+
participle: &str,
698+
parent_hir_id: Option<hir::HirId>,
699+
) {
700+
if let Some((id, _, name)) = dead_codes.first()
701+
&& !name.as_str().starts_with('_')
702+
{
703+
self.tcx.struct_span_lint_hir(
704+
lint::builtin::DEAD_CODE,
705+
*id,
706+
MultiSpan::from_spans(
707+
dead_codes.iter().map(|(_, span, _)| *span).collect(),
708+
),
709+
|lint| {
710+
let def_id = self.tcx.hir().local_def_id(*id);
711+
let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id());
712+
let span_len = dead_codes.len();
713+
let names = match &dead_codes.iter().map(|(_, _, n)| n.to_string()).collect::<Vec<_>>()[..]
714+
{
715+
_ if span_len > 6 => String::new(),
716+
[name] => format!("`{name}` "),
717+
[names @ .., last] => {
718+
format!("{} and `{last}` ", names.iter().map(|name| format!("`{name}`")).join(", "))
719+
}
720+
[] => unreachable!(),
721+
};
722+
let mut err = lint.build(&format!(
723+
"{these}{descr}{s} {names}{are} never {participle}",
724+
these = if span_len > 6 { "multiple " } else { "" },
725+
s = pluralize!(span_len),
726+
are = pluralize!("is", span_len),
727+
));
728+
let hir = self.tcx.hir();
729+
if let Some(parent_hir_id) = parent_hir_id
730+
&& let Some(parent_node) = hir.find(parent_hir_id)
731+
&& let Node::Item(item) = parent_node
732+
{
733+
let def_id = self.tcx.hir().local_def_id(parent_hir_id);
734+
let parent_descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id());
735+
err.span_label(
736+
item.ident.span,
737+
format!(
738+
"{descr}{s} in this {parent_descr}",
739+
s = pluralize!(span_len)
740+
),
741+
);
742+
}
743+
if let Some(encl_scope) = hir.get_enclosing_scope(*id)
744+
&& let Some(encl_def_id) = hir.opt_local_def_id(encl_scope)
745+
&& let Some(ign_traits) = self.ignored_derived_traits.get(&encl_def_id)
746+
{
747+
let traits_str = ign_traits
748+
.iter()
749+
.map(|(trait_id, _)| format!("`{}`", self.tcx.item_name(*trait_id)))
750+
.collect::<Vec<_>>()
751+
.join(" and ");
752+
let plural_s = pluralize!(ign_traits.len());
753+
let article = if ign_traits.len() > 1 { "" } else { "a " };
754+
let is_are = if ign_traits.len() > 1 { "these are" } else { "this is" };
755+
let msg = format!(
756+
"`{}` has {}derived impl{} for the trait{} {}, but {} \
757+
intentionally ignored during dead code analysis",
758+
self.tcx.item_name(encl_def_id.to_def_id()),
759+
article,
760+
plural_s,
761+
plural_s,
762+
traits_str,
763+
is_are
764+
);
765+
err.note(&msg);
766+
}
767+
err.emit();
768+
},
769+
);
770+
}
771+
}
772+
773+
fn warn_dead_fields_and_variants(
774+
&self,
775+
hir_id: hir::HirId,
776+
participle: &str,
777+
dead_codes: Vec<DeadVariant>,
778+
) {
779+
let mut dead_codes = dead_codes
780+
.iter()
781+
.filter(|v| !v.name.as_str().starts_with('_'))
782+
.map(|v| v)
783+
.collect::<Vec<&DeadVariant>>();
784+
if dead_codes.is_empty() {
785+
return;
786+
}
787+
dead_codes.sort_by_key(|v| v.level);
788+
for (_, group) in &dead_codes.into_iter().group_by(|v| v.level) {
789+
self.warn_multiple_dead_codes(
790+
&group
791+
.map(|v| (v.hir_id, v.span, v.name))
792+
.collect::<Vec<(hir::HirId, Span, Symbol)>>(),
793+
participle,
794+
Some(hir_id),
795+
);
796+
}
797+
}
798+
689799
fn warn_dead_code(
690800
&mut self,
691801
id: hir::HirId,
692802
span: rustc_span::Span,
693803
name: Symbol,
694804
participle: &str,
695805
) {
696-
if !name.as_str().starts_with('_') {
697-
self.tcx.struct_span_lint_hir(lint::builtin::DEAD_CODE, id, span, |lint| {
698-
let def_id = self.tcx.hir().local_def_id(id);
699-
let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id());
700-
let mut err = lint.build(&format!("{descr} is never {participle}: `{name}`"));
701-
let hir = self.tcx.hir();
702-
let is_field_in_same_struct =
703-
if let Some(parent_hir_id) = self.tcx.hir().find_parent_node(id)
704-
&& let Some(parent_node) = self.tcx.hir().find(parent_hir_id)
705-
&& let Node::Item(hir::Item{kind: hir::ItemKind::Struct(..), ..}) = parent_node
706-
&& let Some(did) = self.tcx.hir().opt_local_def_id(parent_hir_id)
707-
{
708-
!self.ignored_struct_def.insert(did)
709-
} else {
710-
false
711-
};
712-
if !is_field_in_same_struct
713-
&& let Some(encl_scope) = hir.get_enclosing_scope(id)
714-
&& let Some(encl_def_id) = hir.opt_local_def_id(encl_scope)
715-
&& let Some(ign_traits) = self.ignored_derived_traits.get(&encl_def_id)
716-
{
717-
let traits_str = ign_traits
718-
.iter()
719-
.map(|(trait_id, _)| format!("`{}`", self.tcx.item_name(*trait_id)))
720-
.collect::<Vec<_>>()
721-
.join(" and ");
722-
let plural_s = pluralize!(ign_traits.len());
723-
let article = if ign_traits.len() > 1 { "" } else { "a " };
724-
let is_are = if ign_traits.len() > 1 { "these are" } else { "this is" };
725-
let msg = format!(
726-
"`{}` has {}derived impl{} for the trait{} {}, but {} \
727-
intentionally ignored during dead code analysis",
728-
self.tcx.item_name(encl_def_id.to_def_id()),
729-
article,
730-
plural_s,
731-
plural_s,
732-
traits_str,
733-
is_are
734-
);
735-
err.note(&msg);
736-
}
737-
err.emit();
738-
});
739-
}
806+
self.warn_multiple_dead_codes(&[(id, span, name)], participle, None);
740807
}
741808
}
742809

@@ -790,15 +857,40 @@ impl<'tcx> Visitor<'tcx> for DeadVisitor<'tcx> {
790857
// This visitor should only visit a single module at a time.
791858
fn visit_mod(&mut self, _: &'tcx hir::Mod<'tcx>, _: Span, _: hir::HirId) {}
792859

860+
fn visit_enum_def(
861+
&mut self,
862+
enum_definition: &'tcx hir::EnumDef<'tcx>,
863+
generics: &'tcx hir::Generics<'tcx>,
864+
item_id: hir::HirId,
865+
_: Span,
866+
) {
867+
intravisit::walk_enum_def(self, enum_definition, generics, item_id);
868+
let dead_variants = enum_definition
869+
.variants
870+
.iter()
871+
.filter_map(|variant| {
872+
if self.should_warn_about_variant(&variant) {
873+
Some(DeadVariant {
874+
hir_id: variant.id,
875+
span: variant.span,
876+
name: variant.ident.name,
877+
level: self.tcx.lint_level_at_node(lint::builtin::DEAD_CODE, variant.id).0,
878+
})
879+
} else {
880+
None
881+
}
882+
})
883+
.collect();
884+
self.warn_dead_fields_and_variants(item_id, "constructed", dead_variants)
885+
}
886+
793887
fn visit_variant(
794888
&mut self,
795889
variant: &'tcx hir::Variant<'tcx>,
796890
g: &'tcx hir::Generics<'tcx>,
797891
id: hir::HirId,
798892
) {
799-
if self.should_warn_about_variant(&variant) {
800-
self.warn_dead_code(variant.id, variant.span, variant.ident.name, "constructed");
801-
} else {
893+
if !self.should_warn_about_variant(&variant) {
802894
intravisit::walk_variant(self, variant, g, id);
803895
}
804896
}
@@ -810,11 +902,35 @@ impl<'tcx> Visitor<'tcx> for DeadVisitor<'tcx> {
810902
intravisit::walk_foreign_item(self, fi);
811903
}
812904

813-
fn visit_field_def(&mut self, field: &'tcx hir::FieldDef<'tcx>) {
814-
if self.should_warn_about_field(&field) {
815-
self.warn_dead_code(field.hir_id, field.span, field.ident.name, "read");
816-
}
817-
intravisit::walk_field_def(self, field);
905+
fn visit_variant_data(
906+
&mut self,
907+
def: &'tcx hir::VariantData<'tcx>,
908+
_: Symbol,
909+
_: &hir::Generics<'_>,
910+
id: hir::HirId,
911+
_: rustc_span::Span,
912+
) {
913+
intravisit::walk_struct_def(self, def);
914+
let dead_fields = def
915+
.fields()
916+
.iter()
917+
.filter_map(|field| {
918+
if self.should_warn_about_field(&field) {
919+
Some(DeadVariant {
920+
hir_id: field.hir_id,
921+
span: field.span,
922+
name: field.ident.name,
923+
level: self
924+
.tcx
925+
.lint_level_at_node(lint::builtin::DEAD_CODE, field.hir_id)
926+
.0,
927+
})
928+
} else {
929+
None
930+
}
931+
})
932+
.collect();
933+
self.warn_dead_fields_and_variants(id, "read", dead_fields)
818934
}
819935

820936
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
@@ -867,12 +983,7 @@ impl<'tcx> Visitor<'tcx> for DeadVisitor<'tcx> {
867983

868984
fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalDefId) {
869985
let (live_symbols, ignored_derived_traits) = tcx.live_symbols_and_ignored_derived_traits(());
870-
let mut visitor = DeadVisitor {
871-
tcx,
872-
live_symbols,
873-
ignored_derived_traits,
874-
ignored_struct_def: FxHashSet::default(),
875-
};
986+
let mut visitor = DeadVisitor { tcx, live_symbols, ignored_derived_traits };
876987
let (module, _, module_id) = tcx.hir().get_module(module);
877988
// Do not use an ItemLikeVisitor since we may want to skip visiting some items
878989
// when a surrounding one is warned against or `_`.

Diff for: src/test/rustdoc-ui/display-output.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ warning: unused variable: `x`
2424
LL | fn foo(x: &dyn std::fmt::Display) {}
2525
| ^ help: if this is intentional, prefix it with an underscore: `_x`
2626

27-
warning: function is never used: `foo`
27+
warning: function `foo` is never used
2828
--> $DIR/display-output.rs:13:4
2929
|
3030
LL | fn foo(x: &dyn std::fmt::Display) {}

Diff for: src/test/ui/associated-consts/associated-const-dead-code.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ struct MyFoo;
44

55
impl MyFoo {
66
const BAR: u32 = 1;
7-
//~^ ERROR associated constant is never used: `BAR`
7+
//~^ ERROR associated constant `BAR` is never used
88
}
99

1010
fn main() {

Diff for: src/test/ui/associated-consts/associated-const-dead-code.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: associated constant is never used: `BAR`
1+
error: associated constant `BAR` is never used
22
--> $DIR/associated-const-dead-code.rs:6:5
33
|
44
LL | const BAR: u32 = 1;

Diff for: src/test/ui/closures/2229_closure_analysis/issue-87987.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22
// edition:2021
33

44
struct Props {
5-
field_1: u32, //~ WARNING: field is never read: `field_1`
6-
field_2: u32, //~ WARNING: field is never read: `field_2`
5+
field_1: u32, //~ WARNING: fields `field_1` and `field_2` are never read
6+
field_2: u32,
77
}
88

99
fn main() {
1010
// Test 1
11-
let props_2 = Props {
12-
field_1: 1,
13-
field_2: 1,
14-
};
11+
let props_2 = Props { field_1: 1, field_2: 1 };
1512

1613
let _ = || {
1714
let _: Props = props_2;
@@ -23,7 +20,7 @@ fn main() {
2320
let mref = &mut arr;
2421

2522
let _c = || match arr {
26-
[_, _, _, _] => println!("A")
23+
[_, _, _, _] => println!("A"),
2724
};
2825

2926
println!("{:#?}", mref);

0 commit comments

Comments
 (0)