Skip to content

Commit 846ae57

Browse files
committed
[Clippy] Swap VecArgs::hir to use diagnostic items instead of paths
1 parent 28f4c82 commit 846ae57

File tree

6 files changed

+11
-8
lines changed

6 files changed

+11
-8
lines changed

compiler/rustc_span/src/symbol.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1836,6 +1836,7 @@ symbols! {
18361836
slice,
18371837
slice_from_raw_parts,
18381838
slice_from_raw_parts_mut,
1839+
slice_into_vec,
18391840
slice_iter,
18401841
slice_len_fn,
18411842
slice_patterns,
@@ -2090,7 +2091,9 @@ symbols! {
20902091
vec,
20912092
vec_as_mut_slice,
20922093
vec_as_slice,
2094+
vec_from_elem,
20932095
vec_macro,
2096+
vec_new,
20942097
vecdeque_iter,
20952098
version,
20962099
vfp2,

library/alloc/src/slice.rs

+1
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ impl<T> [T] {
496496
#[rustc_allow_incoherent_impl]
497497
#[stable(feature = "rust1", since = "1.0.0")]
498498
#[inline]
499+
#[cfg_attr(not(test), rustc_diagnostic_item = "slice_into_vec")]
499500
pub fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A> {
500501
// N.B., see the `hack` module in this file for more details.
501502
hack::into_vec(self)

library/alloc/src/vec/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ impl<T> Vec<T> {
416416
/// ```
417417
#[inline]
418418
#[rustc_const_stable(feature = "const_vec_new", since = "1.39.0")]
419+
#[cfg_attr(not(test), rustc_diagnostic_item = "vec_new")]
419420
#[stable(feature = "rust1", since = "1.0.0")]
420421
#[must_use]
421422
pub const fn new() -> Self {
@@ -3046,6 +3047,7 @@ impl<T: PartialEq, A: Allocator> Vec<T, A> {
30463047
#[doc(hidden)]
30473048
#[cfg(not(no_global_oom_handling))]
30483049
#[stable(feature = "rust1", since = "1.0.0")]
3050+
#[cfg_attr(not(test), rustc_diagnostic_item = "vec_from_elem")]
30493051
pub fn from_elem<T: Clone>(elem: T, n: usize) -> Vec<T> {
30503052
<T as SpecFromElem>::from_elem(elem, n, Global)
30513053
}

src/tools/clippy/clippy_lints/src/slow_vector_initialization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl SlowVectorInit {
153153
&& is_expr_path_def_path(cx, func, &paths::VEC_WITH_CAPACITY)
154154
{
155155
Some(InitializedSize::Initialized(len_expr))
156-
} else if matches!(expr.kind, ExprKind::Call(func, _) if is_expr_path_def_path(cx, func, &paths::VEC_NEW)) {
156+
} else if matches!(expr.kind, ExprKind::Call(func, _) if is_path_diagnostic_item(cx, func, sym::vec_new)) {
157157
Some(InitializedSize::Uninitialized)
158158
} else {
159159
None

src/tools/clippy/clippy_utils/src/higher.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use crate::consts::{ConstEvalCtxt, Constant};
66
use crate::ty::is_type_diagnostic_item;
7-
use crate::{is_expn_of, match_def_path, paths};
7+
use crate::is_expn_of;
88

99
use rustc_ast::ast;
1010
use rustc_hir as hir;
@@ -297,10 +297,10 @@ impl<'a> VecArgs<'a> {
297297
&& is_expn_of(fun.span, "vec").is_some()
298298
&& let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id()
299299
{
300-
return if match_def_path(cx, fun_def_id, &paths::VEC_FROM_ELEM) && args.len() == 2 {
300+
return if cx.tcx.is_diagnostic_item(sym::vec_from_elem, fun_def_id) && args.len() == 2 {
301301
// `vec![elem; size]` case
302302
Some(VecArgs::Repeat(&args[0], &args[1]))
303-
} else if match_def_path(cx, fun_def_id, &paths::SLICE_INTO_VEC) && args.len() == 1 {
303+
} else if cx.tcx.is_diagnostic_item(sym::slice_into_vec, fun_def_id) && args.len() == 1 {
304304
// `vec![a, b, c]` case
305305
if let ExprKind::Call(_, [arg]) = &args[0].kind
306306
&& let ExprKind::Array(args) = arg.kind
@@ -309,7 +309,7 @@ impl<'a> VecArgs<'a> {
309309
} else {
310310
None
311311
}
312-
} else if match_def_path(cx, fun_def_id, &paths::VEC_NEW) && args.is_empty() {
312+
} else if cx.tcx.is_diagnostic_item(sym::vec_new, fun_def_id) && args.is_empty() {
313313
Some(VecArgs::Vec(&[]))
314314
} else {
315315
None

src/tools/clippy/clippy_utils/src/paths.rs

-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ pub const REGEX_NEW: [&str; 3] = ["regex", "Regex", "new"];
4949
pub const REGEX_SET_NEW: [&str; 3] = ["regex", "RegexSet", "new"];
5050
pub const SERDE_DESERIALIZE: [&str; 3] = ["serde", "de", "Deserialize"];
5151
pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"];
52-
pub const SLICE_INTO_VEC: [&str; 4] = ["alloc", "slice", "<impl [T]>", "into_vec"];
5352
pub const STD_IO_SEEK_FROM_CURRENT: [&str; 4] = ["std", "io", "SeekFrom", "Current"];
5453
pub const STD_IO_SEEKFROM_START: [&str; 4] = ["std", "io", "SeekFrom", "Start"];
5554
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
7372
pub const TOKIO_IO_OPEN_OPTIONS: [&str; 4] = ["tokio", "fs", "open_options", "OpenOptions"];
7473
#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates
7574
pub const TOKIO_IO_OPEN_OPTIONS_NEW: [&str; 5] = ["tokio", "fs", "open_options", "OpenOptions", "new"];
76-
pub const VEC_FROM_ELEM: [&str; 3] = ["alloc", "vec", "from_elem"];
77-
pub const VEC_NEW: [&str; 4] = ["alloc", "vec", "Vec", "new"];
7875
pub const VEC_WITH_CAPACITY: [&str; 4] = ["alloc", "vec", "Vec", "with_capacity"];
7976
pub const INSTANT_NOW: [&str; 4] = ["std", "time", "Instant", "now"];
8077
pub const VEC_IS_EMPTY: [&str; 4] = ["alloc", "vec", "Vec", "is_empty"];

0 commit comments

Comments
 (0)