Skip to main content

clippy_utils/
paths.rs

1//! This module contains paths to types and functions Clippy needs to know
2//! about.
3//!
4//! Whenever possible, please consider diagnostic items over hardcoded paths.
5//! See <https://github.com/rust-lang/rust-clippy/issues/5393> for more information.
6
7use crate::res::MaybeQPath;
8use crate::sym;
9use rustc_ast::Mutability;
10use rustc_data_structures::fx::FxHashMap;
11use rustc_hir::def::Namespace::{MacroNS, TypeNS, ValueNS};
12use rustc_hir::def::{DefKind, Namespace, Res};
13use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
14use rustc_hir::{ItemKind, Node, UseKind};
15use rustc_lint::LateContext;
16use rustc_middle::ty::fast_reject::SimplifiedType;
17use rustc_middle::ty::layout::HasTyCtxt;
18use rustc_middle::ty::{FloatTy, IntTy, Ty, TyCtxt, UintTy};
19use rustc_span::{Ident, STDLIB_STABLE_CRATES, Symbol};
20use std::sync::OnceLock;
21
22/// Specifies whether to resolve a path in the [`TypeNS`], [`ValueNS`], [`MacroNS`] or in an
23/// arbitrary namespace
24#[derive(Clone, Copy, PartialEq, Debug)]
25pub enum PathNS {
26    Type,
27    Value,
28    Macro,
29    Field,
30
31    /// Resolves to the name in the first available namespace, e.g. for `std::vec` this would return
32    /// either the macro or the module but **not** both
33    ///
34    /// Must only be used when the specific resolution is unimportant such as in
35    /// `missing_enforced_import_renames`
36    Arbitrary,
37}
38
39impl PathNS {
40    fn matches(self, ns: Option<Namespace>) -> bool {
41        let required = match self {
42            PathNS::Type => TypeNS,
43            PathNS::Value => ValueNS,
44            PathNS::Macro => MacroNS,
45            PathNS::Field => return false,
46            PathNS::Arbitrary => return true,
47        };
48
49        ns == Some(required)
50    }
51}
52
53/// Lazily resolves a path into a list of [`DefId`]s using [`lookup_path`].
54///
55/// Typically it will contain one [`DefId`] or none, but in some situations there can be multiple:
56/// - `memchr::memchr` could return the functions from both memchr 1.0 and memchr 2.0
57/// - `alloc::boxed::Box::downcast` would return a function for each of the different inherent impls
58///   ([1], [2], [3])
59///
60/// [1]: https://doc.rust-lang.org/std/boxed/struct.Box.html#method.downcast
61/// [2]: https://doc.rust-lang.org/std/boxed/struct.Box.html#method.downcast-1
62/// [3]: https://doc.rust-lang.org/std/boxed/struct.Box.html#method.downcast-2
63pub struct PathLookup {
64    ns: PathNS,
65    path: &'static [Symbol],
66    once: OnceLock<Vec<DefId>>,
67}
68
69impl PathLookup {
70    /// Only exported for tests and `clippy_lints_internal`
71    #[doc(hidden)]
72    pub const fn new(ns: PathNS, path: &'static [Symbol]) -> Self {
73        Self {
74            ns,
75            path,
76            once: OnceLock::new(),
77        }
78    }
79
80    /// Returns the list of [`DefId`]s that the path resolves to
81    pub fn get<'tcx>(&self, tcx: &impl HasTyCtxt<'tcx>) -> &[DefId] {
82        self.once.get_or_init(|| lookup_path(tcx.tcx(), self.ns, self.path))
83    }
84
85    /// Returns the single [`DefId`] that the path resolves to, this can only be used for paths into
86    /// stdlib crates to avoid the issue of multiple [`DefId`]s being returned
87    ///
88    /// May return [`None`] in `no_std`/`no_core` environments
89    pub fn only(&self, cx: &LateContext<'_>) -> Option<DefId> {
90        let ids = self.get(cx);
91        debug_assert!(STDLIB_STABLE_CRATES.contains(&self.path[0]));
92        debug_assert!(ids.len() <= 1, "{ids:?}");
93        ids.first().copied()
94    }
95
96    /// Checks if the path resolves to the given `def_id`
97    pub fn matches<'tcx>(&self, tcx: &impl HasTyCtxt<'tcx>, def_id: DefId) -> bool {
98        self.get(&tcx.tcx()).contains(&def_id)
99    }
100
101    /// Resolves `maybe_path` to a [`DefId`] and checks if the [`PathLookup`] matches it
102    pub fn matches_path<'tcx>(&self, cx: &LateContext<'_>, maybe_path: impl MaybeQPath<'tcx>) -> bool {
103        maybe_path
104            .res(cx)
105            .opt_def_id()
106            .is_some_and(|def_id| self.matches(cx, def_id))
107    }
108
109    /// Checks if the path resolves to `ty`'s definition, must be an `Adt`
110    pub fn matches_ty<'tcx>(&self, tcx: &impl HasTyCtxt<'tcx>, ty: Ty<'_>) -> bool {
111        ty.ty_adt_def().is_some_and(|adt| self.matches(&tcx.tcx(), adt.did()))
112    }
113}
114
115macro_rules! path_macros {
116    ($($name:ident: $ns:expr,)*) => {
117        $(
118            /// Only exported for tests and `clippy_lints_internal`
119            #[doc(hidden)]
120            #[macro_export]
121            macro_rules! $name {
122                ($$($$seg:ident $$(::)?)*) => {
123                    PathLookup::new($ns, &[$$(sym::$$seg,)*])
124                };
125            }
126        )*
127    };
128}
129
130path_macros! {
131    type_path: PathNS::Type,
132    value_path: PathNS::Value,
133    macro_path: PathNS::Macro,
134}
135
136// Paths in the standard library missing a diagnostic item
137
138// Paths in external crates
139pub static FUTURES_IO_ASYNCREADEXT: PathLookup = type_path!(futures_util::AsyncReadExt);
140pub static FUTURES_IO_ASYNCWRITEEXT: PathLookup = type_path!(futures_util::AsyncWriteExt);
141pub static ITERTOOLS_NEXT_TUPLE: PathLookup = value_path!(itertools::Itertools::next_tuple);
142pub static PARKING_LOT_GUARDS: [PathLookup; 3] = [
143    type_path!(lock_api::mutex::MutexGuard),
144    type_path!(lock_api::rwlock::RwLockReadGuard),
145    type_path!(lock_api::rwlock::RwLockWriteGuard),
146];
147pub static REGEX_BUILDER_NEW: PathLookup = value_path!(regex::RegexBuilder::new);
148pub static REGEX_BYTES_BUILDER_NEW: PathLookup = value_path!(regex::bytes::RegexBuilder::new);
149pub static REGEX_BYTES_NEW: PathLookup = value_path!(regex::bytes::Regex::new);
150pub static REGEX_BYTES_SET_NEW: PathLookup = value_path!(regex::bytes::RegexSet::new);
151pub static REGEX_NEW: PathLookup = value_path!(regex::Regex::new);
152pub static REGEX_SET_NEW: PathLookup = value_path!(regex::RegexSet::new);
153pub static SERDE_DESERIALIZE: PathLookup = type_path!(serde::de::Deserialize);
154pub static SERDE_DE_VISITOR: PathLookup = type_path!(serde::de::Visitor);
155pub static TOKIO_FILE_OPTIONS: PathLookup = value_path!(tokio::fs::File::options);
156pub static TOKIO_IO_ASYNCREADEXT: PathLookup = type_path!(tokio::io::AsyncReadExt);
157pub static TOKIO_IO_ASYNCWRITEEXT: PathLookup = type_path!(tokio::io::AsyncWriteExt);
158pub static TOKIO_IO_OPEN_OPTIONS: PathLookup = type_path!(tokio::fs::OpenOptions);
159pub static TOKIO_IO_OPEN_OPTIONS_NEW: PathLookup = value_path!(tokio::fs::OpenOptions::new);
160pub static LAZY_STATIC: PathLookup = macro_path!(lazy_static::lazy_static);
161pub static ONCE_CELL_SYNC_LAZY: PathLookup = type_path!(once_cell::sync::Lazy);
162pub static ONCE_CELL_SYNC_LAZY_NEW: PathLookup = value_path!(once_cell::sync::Lazy::new);
163
164// Paths for internal lints go in `clippy_lints_internal/src/internal_paths.rs`
165
166/// Equivalent to a [`lookup_path`] after splitting the input string on `::`
167///
168/// This function is expensive and should be used sparingly.
169pub fn lookup_path_str(tcx: TyCtxt<'_>, ns: PathNS, path: &str) -> Vec<DefId> {
170    let path: Vec<Symbol> = path.split("::").map(Symbol::intern).collect();
171    lookup_path(tcx, ns, &path)
172}
173
174/// Resolves a def path like `std::vec::Vec`.
175///
176/// Typically it will return one [`DefId`] or none, but in some situations there can be multiple:
177/// - `memchr::memchr` could return the functions from both memchr 1.0 and memchr 2.0
178/// - `alloc::boxed::Box::downcast` would return a function for each of the different inherent impls
179///   ([1], [2], [3])
180///
181/// This function is expensive and should be used sparingly.
182///
183/// [1]: https://doc.rust-lang.org/std/boxed/struct.Box.html#method.downcast
184/// [2]: https://doc.rust-lang.org/std/boxed/struct.Box.html#method.downcast-1
185/// [3]: https://doc.rust-lang.org/std/boxed/struct.Box.html#method.downcast-2
186pub fn lookup_path(tcx: TyCtxt<'_>, ns: PathNS, path: &[Symbol]) -> Vec<DefId> {
187    let (root, rest) = match *path {
188        [] | [_] => return Vec::new(),
189        [root, ref rest @ ..] => (root, rest),
190    };
191
192    let mut out = Vec::new();
193    for &base in find_crates(tcx, root).iter().chain(find_primitive_impls(tcx, root)) {
194        lookup_with_base(tcx, base, ns, rest, &mut out);
195    }
196    out
197}
198
199/// Finds the crates called `name`, may be multiple due to multiple major versions.
200pub fn find_crates(tcx: TyCtxt<'_>, name: Symbol) -> &'static [DefId] {
201    static BY_NAME: OnceLock<FxHashMap<Symbol, Vec<DefId>>> = OnceLock::new();
202    let map = BY_NAME.get_or_init(|| {
203        let mut map = FxHashMap::default();
204        map.insert(tcx.crate_name(LOCAL_CRATE), vec![LOCAL_CRATE.as_def_id()]);
205        for &num in tcx.crates(()) {
206            map.entry(tcx.crate_name(num)).or_default().push(num.as_def_id());
207        }
208        map
209    });
210    match map.get(&name) {
211        Some(def_ids) => def_ids,
212        None => &[],
213    }
214}
215
216fn find_primitive_impls(tcx: TyCtxt<'_>, name: Symbol) -> &[DefId] {
217    let ty = match name {
218        sym::bool => SimplifiedType::Bool,
219        sym::char => SimplifiedType::Char,
220        sym::str => SimplifiedType::Str,
221        sym::array => SimplifiedType::Array,
222        sym::slice => SimplifiedType::Slice,
223        // FIXME: rustdoc documents these two using just `pointer`.
224        //
225        // Maybe this is something we should do here too.
226        sym::const_ptr => SimplifiedType::Ptr(Mutability::Not),
227        sym::mut_ptr => SimplifiedType::Ptr(Mutability::Mut),
228        sym::isize => SimplifiedType::Int(IntTy::Isize),
229        sym::i8 => SimplifiedType::Int(IntTy::I8),
230        sym::i16 => SimplifiedType::Int(IntTy::I16),
231        sym::i32 => SimplifiedType::Int(IntTy::I32),
232        sym::i64 => SimplifiedType::Int(IntTy::I64),
233        sym::i128 => SimplifiedType::Int(IntTy::I128),
234        sym::usize => SimplifiedType::Uint(UintTy::Usize),
235        sym::u8 => SimplifiedType::Uint(UintTy::U8),
236        sym::u16 => SimplifiedType::Uint(UintTy::U16),
237        sym::u32 => SimplifiedType::Uint(UintTy::U32),
238        sym::u64 => SimplifiedType::Uint(UintTy::U64),
239        sym::u128 => SimplifiedType::Uint(UintTy::U128),
240        sym::f32 => SimplifiedType::Float(FloatTy::F32),
241        sym::f64 => SimplifiedType::Float(FloatTy::F64),
242        _ => return &[],
243    };
244
245    tcx.incoherent_impls(ty)
246}
247
248/// Resolves a def path like `vec::Vec` with the base `std`.
249fn lookup_with_base(tcx: TyCtxt<'_>, mut base: DefId, ns: PathNS, mut path: &[Symbol], out: &mut Vec<DefId>) {
250    loop {
251        match *path {
252            [segment] => {
253                out.extend(item_child_by_name(tcx, base, ns, segment));
254
255                // When the current def_id is e.g. `struct S`, check the impl items in
256                // `impl S { ... }`
257                let inherent_impl_children = tcx
258                    .inherent_impls(base)
259                    .iter()
260                    .filter_map(|&impl_def_id| item_child_by_name(tcx, impl_def_id, ns, segment));
261                out.extend(inherent_impl_children);
262
263                return;
264            },
265            [segment, ref rest @ ..] => {
266                path = rest;
267                let Some(child) = item_child_by_name(tcx, base, PathNS::Type, segment) else {
268                    return;
269                };
270                base = child;
271            },
272            [] => unreachable!(),
273        }
274    }
275}
276
277fn item_child_by_name(tcx: TyCtxt<'_>, def_id: DefId, ns: PathNS, name: Symbol) -> Option<DefId> {
278    if let Some(local_id) = def_id.as_local() {
279        local_item_child_by_name(tcx, local_id, ns, name)
280    } else {
281        non_local_item_child_by_name(tcx, def_id, ns, name)
282    }
283}
284
285fn local_item_child_by_name(tcx: TyCtxt<'_>, local_id: LocalDefId, ns: PathNS, name: Symbol) -> Option<DefId> {
286    let root_mod;
287    let item_kind = match tcx.hir_node_by_def_id(local_id) {
288        Node::Crate(r#mod) => {
289            root_mod = ItemKind::Mod(Ident::dummy(), r#mod);
290            &root_mod
291        },
292        Node::Item(item) => &item.kind,
293        Node::Variant(variant) if ns == PathNS::Field => {
294            return if let rustc_hir::VariantData::Struct { fields, .. } = variant.data
295                && let Some(field_def_id) = fields.iter().find_map(|field| {
296                    if field.ident.name == name {
297                        Some(field.def_id.to_def_id())
298                    } else {
299                        None
300                    }
301                }) {
302                Some(field_def_id)
303            } else {
304                None
305            };
306        },
307        _ => return None,
308    };
309
310    match item_kind {
311        ItemKind::Mod(_, r#mod) => r#mod.item_ids.iter().find_map(|&item_id| {
312            let item = tcx.hir_item(item_id);
313            if let ItemKind::Use(path, UseKind::Single(ident)) = item.kind {
314                if ident.name == name {
315                    let opt_def_id = |ns: Option<Res>| ns.and_then(|res| res.opt_def_id());
316                    match ns {
317                        PathNS::Type => opt_def_id(path.res.type_ns),
318                        PathNS::Value => opt_def_id(path.res.value_ns),
319                        PathNS::Macro => opt_def_id(path.res.macro_ns),
320                        PathNS::Field => None,
321                        PathNS::Arbitrary => unreachable!(),
322                    }
323                } else {
324                    None
325                }
326            } else if let Some(ident) = item.kind.ident()
327                && ident.name == name
328                && ns.matches(tcx.def_kind(item.owner_id).ns())
329            {
330                Some(item.owner_id.to_def_id())
331            } else {
332                None
333            }
334        }),
335        ItemKind::Impl(..) | ItemKind::Trait(..) => tcx
336            .associated_items(local_id)
337            .filter_by_name_unhygienic(name)
338            .find(|assoc_item| ns.matches(Some(assoc_item.namespace())))
339            .map(|assoc_item| assoc_item.def_id),
340        ItemKind::Struct(_, _, rustc_hir::VariantData::Struct { fields, .. }) if ns == PathNS::Field => {
341            fields.iter().find_map(|field| {
342                if field.ident.name == name {
343                    Some(field.def_id.to_def_id())
344                } else {
345                    None
346                }
347            })
348        },
349        ItemKind::Enum(_, _, rustc_hir::EnumDef { variants }) if ns == PathNS::Type => {
350            variants.iter().find_map(|variant| {
351                if variant.ident.name == name {
352                    Some(variant.def_id.to_def_id())
353                } else {
354                    None
355                }
356            })
357        },
358        _ => None,
359    }
360}
361
362fn non_local_item_child_by_name(tcx: TyCtxt<'_>, def_id: DefId, ns: PathNS, name: Symbol) -> Option<DefId> {
363    match tcx.def_kind(def_id) {
364        DefKind::Mod | DefKind::Enum | DefKind::Trait => tcx.module_children(def_id).iter().find_map(|child| {
365            if child.ident.name == name && ns.matches(child.res.ns()) {
366                child.res.opt_def_id()
367            } else {
368                None
369            }
370        }),
371        DefKind::Impl { .. } => tcx
372            .associated_item_def_ids(def_id)
373            .iter()
374            .copied()
375            .find(|&assoc_def_id| tcx.item_name(assoc_def_id) == name && ns.matches(tcx.def_kind(assoc_def_id).ns())),
376        DefKind::Struct => tcx
377            .associated_item_def_ids(def_id)
378            .iter()
379            .copied()
380            .find(|&assoc_def_id| tcx.item_name(assoc_def_id) == name),
381        _ => None,
382    }
383}