From 8e6092c0f6df671c33f15de11ef3583ef8fd3cc9 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 29 Dec 2024 20:10:51 +0100 Subject: [PATCH] Move `concat` macro path into `clippy_utils::paths` --- clippy_lints/src/useless_concat.rs | 3 ++- clippy_lints/src/utils/internal_lints/invalid_paths.rs | 5 +++++ clippy_utils/src/paths.rs | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/useless_concat.rs b/clippy_lints/src/useless_concat.rs index 3f68b93d4f57..4a818532f177 100644 --- a/clippy_lints/src/useless_concat.rs +++ b/clippy_lints/src/useless_concat.rs @@ -1,5 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::macros::macro_backtrace; +use clippy_utils::paths::CONCAT; use clippy_utils::source::snippet_opt; use clippy_utils::{match_def_path, tokenize_with_text}; use rustc_ast::LitKind; @@ -42,7 +43,7 @@ impl LateLintPass<'_> for UselessConcat { // Get the direct parent of the expression. && let Some(macro_call) = macro_backtrace(expr.span).next() // Check if the `concat` macro from the `core` library. - && match_def_path(cx, macro_call.def_id, &["core", "macros", "builtin", "concat"]) + && match_def_path(cx, macro_call.def_id, &CONCAT) // We get the original code to parse it. && let Some(original_code) = snippet_opt(cx, macro_call.span) // This check allows us to ensure that the code snippet: diff --git a/clippy_lints/src/utils/internal_lints/invalid_paths.rs b/clippy_lints/src/utils/internal_lints/invalid_paths.rs index a5fad68eea18..7a43c9a9ae57 100644 --- a/clippy_lints/src/utils/internal_lints/invalid_paths.rs +++ b/clippy_lints/src/utils/internal_lints/invalid_paths.rs @@ -61,6 +61,10 @@ pub fn check_path(cx: &LateContext<'_>, path: &[&str]) -> bool { if !def_path_res(cx.tcx, path).is_empty() { return true; } + // `builtin` macros don't seem to be found in `def_path_res`... + if path == ["core", "macros", "builtin", "concat"] { + return true; + } // Some implementations can't be found by `path_to_res`, particularly inherent // implementations of native types. Check lang items. @@ -77,6 +81,7 @@ pub fn check_path(cx: &LateContext<'_>, path: &[&str]) -> bool { .iter() .flat_map(|&ty| cx.tcx.incoherent_impls(ty).iter()) .copied(); + for item_def_id in lang_items.iter().map(|(_, def_id)| def_id).chain(incoherent_impls) { let lang_item_path = cx.get_def_path(item_def_id); if path_syms.starts_with(&lang_item_path) { diff --git a/clippy_utils/src/paths.rs b/clippy_utils/src/paths.rs index 8cb8cd590140..5b9a1a2c7113 100644 --- a/clippy_utils/src/paths.rs +++ b/clippy_utils/src/paths.rs @@ -33,6 +33,7 @@ pub const CHILD: [&str; 3] = ["std", "process", "Child"]; pub const CHILD_ID: [&str; 4] = ["std", "process", "Child", "id"]; pub const CHILD_KILL: [&str; 4] = ["std", "process", "Child", "kill"]; pub const PANIC_ANY: [&str; 3] = ["std", "panic", "panic_any"]; +pub const CONCAT: [&str; 4] = ["core", "macros", "builtin", "concat"]; // Paths in clippy itself pub const MSRV: [&str; 3] = ["clippy_utils", "msrvs", "Msrv"];