Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
9f0c7f0
transmute size check: properly account for alignment
RalfJung May 29, 2024
d630f5d
Show notice about "never used" for enum
long-long-float Apr 28, 2024
a264bff
Mark assoc tys live only if the trait is live
mu001999 Jun 18, 2024
3f2f843
Ensure we don't accidentally succeed when we want to report an error
oli-obk Jun 13, 2024
3390159
Add `rust_analyzer` as a predefined tool
Veykril May 18, 2024
57a82e0
On short error format, append primary span label to message
estebank Jun 21, 2024
594fa01
not use offset when there is not ends with brace
bvanjoi Jun 23, 2024
8cfd4b1
Unify the precedence level for PREC_POSTFIX and PREC_PAREN
dtolnay Jun 24, 2024
273447c
Rename the 2 unambiguous precedence levels to PREC_UNAMBIGUOUS
dtolnay Jun 24, 2024
ba5ec1f
Suggest inline const blocks for array initialization
GrigorenkoPV Jun 24, 2024
8ffb5f9
compiletest: make the crash test error message abit more informative
matthiaskrgr Jun 13, 2024
1368c5e
Rollup merge of #124460 - long-long-float:show-notice-about-enum-with…
matthiaskrgr Jun 24, 2024
a7e148d
Rollup merge of #125241 - Veykril:tool-rust-analyzer, r=davidtwco
matthiaskrgr Jun 24, 2024
621ab36
Rollup merge of #125740 - RalfJung:transmute-size-check, r=oli-obk
matthiaskrgr Jun 24, 2024
b77d610
Rollup merge of #126413 - matthiaskrgr:crshmsg, r=oli-obk
matthiaskrgr Jun 24, 2024
4b78058
Rollup merge of #126618 - mu001999-contrib:dead/enhance, r=pnkfelix
matthiaskrgr Jun 24, 2024
a12f2d2
Rollup merge of #126673 - oli-obk:dont_rely_on_err_reporting, r=compi…
matthiaskrgr Jun 24, 2024
52a90f4
Rollup merge of #126804 - estebank:short-error-primary-label, r=david…
matthiaskrgr Jun 24, 2024
1ae001d
Rollup merge of #126868 - bvanjoi:fix-126764, r=davidtwco
matthiaskrgr Jun 24, 2024
98379cf
Rollup merge of #126893 - dtolnay:prec, r=compiler-errors
matthiaskrgr Jun 24, 2024
4aaa3b0
Rollup merge of #126899 - GrigorenkoPV:suggest-const-block, r=davidtwco
matthiaskrgr Jun 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Show notice about "never used" for enum
  • Loading branch information
long-long-float committed Jun 16, 2024
commit d630f5da7a24e225a9547301e6a7c634f67aad79
19 changes: 19 additions & 0 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,22 @@ impl<'tcx> DeadVisitor<'tcx> {
parent_item: Option<LocalDefId>,
report_on: ReportOn,
) {
fn get_parent_if_enum_variant<'tcx>(
tcx: TyCtxt<'tcx>,
may_variant: LocalDefId,
) -> LocalDefId {
if let Node::Variant(_) = tcx.hir_node_by_def_id(may_variant)
&& let Some(enum_did) = tcx.opt_parent(may_variant.to_def_id())
&& let Some(enum_local_id) = enum_did.as_local()
&& let Node::Item(item) = tcx.hir_node_by_def_id(enum_local_id)
&& let ItemKind::Enum(_, _) = item.kind
{
enum_local_id
} else {
may_variant
}
}

let Some(&first_item) = dead_codes.first() else {
return;
};
Expand Down Expand Up @@ -1054,6 +1070,9 @@ impl<'tcx> DeadVisitor<'tcx> {
};

let encl_def_id = parent_item.unwrap_or(first_item.def_id);
// If parent of encl_def_id is an enum, use the parent ID intead.
let encl_def_id = get_parent_if_enum_variant(tcx, encl_def_id);

let ignored_derived_impls =
if let Some(ign_traits) = self.ignored_derived_traits.get(&encl_def_id) {
let trait_list = ign_traits
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/lint/dead-code/unused-variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,22 @@ enum Enum {
Variant2,
}

#[derive(Debug)]
enum TupleVariant {
Variant1(i32), //~ ERROR: variant `Variant1` is never constructed
Variant2,
}

#[derive(Debug)]
enum StructVariant {
Variant1 { id: i32 }, //~ ERROR: variant `Variant1` is never constructed
Variant2,
}

fn main() {
let e = Enum::Variant2;
e.clone();

let _ = TupleVariant::Variant2;
let _ = StructVariant::Variant2;
}
22 changes: 21 additions & 1 deletion tests/ui/lint/dead-code/unused-variant.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,25 @@ note: the lint level is defined here
LL | #![deny(dead_code)]
| ^^^^^^^^^

error: aborting due to 1 previous error
error: variant `Variant1` is never constructed
--> $DIR/unused-variant.rs:11:5
|
LL | enum TupleVariant {
| ------------ variant in this enum
LL | Variant1(i32),
| ^^^^^^^^
|
= note: `TupleVariant` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

error: variant `Variant1` is never constructed
--> $DIR/unused-variant.rs:17:5
|
LL | enum StructVariant {
| ------------- variant in this enum
LL | Variant1 { id: i32 },
| ^^^^^^^^
|
= note: `StructVariant` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

error: aborting due to 3 previous errors