From 846ae57fc1853949ff95c537023ad9b4dad11504 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 21:42:38 +0100 Subject: [PATCH] [Clippy] Swap `VecArgs::hir` to use diagnostic items instead of paths --- compiler/rustc_span/src/symbol.rs | 3 +++ library/alloc/src/slice.rs | 1 + library/alloc/src/vec/mod.rs | 2 ++ .../clippy/clippy_lints/src/slow_vector_initialization.rs | 2 +- src/tools/clippy/clippy_utils/src/higher.rs | 8 ++++---- src/tools/clippy/clippy_utils/src/paths.rs | 3 --- 6 files changed, 11 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 2194c4ac3b7..d449fac14bc 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1836,6 +1836,7 @@ symbols! { slice, slice_from_raw_parts, slice_from_raw_parts_mut, + slice_into_vec, slice_iter, slice_len_fn, slice_patterns, @@ -2090,7 +2091,9 @@ symbols! { vec, vec_as_mut_slice, vec_as_slice, + vec_from_elem, vec_macro, + vec_new, vecdeque_iter, version, vfp2, diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index 8cdba166c9d..45fb88969c6 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -496,6 +496,7 @@ impl [T] { #[rustc_allow_incoherent_impl] #[stable(feature = "rust1", since = "1.0.0")] #[inline] + #[cfg_attr(not(test), rustc_diagnostic_item = "slice_into_vec")] pub fn into_vec(self: Box) -> Vec { // N.B., see the `hack` module in this file for more details. hack::into_vec(self) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index ac0db38eac3..29ddf939499 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -416,6 +416,7 @@ impl Vec { /// ``` #[inline] #[rustc_const_stable(feature = "const_vec_new", since = "1.39.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "vec_new")] #[stable(feature = "rust1", since = "1.0.0")] #[must_use] pub const fn new() -> Self { @@ -3046,6 +3047,7 @@ impl Vec { #[doc(hidden)] #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "vec_from_elem")] pub fn from_elem(elem: T, n: usize) -> Vec { ::from_elem(elem, n, Global) } diff --git a/src/tools/clippy/clippy_lints/src/slow_vector_initialization.rs b/src/tools/clippy/clippy_lints/src/slow_vector_initialization.rs index b0e25c02265..6c0b0e11c9e 100644 --- a/src/tools/clippy/clippy_lints/src/slow_vector_initialization.rs +++ b/src/tools/clippy/clippy_lints/src/slow_vector_initialization.rs @@ -153,7 +153,7 @@ impl SlowVectorInit { && is_expr_path_def_path(cx, func, &paths::VEC_WITH_CAPACITY) { Some(InitializedSize::Initialized(len_expr)) - } else if matches!(expr.kind, ExprKind::Call(func, _) if is_expr_path_def_path(cx, func, &paths::VEC_NEW)) { + } else if matches!(expr.kind, ExprKind::Call(func, _) if is_path_diagnostic_item(cx, func, sym::vec_new)) { Some(InitializedSize::Uninitialized) } else { None diff --git a/src/tools/clippy/clippy_utils/src/higher.rs b/src/tools/clippy/clippy_utils/src/higher.rs index 8970b4d1229..e09cc9edf3a 100644 --- a/src/tools/clippy/clippy_utils/src/higher.rs +++ b/src/tools/clippy/clippy_utils/src/higher.rs @@ -4,7 +4,7 @@ use crate::consts::{ConstEvalCtxt, Constant}; use crate::ty::is_type_diagnostic_item; -use crate::{is_expn_of, match_def_path, paths}; +use crate::is_expn_of; use rustc_ast::ast; use rustc_hir as hir; @@ -297,10 +297,10 @@ impl<'a> VecArgs<'a> { && is_expn_of(fun.span, "vec").is_some() && let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id() { - return if match_def_path(cx, fun_def_id, &paths::VEC_FROM_ELEM) && args.len() == 2 { + return if cx.tcx.is_diagnostic_item(sym::vec_from_elem, fun_def_id) && args.len() == 2 { // `vec![elem; size]` case Some(VecArgs::Repeat(&args[0], &args[1])) - } else if match_def_path(cx, fun_def_id, &paths::SLICE_INTO_VEC) && args.len() == 1 { + } else if cx.tcx.is_diagnostic_item(sym::slice_into_vec, fun_def_id) && args.len() == 1 { // `vec![a, b, c]` case if let ExprKind::Call(_, [arg]) = &args[0].kind && let ExprKind::Array(args) = arg.kind @@ -309,7 +309,7 @@ impl<'a> VecArgs<'a> { } else { None } - } else if match_def_path(cx, fun_def_id, &paths::VEC_NEW) && args.is_empty() { + } else if cx.tcx.is_diagnostic_item(sym::vec_new, fun_def_id) && args.is_empty() { Some(VecArgs::Vec(&[])) } else { None diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index 64540637cdb..1d5a2294978 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -49,7 +49,6 @@ pub const REGEX_NEW: [&str; 3] = ["regex", "Regex", "new"]; pub const REGEX_SET_NEW: [&str; 3] = ["regex", "RegexSet", "new"]; pub const SERDE_DESERIALIZE: [&str; 3] = ["serde", "de", "Deserialize"]; pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"]; -pub const SLICE_INTO_VEC: [&str; 4] = ["alloc", "slice", "", "into_vec"]; pub const STD_IO_SEEK_FROM_CURRENT: [&str; 4] = ["std", "io", "SeekFrom", "Current"]; pub const STD_IO_SEEKFROM_START: [&str; 4] = ["std", "io", "SeekFrom", "Start"]; pub const STRING_NEW: [&str; 4] = ["alloc", "string", "String", "new"]; @@ -73,8 +72,6 @@ pub const TOKIO_IO_ASYNCWRITEEXT: [&str; 5] = ["tokio", "io", "util", "async_wri pub const TOKIO_IO_OPEN_OPTIONS: [&str; 4] = ["tokio", "fs", "open_options", "OpenOptions"]; #[expect(clippy::invalid_paths)] // internal lints do not know about all external crates pub const TOKIO_IO_OPEN_OPTIONS_NEW: [&str; 5] = ["tokio", "fs", "open_options", "OpenOptions", "new"]; -pub const VEC_FROM_ELEM: [&str; 3] = ["alloc", "vec", "from_elem"]; -pub const VEC_NEW: [&str; 4] = ["alloc", "vec", "Vec", "new"]; pub const VEC_WITH_CAPACITY: [&str; 4] = ["alloc", "vec", "Vec", "with_capacity"]; pub const INSTANT_NOW: [&str; 4] = ["std", "time", "Instant", "now"]; pub const VEC_IS_EMPTY: [&str; 4] = ["alloc", "vec", "Vec", "is_empty"];