[Clippy] Swap repeat_vec_with_capacity
to use diagnostic item instead of path
This commit is contained in:
parent
846ae57fc1
commit
15240a93c9
@ -2094,6 +2094,7 @@
|
|||||||
vec_from_elem,
|
vec_from_elem,
|
||||||
vec_macro,
|
vec_macro,
|
||||||
vec_new,
|
vec_new,
|
||||||
|
vec_with_capacity,
|
||||||
vecdeque_iter,
|
vecdeque_iter,
|
||||||
version,
|
version,
|
||||||
vfp2,
|
vfp2,
|
||||||
|
@ -477,6 +477,7 @@ pub const fn new() -> Self {
|
|||||||
#[inline]
|
#[inline]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
#[cfg_attr(not(test), rustc_diagnostic_item = "vec_with_capacity")]
|
||||||
pub fn with_capacity(capacity: usize) -> Self {
|
pub fn with_capacity(capacity: usize) -> Self {
|
||||||
Self::with_capacity_in(capacity, Global)
|
Self::with_capacity_in(capacity, Global)
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
use clippy_utils::higher::VecArgs;
|
use clippy_utils::higher::VecArgs;
|
||||||
use clippy_utils::macros::matching_root_macro_call;
|
use clippy_utils::macros::matching_root_macro_call;
|
||||||
use clippy_utils::source::snippet;
|
use clippy_utils::source::snippet;
|
||||||
use clippy_utils::{expr_or_init, fn_def_id, match_def_path, paths};
|
use clippy_utils::{expr_or_init, fn_def_id};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{Expr, ExprKind};
|
use rustc_hir::{Expr, ExprKind};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
@ -67,7 +67,7 @@ fn emit_lint(cx: &LateContext<'_>, span: Span, kind: &str, note: &'static str, s
|
|||||||
fn check_vec_macro(cx: &LateContext<'_>, expr: &Expr<'_>) {
|
fn check_vec_macro(cx: &LateContext<'_>, expr: &Expr<'_>) {
|
||||||
if matching_root_macro_call(cx, expr.span, sym::vec_macro).is_some()
|
if matching_root_macro_call(cx, expr.span, sym::vec_macro).is_some()
|
||||||
&& let Some(VecArgs::Repeat(repeat_expr, len_expr)) = VecArgs::hir(cx, expr)
|
&& let Some(VecArgs::Repeat(repeat_expr, len_expr)) = VecArgs::hir(cx, expr)
|
||||||
&& fn_def_id(cx, repeat_expr).is_some_and(|did| match_def_path(cx, did, &paths::VEC_WITH_CAPACITY))
|
&& fn_def_id(cx, repeat_expr).is_some_and(|did| cx.tcx.is_diagnostic_item(sym::vec_with_capacity, did))
|
||||||
&& !len_expr.span.from_expansion()
|
&& !len_expr.span.from_expansion()
|
||||||
&& let Some(Constant::Int(2..)) = ConstEvalCtxt::new(cx).eval(expr_or_init(cx, len_expr))
|
&& let Some(Constant::Int(2..)) = ConstEvalCtxt::new(cx).eval(expr_or_init(cx, len_expr))
|
||||||
{
|
{
|
||||||
@ -91,7 +91,7 @@ fn check_repeat_fn(cx: &LateContext<'_>, expr: &Expr<'_>) {
|
|||||||
if !expr.span.from_expansion()
|
if !expr.span.from_expansion()
|
||||||
&& fn_def_id(cx, expr).is_some_and(|did| cx.tcx.is_diagnostic_item(sym::iter_repeat, did))
|
&& fn_def_id(cx, expr).is_some_and(|did| cx.tcx.is_diagnostic_item(sym::iter_repeat, did))
|
||||||
&& let ExprKind::Call(_, [repeat_expr]) = expr.kind
|
&& let ExprKind::Call(_, [repeat_expr]) = expr.kind
|
||||||
&& fn_def_id(cx, repeat_expr).is_some_and(|did| match_def_path(cx, did, &paths::VEC_WITH_CAPACITY))
|
&& fn_def_id(cx, repeat_expr).is_some_and(|did| cx.tcx.is_diagnostic_item(sym::vec_with_capacity, did))
|
||||||
&& !repeat_expr.span.from_expansion()
|
&& !repeat_expr.span.from_expansion()
|
||||||
{
|
{
|
||||||
emit_lint(
|
emit_lint(
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
use clippy_utils::macros::matching_root_macro_call;
|
use clippy_utils::macros::matching_root_macro_call;
|
||||||
use clippy_utils::sugg::Sugg;
|
use clippy_utils::sugg::Sugg;
|
||||||
use clippy_utils::{
|
use clippy_utils::{
|
||||||
get_enclosing_block, is_expr_path_def_path, is_integer_literal, is_path_diagnostic_item, path_to_local,
|
get_enclosing_block, is_integer_literal, is_path_diagnostic_item, path_to_local,
|
||||||
path_to_local_id, paths, SpanlessEq,
|
path_to_local_id, SpanlessEq,
|
||||||
};
|
};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::intravisit::{walk_block, walk_expr, walk_stmt, Visitor};
|
use rustc_hir::intravisit::{walk_block, walk_expr, walk_stmt, Visitor};
|
||||||
@ -150,7 +150,7 @@ fn as_vec_initializer<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'tcx>) -> Opt
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let ExprKind::Call(func, [len_expr]) = expr.kind
|
if let ExprKind::Call(func, [len_expr]) = expr.kind
|
||||||
&& is_expr_path_def_path(cx, func, &paths::VEC_WITH_CAPACITY)
|
&& is_path_diagnostic_item(cx, func, sym::vec_with_capacity)
|
||||||
{
|
{
|
||||||
Some(InitializedSize::Initialized(len_expr))
|
Some(InitializedSize::Initialized(len_expr))
|
||||||
} else if matches!(expr.kind, ExprKind::Call(func, _) if is_path_diagnostic_item(cx, func, sym::vec_new)) {
|
} else if matches!(expr.kind, ExprKind::Call(func, _) if is_path_diagnostic_item(cx, func, sym::vec_new)) {
|
||||||
|
@ -72,7 +72,6 @@
|
|||||||
pub const TOKIO_IO_OPEN_OPTIONS: [&str; 4] = ["tokio", "fs", "open_options", "OpenOptions"];
|
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
|
#[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 TOKIO_IO_OPEN_OPTIONS_NEW: [&str; 5] = ["tokio", "fs", "open_options", "OpenOptions", "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 INSTANT_NOW: [&str; 4] = ["std", "time", "Instant", "now"];
|
||||||
pub const VEC_IS_EMPTY: [&str; 4] = ["alloc", "vec", "Vec", "is_empty"];
|
pub const VEC_IS_EMPTY: [&str; 4] = ["alloc", "vec", "Vec", "is_empty"];
|
||||||
pub const VEC_POP: [&str; 4] = ["alloc", "vec", "Vec", "pop"];
|
pub const VEC_POP: [&str; 4] = ["alloc", "vec", "Vec", "pop"];
|
||||||
|
Loading…
Reference in New Issue
Block a user