[Clippy] Swap VecArgs::hir to use diagnostic items instead of paths

This commit is contained in:
GnomedDev 2024-09-18 21:42:38 +01:00
parent f66915e8f8
commit 545967955a
3 changed files with 5 additions and 8 deletions

View File

@ -153,7 +153,7 @@ fn as_vec_initializer<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'tcx>) -> Opt
&& is_expr_path_def_path(cx, func, &paths::VEC_WITH_CAPACITY) && is_expr_path_def_path(cx, func, &paths::VEC_WITH_CAPACITY)
{ {
Some(InitializedSize::Initialized(len_expr)) 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) Some(InitializedSize::Uninitialized)
} else { } else {
None None

View File

@ -4,7 +4,7 @@
use crate::consts::{ConstEvalCtxt, Constant}; use crate::consts::{ConstEvalCtxt, Constant};
use crate::ty::is_type_diagnostic_item; 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_ast::ast;
use rustc_hir as hir; use rustc_hir as hir;
@ -297,10 +297,10 @@ pub fn hir(cx: &LateContext<'_>, expr: &'a Expr<'_>) -> Option<VecArgs<'a>> {
&& is_expn_of(fun.span, "vec").is_some() && is_expn_of(fun.span, "vec").is_some()
&& let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id() && 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 // `vec![elem; size]` case
Some(VecArgs::Repeat(&args[0], &args[1])) 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 // `vec![a, b, c]` case
if let ExprKind::Call(_, [arg]) = &args[0].kind if let ExprKind::Call(_, [arg]) = &args[0].kind
&& let ExprKind::Array(args) = arg.kind && let ExprKind::Array(args) = arg.kind
@ -309,7 +309,7 @@ pub fn hir(cx: &LateContext<'_>, expr: &'a Expr<'_>) -> Option<VecArgs<'a>> {
} else { } else {
None 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(&[])) Some(VecArgs::Vec(&[]))
} else { } else {
None None

View File

@ -49,7 +49,6 @@
pub const REGEX_SET_NEW: [&str; 3] = ["regex", "RegexSet", "new"]; pub const REGEX_SET_NEW: [&str; 3] = ["regex", "RegexSet", "new"];
pub const SERDE_DESERIALIZE: [&str; 3] = ["serde", "de", "Deserialize"]; pub const SERDE_DESERIALIZE: [&str; 3] = ["serde", "de", "Deserialize"];
pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"]; pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"];
pub const SLICE_INTO_VEC: [&str; 4] = ["alloc", "slice", "<impl [T]>", "into_vec"];
pub const STD_IO_SEEK_FROM_CURRENT: [&str; 4] = ["std", "io", "SeekFrom", "Current"]; 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 STD_IO_SEEKFROM_START: [&str; 4] = ["std", "io", "SeekFrom", "Start"];
pub const STRING_NEW: [&str; 4] = ["alloc", "string", "String", "new"]; pub const STRING_NEW: [&str; 4] = ["alloc", "string", "String", "new"];
@ -73,8 +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_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 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"];