From 559648a0a4c942993c321d999f708e64f169b245 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 29 Jan 2025 08:52:43 +0000 Subject: [PATCH] Handle all `PatExpr`s in dead code analysis --- compiler/rustc_passes/src/dead.rs | 17 ++++++++++++----- tests/ui/pattern/issue-110508.rs | 4 +++- tests/ui/pattern/issue-110508.stderr | 17 ----------------- 3 files changed, 15 insertions(+), 23 deletions(-) delete mode 100644 tests/ui/pattern/issue-110508.stderr diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index bc2bebdfef68c..95f18eaa7ef1b 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -13,7 +13,7 @@ use rustc_errors::MultiSpan; use rustc_hir::def::{CtorOf, DefKind, Res}; use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId}; use rustc_hir::intravisit::{self, Visitor}; -use rustc_hir::{self as hir, Node, PatExpr, PatExprKind, PatKind, TyKind}; +use rustc_hir::{self as hir, Node, PatKind, TyKind}; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; use rustc_middle::middle::privacy::Level; use rustc_middle::query::Providers; @@ -636,10 +636,6 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> { let res = self.typeck_results().qpath_res(path, pat.hir_id); self.handle_field_pattern_match(pat, res, fields); } - PatKind::Expr(PatExpr { kind: PatExprKind::Path(ref qpath), hir_id, .. }) => { - let res = self.typeck_results().qpath_res(qpath, *hir_id); - self.handle_res(res); - } PatKind::TupleStruct(ref qpath, fields, dotdot) => { let res = self.typeck_results().qpath_res(qpath, pat.hir_id); self.handle_tuple_field_pattern_match(pat, res, fields, dotdot); @@ -651,6 +647,17 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> { self.in_pat = false; } + fn visit_pat_expr(&mut self, expr: &'tcx rustc_hir::PatExpr<'tcx>) { + match &expr.kind { + rustc_hir::PatExprKind::Path(qpath) => { + let res = self.typeck_results().qpath_res(qpath, expr.hir_id); + self.handle_res(res); + } + _ => {} + } + intravisit::walk_pat_expr(self, expr); + } + fn visit_path(&mut self, path: &hir::Path<'tcx>, _: hir::HirId) { self.handle_res(path.res); intravisit::walk_path(self, path); diff --git a/tests/ui/pattern/issue-110508.rs b/tests/ui/pattern/issue-110508.rs index 980d8d52f1d57..74a8d673e8378 100644 --- a/tests/ui/pattern/issue-110508.rs +++ b/tests/ui/pattern/issue-110508.rs @@ -1,3 +1,5 @@ +//@ run-pass + #![deny(dead_code)] #[derive(PartialEq, Eq)] @@ -11,7 +13,7 @@ impl Foo { const A2: Foo = Self::FooA(()); const A3: Self = Foo::FooA(()); const A4: Self = Self::FooA(()); - const A5: u32 = 1; //~ ERROR: dead_code + const A5: u32 = 1; } fn main() { diff --git a/tests/ui/pattern/issue-110508.stderr b/tests/ui/pattern/issue-110508.stderr deleted file mode 100644 index aaec91167692c..0000000000000 --- a/tests/ui/pattern/issue-110508.stderr +++ /dev/null @@ -1,17 +0,0 @@ -error: associated constant `A5` is never used - --> $DIR/issue-110508.rs:14:11 - | -LL | impl Foo { - | -------- associated constant in this implementation -... -LL | const A5: u32 = 1; - | ^^ - | -note: the lint level is defined here - --> $DIR/issue-110508.rs:1:9 - | -LL | #![deny(dead_code)] - | ^^^^^^^^^ - -error: aborting due to 1 previous error -