From 2a6564fc9ef785a4b7e96d1a67ab558f93519f33 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 10 Nov 2026 07:06:02 -0500 Subject: [PATCH] =?UTF-8?q?Read=20qualified=20struct=20paths=20through=20C?= =?UTF-8?q?lippy=E2=80=99s=20HIR=20interface?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clippy_lints/src/manual_let_else.rs | 2 +- clippy_lints/src/matches/match_wild_enum.rs | 2 +- .../matches/rest_pat_in_fully_bound_struct.rs | 6 ++++-- .../src/methods/string_from_utf8_as_bytes.rs | 7 ++++--- clippy_lints/src/unneeded_struct_pattern.rs | 10 +++++----- clippy_lints/src/use_self.rs | 17 +++++++++++------ clippy_lints/src/utils/author.rs | 6 ++++-- clippy_utils/src/higher.rs | 5 +++-- clippy_utils/src/lib.rs | 8 ++++++++ 9 files changed, 41 insertions(+), 22 deletions(-) diff --git a/clippy_lints/src/manual_let_else.rs b/clippy_lints/src/manual_let_else.rs index 87523cd0e8..4856236567 100644 --- a/clippy_lints/src/manual_let_else.rs +++ b/clippy_lints/src/manual_let_else.rs @@ -127,7 +127,7 @@ fn is_arms_disjointed(cx: &LateContext<'_>, arm1: &Arm<'_>, arm2: &Arm<'_>) -> b /// Returns `true` if the given pattern is a variant of an enum. pub fn is_enum_variant(cx: &LateContext<'_>, pat: &Pat<'_>) -> bool { - let path = match pat.kind { + let path: (&QPath<'_>, _) = match pat.kind { PatKind::Struct(ref qpath, fields, _) if fields .iter() diff --git a/clippy_lints/src/matches/match_wild_enum.rs b/clippy_lints/src/matches/match_wild_enum.rs index 443e84f163..3816b2f795 100644 --- a/clippy_lints/src/matches/match_wild_enum.rs +++ b/clippy_lints/src/matches/match_wild_enum.rs @@ -59,7 +59,7 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) { // this is incorrect, as we should really check whether each variant is exhaustively // covered by the set of guards that cover it, but that's really hard to do. recurse_or_patterns(arm.pat, |pat| { - let path = match &peel_hir_pat_refs(pat).0.kind { + let path: &QPath<'_> = match &peel_hir_pat_refs(pat).0.kind { PatKind::Expr(PatExpr { hir_id, kind: PatExprKind::Path(path), diff --git a/clippy_lints/src/matches/rest_pat_in_fully_bound_struct.rs b/clippy_lints/src/matches/rest_pat_in_fully_bound_struct.rs index 4a0da63681..459657d4a5 100644 --- a/clippy_lints/src/matches/rest_pat_in_fully_bound_struct.rs +++ b/clippy_lints/src/matches/rest_pat_in_fully_bound_struct.rs @@ -1,5 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_then; -use rustc_hir::{Pat, PatKind, QPath}; +use clippy_utils::resolved_path; +use rustc_hir::{Pat, PatKind}; use rustc_lint::LateContext; use rustc_middle::ty; @@ -7,7 +8,8 @@ use super::REST_PAT_IN_FULLY_BOUND_STRUCTS; pub(crate) fn check(cx: &LateContext<'_>, pat: &Pat<'_>) { if !pat.span.from_expansion() - && let PatKind::Struct(QPath::Resolved(_, path), fields, Some(dotdot)) = pat.kind + && let PatKind::Struct(ref qpath, fields, Some(dotdot)) = pat.kind + && let Some(path) = resolved_path(qpath) && let Some(def_id) = path.res.opt_def_id() && let ty = cx.tcx.type_of(def_id).instantiate_identity().skip_norm_wip() && let ty::Adt(def, _) = ty.kind() diff --git a/clippy_lints/src/methods/string_from_utf8_as_bytes.rs b/clippy_lints/src/methods/string_from_utf8_as_bytes.rs index 97506d795c..8e31cfe315 100644 --- a/clippy_lints/src/methods/string_from_utf8_as_bytes.rs +++ b/clippy_lints/src/methods/string_from_utf8_as_bytes.rs @@ -6,10 +6,11 @@ use clippy_utils::{method_calls, sym}; use rustc_ast::BorrowKind; use rustc_errors::Applicability; use rustc_hir::attrs::lang_items::LangItem; -use rustc_hir::{Expr, ExprKind}; +use rustc_hir::{Expr, ExprKind, QPath}; use rustc_lint::LateContext; pub(super) fn check_call(cx: &LateContext<'_>, expr: &Expr<'_>, fun: &Expr<'_>, args: &[Expr<'_>]) { + let is_range = |qpath: &QPath<'_>| cx.tcx.qpath_is_lang_item(*qpath, LangItem::Range); // Find `std::str::converts::from_utf8` or `std::primitive::str::from_utf8` if let [bytes_arg] = args && let Some(sym::str_from_utf8 | sym::str_inherent_from_utf8) = @@ -24,8 +25,8 @@ pub(super) fn check_call(cx: &LateContext<'_>, expr: &Expr<'_>, fun: &Expr<'_>, && expressions[0].1.is_empty() // Check for slicer - && let ExprKind::Struct(&qpath, _, _) = right.kind - && cx.tcx.qpath_is_lang_item(qpath, LangItem::Range) + && let ExprKind::Struct(qpath, _, _) = right.kind + && is_range(qpath) { let mut applicability = Applicability::MachineApplicable; let string_expression = &expressions[0].0; diff --git a/clippy_lints/src/unneeded_struct_pattern.rs b/clippy_lints/src/unneeded_struct_pattern.rs index 687fbdac2e..82c4a9502c 100644 --- a/clippy_lints/src/unneeded_struct_pattern.rs +++ b/clippy_lints/src/unneeded_struct_pattern.rs @@ -1,8 +1,8 @@ use clippy_utils::diagnostics::span_lint_and_sugg; -use clippy_utils::is_from_proc_macro; +use clippy_utils::{is_from_proc_macro, resolved_path}; use rustc_errors::Applicability; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::{Pat, PatKind, QPath}; +use rustc_hir::{Pat, PatKind}; use rustc_lint::{LateContext, LateLintPass, declare_lint_pass}; declare_clippy_lint! { @@ -42,8 +42,8 @@ declare_lint_pass!(UnneededStructPattern => [UNNEEDED_STRUCT_PATTERN]); impl LateLintPass<'_> for UnneededStructPattern { fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) { if !pat.span.from_expansion() - && let PatKind::Struct(path, [], _) = &pat.kind - && let QPath::Resolved(_, path) = path + && let PatKind::Struct(ref qpath, [], _) = pat.kind + && let Some(path) = resolved_path(qpath) && let Res::Def(DefKind::Variant, did) = path.res { let enum_did = cx.tcx.parent(did); @@ -55,7 +55,7 @@ impl LateLintPass<'_> for UnneededStructPattern { return; } - if is_from_proc_macro(cx, *path) { + if is_from_proc_macro(cx, path) { return; } diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs index 7b4250c607..03e7ba10c0 100644 --- a/clippy_lints/src/use_self.rs +++ b/clippy_lints/src/use_self.rs @@ -1,8 +1,8 @@ use clippy_config::Conf; use clippy_utils::diagnostics::span_lint_and_sugg; -use clippy_utils::is_from_proc_macro; use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::ty::{same_type_modulo_regions, ty_from_hir_ty}; +use clippy_utils::{is_from_proc_macro, resolved_path}; use rustc_data_structures::fx::FxHashSet; use rustc_errors::Applicability; use rustc_hir::def::{CtorOf, DefKind, Res}; @@ -235,7 +235,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf { && self.msrv.meets(cx, msrvs::TYPE_ALIAS_ENUM_VARIANTS) { match expr.kind { - ExprKind::Struct(QPath::Resolved(_, path), ..) => check_path(cx, path), + ExprKind::Struct(qpath, ..) if let Some(path) = resolved_path(qpath) => check_path(cx, path), ExprKind::Call(fun, _) => { if let ExprKind::Path(QPath::Resolved(_, path)) = fun.kind { check_path(cx, path); @@ -250,10 +250,15 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf { fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) { if !pat.span.from_expansion() && let Some(&StackItem::Check { impl_id, .. }) = self.stack.last() - // get the path from the pattern - && let PatKind::Expr(&PatExpr { kind: PatExprKind::Path(QPath::Resolved(_, path)), .. }) - | PatKind::TupleStruct(QPath::Resolved(_, path), _, _) - | PatKind::Struct(QPath::Resolved(_, path), _, _) = pat.kind + && let Some(path) = match &pat.kind { + PatKind::Expr(PatExpr { + kind: PatExprKind::Path(QPath::Resolved(_, path)), + .. + }) + | PatKind::TupleStruct(QPath::Resolved(_, path), _, _) => Some(*path), + PatKind::Struct(qpath, _, _) => resolved_path(qpath), + _ => None, + } && cx.typeck_results().pat_ty(pat) == cx.tcx.type_of(impl_id).instantiate_identity().skip_norm_wip() && self.msrv.meets(cx, msrvs::TYPE_ALIAS_ENUM_VARIANTS) { diff --git a/clippy_lints/src/utils/author.rs b/clippy_lints/src/utils/author.rs index a32a641c34..7047ad7475 100644 --- a/clippy_lints/src/utils/author.rs +++ b/clippy_lints/src/utils/author.rs @@ -686,7 +686,8 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> { bind!(self, container, fields); kind!("OffsetOf({container}, {fields})"); }, - ExprKind::Struct(qpath, fields, base) => { + ExprKind::Struct(path, fields, base) => { + let qpath: &QPath<'_> = path; bind!(self, qpath, fields); let base = OptionPat::new(match base { StructTailExpr::Base(base) => Some(self.bind("base", base)), @@ -778,7 +779,8 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> { self.ident(name); sub.if_some(|p| self.pat(p)); }, - PatKind::Struct(ref qpath, fields, etc) => { + PatKind::Struct(ref path, fields, etc) => { + let qpath: &QPath<'_> = path; let ignore = etc.is_some(); bind!(self, qpath, fields); kind!("Struct(ref {qpath}, {fields}, {ignore})"); diff --git a/clippy_utils/src/higher.rs b/clippy_utils/src/higher.rs index 8d37f8df3f..cc8c1fd499 100644 --- a/clippy_utils/src/higher.rs +++ b/clippy_utils/src/higher.rs @@ -219,14 +219,15 @@ impl<'a> Range<'a> { /// Higher a `hir` range to something similar to `ast::ExprKind::Range`. pub fn hir(cx: &LateContext<'_>, expr: &'a Expr<'_>) -> Option> { let span = expr.range_span()?; + let lang_item = |path: &QPath<'_>| cx.tcx.qpath_lang_item(*path); let (ty, start, end) = match expr.kind { ExprKind::Call(path, [arg1, arg2]) if let ExprKind::Path(qpath) = path.kind - && cx.tcx.qpath_is_lang_item(qpath, LangItem::RangeInclusiveNew) => + && lang_item(&qpath) == Some(LangItem::RangeInclusiveNew) => { (RangeTy::OpsInclusive, Some(arg1), Some(arg2)) }, - ExprKind::Struct(&qpath, fields, StructTailExpr::None) => match (cx.tcx.qpath_lang_item(qpath)?, fields) { + ExprKind::Struct(qpath, fields, StructTailExpr::None) => match (lang_item(qpath)?, fields) { (LangItem::RangeFull, []) => (RangeTy::OpsFull, None, None), (LangItem::RangeFrom, [start]) if start.ident.name == sym::start => { (RangeTy::OpsFrom, Some(start.expr), None) diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 81d8ab4fb3..b68f2cde6b 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -406,6 +406,14 @@ pub fn is_def_id_trait_method(cx: &LateContext<'_>, def_id: LocalDefId) -> bool } } +/// Returns the resolved path, if name resolution did not defer it to type checking. +pub fn resolved_path<'tcx>(qpath: &QPath<'tcx>) -> Option<&'tcx Path<'tcx>> { + match *qpath { + QPath::Resolved(_, path) => Some(path), + QPath::TypeRelative(..) => None, + } +} + pub fn last_path_segment<'tcx>(path: &QPath<'tcx>) -> &'tcx PathSegment<'tcx> { match *path { QPath::Resolved(_, path) => path.segments.last().expect("A path must have at least one segment"),