From f742afaca4c5afa3277f7102eac1e7a49048fefe Mon Sep 17 00:00:00 2001 From: jakubadamw Date: Sat, 11 Apr 2020 22:14:33 +0900 Subject: [PATCH 1/2] test for false "never constructed" warnings for `Self::` variant paths --- .../ui/lint/dead-code/lint-dead-code-5.rs | 20 +++++++++++++++++++ .../ui/lint/dead-code/lint-dead-code-5.stderr | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/test/ui/lint/dead-code/lint-dead-code-5.rs b/src/test/ui/lint/dead-code/lint-dead-code-5.rs index 764a23e4e49..b477c97c545 100644 --- a/src/test/ui/lint/dead-code/lint-dead-code-5.rs +++ b/src/test/ui/lint/dead-code/lint-dead-code-5.rs @@ -13,6 +13,23 @@ enum Enum2 { Variant5 { _x: isize }, //~ ERROR: variant is never constructed: `Variant5` Variant6(isize), //~ ERROR: variant is never constructed: `Variant6` _Variant7, + Variant8 { _field: bool }, + Variant9, + Variant10(usize) +} + +impl Enum2 { + fn new_variant8() -> Enum2 { + Self::Variant8 { _field: true } + } + + fn new_variant9() -> Enum2 { + Self::Variant9 + } + + fn new_variant10() -> Enum2 { + Self::Variant10(10) + } } enum Enum3 { //~ ERROR: enum is never used @@ -27,4 +44,7 @@ fn main() { Enum1::Variant2 => () } let x = Enum2::Variant3(true); + let _ = Enum2::new_variant8(); + let _ = Enum2::new_variant9(); + let _ = Enum2::new_variant10(); } diff --git a/src/test/ui/lint/dead-code/lint-dead-code-5.stderr b/src/test/ui/lint/dead-code/lint-dead-code-5.stderr index c0de4691020..519add82627 100644 --- a/src/test/ui/lint/dead-code/lint-dead-code-5.stderr +++ b/src/test/ui/lint/dead-code/lint-dead-code-5.stderr @@ -23,7 +23,7 @@ LL | Variant6(isize), | ^^^^^^^^^^^^^^^ error: enum is never used: `Enum3` - --> $DIR/lint-dead-code-5.rs:18:6 + --> $DIR/lint-dead-code-5.rs:35:6 | LL | enum Enum3 { | ^^^^^ From d3f8db6a2f5ae2a4895906f6ed340003affee6d5 Mon Sep 17 00:00:00 2001 From: seiya Date: Sat, 11 Apr 2020 22:34:45 +0900 Subject: [PATCH 2/2] Fix false "never constructed" warnings for `Self::` variant paths --- src/librustc_passes/dead.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/librustc_passes/dead.rs b/src/librustc_passes/dead.rs index 830bd255dfc..ac1f3847d96 100644 --- a/src/librustc_passes/dead.rs +++ b/src/librustc_passes/dead.rs @@ -255,7 +255,9 @@ fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { hir::ExprKind::Field(ref lhs, ..) => { self.handle_field_access(&lhs, expr.hir_id); } - hir::ExprKind::Struct(_, ref fields, _) => { + hir::ExprKind::Struct(ref qpath, ref fields, _) => { + let res = self.tables.qpath_res(qpath, expr.hir_id); + self.handle_res(res); if let ty::Adt(ref adt, _) = self.tables.expr_ty(expr).kind { self.mark_as_used_if_union(adt, fields); }