Skip to content

Commit 15240a9

Browse files
committed
[Clippy] Swap repeat_vec_with_capacity to use diagnostic item instead of path
1 parent 846ae57 commit 15240a9

File tree

5 files changed

+8
-7
lines changed

5 files changed

+8
-7
lines changed

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,6 +2094,7 @@ symbols! {
20942094
vec_from_elem,
20952095
vec_macro,
20962096
vec_new,
2097+
vec_with_capacity,
20972098
vecdeque_iter,
20982099
version,
20992100
vfp2,

library/alloc/src/vec/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ impl<T> Vec<T> {
477477
#[inline]
478478
#[stable(feature = "rust1", since = "1.0.0")]
479479
#[must_use]
480+
#[cfg_attr(not(test), rustc_diagnostic_item = "vec_with_capacity")]
480481
pub fn with_capacity(capacity: usize) -> Self {
481482
Self::with_capacity_in(capacity, Global)
482483
}

src/tools/clippy/clippy_lints/src/repeat_vec_with_capacity.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::higher::VecArgs;
44
use clippy_utils::macros::matching_root_macro_call;
55
use clippy_utils::source::snippet;
6-
use clippy_utils::{expr_or_init, fn_def_id, match_def_path, paths};
6+
use clippy_utils::{expr_or_init, fn_def_id};
77
use rustc_errors::Applicability;
88
use rustc_hir::{Expr, ExprKind};
99
use rustc_lint::{LateContext, LateLintPass};
@@ -67,7 +67,7 @@ fn emit_lint(cx: &LateContext<'_>, span: Span, kind: &str, note: &'static str, s
6767
fn check_vec_macro(cx: &LateContext<'_>, expr: &Expr<'_>) {
6868
if matching_root_macro_call(cx, expr.span, sym::vec_macro).is_some()
6969
&& let Some(VecArgs::Repeat(repeat_expr, len_expr)) = VecArgs::hir(cx, expr)
70-
&& fn_def_id(cx, repeat_expr).is_some_and(|did| match_def_path(cx, did, &paths::VEC_WITH_CAPACITY))
70+
&& fn_def_id(cx, repeat_expr).is_some_and(|did| cx.tcx.is_diagnostic_item(sym::vec_with_capacity, did))
7171
&& !len_expr.span.from_expansion()
7272
&& let Some(Constant::Int(2..)) = ConstEvalCtxt::new(cx).eval(expr_or_init(cx, len_expr))
7373
{
@@ -91,7 +91,7 @@ fn check_repeat_fn(cx: &LateContext<'_>, expr: &Expr<'_>) {
9191
if !expr.span.from_expansion()
9292
&& fn_def_id(cx, expr).is_some_and(|did| cx.tcx.is_diagnostic_item(sym::iter_repeat, did))
9393
&& let ExprKind::Call(_, [repeat_expr]) = expr.kind
94-
&& fn_def_id(cx, repeat_expr).is_some_and(|did| match_def_path(cx, did, &paths::VEC_WITH_CAPACITY))
94+
&& fn_def_id(cx, repeat_expr).is_some_and(|did| cx.tcx.is_diagnostic_item(sym::vec_with_capacity, did))
9595
&& !repeat_expr.span.from_expansion()
9696
{
9797
emit_lint(

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::macros::matching_root_macro_call;
33
use clippy_utils::sugg::Sugg;
44
use clippy_utils::{
5-
get_enclosing_block, is_expr_path_def_path, is_integer_literal, is_path_diagnostic_item, path_to_local,
6-
path_to_local_id, paths, SpanlessEq,
5+
get_enclosing_block, is_integer_literal, is_path_diagnostic_item, path_to_local,
6+
path_to_local_id, SpanlessEq,
77
};
88
use rustc_errors::Applicability;
99
use rustc_hir::intravisit::{walk_block, walk_expr, walk_stmt, Visitor};
@@ -150,7 +150,7 @@ impl SlowVectorInit {
150150
}
151151

152152
if let ExprKind::Call(func, [len_expr]) = expr.kind
153-
&& is_expr_path_def_path(cx, func, &paths::VEC_WITH_CAPACITY)
153+
&& is_path_diagnostic_item(cx, func, sym::vec_with_capacity)
154154
{
155155
Some(InitializedSize::Initialized(len_expr))
156156
} else if matches!(expr.kind, ExprKind::Call(func, _) if is_path_diagnostic_item(cx, func, sym::vec_new)) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ pub const TOKIO_IO_ASYNCWRITEEXT: [&str; 5] = ["tokio", "io", "util", "async_wri
7272
pub const TOKIO_IO_OPEN_OPTIONS: [&str; 4] = ["tokio", "fs", "open_options", "OpenOptions"];
7373
#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates
7474
pub const TOKIO_IO_OPEN_OPTIONS_NEW: [&str; 5] = ["tokio", "fs", "open_options", "OpenOptions", "new"];
75-
pub const VEC_WITH_CAPACITY: [&str; 4] = ["alloc", "vec", "Vec", "with_capacity"];
7675
pub const INSTANT_NOW: [&str; 4] = ["std", "time", "Instant", "now"];
7776
pub const VEC_IS_EMPTY: [&str; 4] = ["alloc", "vec", "Vec", "is_empty"];
7877
pub const VEC_POP: [&str; 4] = ["alloc", "vec", "Vec", "pop"];

0 commit comments

Comments
 (0)