From a786be5d0657a675ed6ddd5179ec66688e801610 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 17:58:18 +0100 Subject: [PATCH 01/20] [Clippy] Swap `map_entry` to use diagnostic items instead of paths --- compiler/rustc_span/src/symbol.rs | 4 ++++ library/alloc/src/collections/btree/map.rs | 2 ++ library/std/src/collections/hash/map.rs | 2 ++ src/tools/clippy/clippy_lints/src/entry.rs | 12 ++++++------ src/tools/clippy/clippy_utils/src/paths.rs | 4 ---- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index b588b4b6e97d..6765ce73186c 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -512,6 +512,8 @@ symbols! { breakpoint, bridge, bswap, + btreemap_contains_key, + btreemap_insert, btreeset_iter, builtin_syntax, c, @@ -973,6 +975,8 @@ symbols! { half_open_range_patterns, half_open_range_patterns_in_slices, hash, + hashmap_contains_key, + hashmap_insert, hashset_iter, hexagon_target_feature, hidden, diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index 60e08b47e3d3..0eadc9ecac8f 100644 --- a/library/alloc/src/collections/btree/map.rs +++ b/library/alloc/src/collections/btree/map.rs @@ -916,6 +916,7 @@ impl BTreeMap { /// assert_eq!(map.contains_key(&2), false); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "btreemap_contains_key")] pub fn contains_key(&self, key: &Q) -> bool where K: Borrow + Ord, @@ -981,6 +982,7 @@ impl BTreeMap { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_confusables("push", "put", "set")] + #[cfg_attr(not(test), rustc_diagnostic_item = "btreemap_insert")] pub fn insert(&mut self, key: K, value: V) -> Option where K: Ord, diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index 7d21d37f40df..0d7346c7f1ea 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -1037,6 +1037,7 @@ where /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_contains_key")] pub fn contains_key(&self, k: &Q) -> bool where K: Borrow, @@ -1100,6 +1101,7 @@ where #[inline] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_confusables("push", "append", "put")] + #[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_insert")] pub fn insert(&mut self, k: K, v: V) -> Option { self.base.insert(k, v) } diff --git a/src/tools/clippy/clippy_lints/src/entry.rs b/src/tools/clippy/clippy_lints/src/entry.rs index b7c9d3d03852..9c5100a8c1af 100644 --- a/src/tools/clippy/clippy_lints/src/entry.rs +++ b/src/tools/clippy/clippy_lints/src/entry.rs @@ -1,8 +1,8 @@ use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::{reindent_multiline, snippet_indent, snippet_with_applicability, snippet_with_context}; use clippy_utils::{ - can_move_expr_to_closure_no_visit, higher, is_expr_final_block_expr, is_expr_used_or_unified, match_def_path, - paths, peel_hir_expr_while, SpanlessEq, + can_move_expr_to_closure_no_visit, higher, is_expr_final_block_expr, is_expr_used_or_unified, + peel_hir_expr_while, SpanlessEq, }; use core::fmt::{self, Write}; use rustc_errors::Applicability; @@ -11,7 +11,7 @@ use rustc_hir::intravisit::{walk_expr, Visitor}; use rustc_hir::{Block, Expr, ExprKind, HirId, Pat, Stmt, StmtKind, UnOp}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::declare_lint_pass; -use rustc_span::{Span, SyntaxContext, DUMMY_SP}; +use rustc_span::{sym, Span, SyntaxContext, DUMMY_SP}; declare_clippy_lint! { /// ### What it does @@ -269,9 +269,9 @@ fn try_parse_contains<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'_>) -> Optio key, call_ctxt: expr.span.ctxt(), }; - if match_def_path(cx, id, &paths::BTREEMAP_CONTAINS_KEY) { + if cx.tcx.is_diagnostic_item(sym::btreemap_contains_key, id) { Some((MapType::BTree, expr)) - } else if match_def_path(cx, id, &paths::HASHMAP_CONTAINS_KEY) { + } else if cx.tcx.is_diagnostic_item(sym::hashmap_contains_key, id) { Some((MapType::Hash, expr)) } else { None @@ -306,7 +306,7 @@ struct InsertExpr<'tcx> { fn try_parse_insert<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option> { if let ExprKind::MethodCall(_, map, [key, value], _) = expr.kind { let id = cx.typeck_results().type_dependent_def_id(expr.hir_id)?; - if match_def_path(cx, id, &paths::BTREEMAP_INSERT) || match_def_path(cx, id, &paths::HASHMAP_INSERT) { + if cx.tcx.is_diagnostic_item(sym::btreemap_insert, id) || cx.tcx.is_diagnostic_item(sym::hashmap_insert, id) { Some(InsertExpr { map, key, value }) } else { None diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index fd28ca12d50f..59105383093d 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -12,8 +12,6 @@ pub const APPLICABILITY_VALUES: [[&str; 3]; 4] = [ ["rustc_lint_defs", "Applicability", "MachineApplicable"], ]; pub const DIAG: [&str; 2] = ["rustc_errors", "Diag"]; -pub const BTREEMAP_CONTAINS_KEY: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "contains_key"]; -pub const BTREEMAP_INSERT: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "insert"]; pub const CORE_RESULT_OK_METHOD: [&str; 4] = ["core", "result", "Result", "ok"]; pub const CSTRING_AS_C_STR: [&str; 5] = ["alloc", "ffi", "c_str", "CString", "as_c_str"]; pub const EARLY_CONTEXT: [&str; 2] = ["rustc_lint", "EarlyContext"]; @@ -25,8 +23,6 @@ pub const FILE_OPTIONS: [&str; 4] = ["std", "fs", "File", "options"]; pub const FUTURES_IO_ASYNCREADEXT: [&str; 3] = ["futures_util", "io", "AsyncReadExt"]; #[expect(clippy::invalid_paths)] // internal lints do not know about all external crates pub const FUTURES_IO_ASYNCWRITEEXT: [&str; 3] = ["futures_util", "io", "AsyncWriteExt"]; -pub const HASHMAP_CONTAINS_KEY: [&str; 6] = ["std", "collections", "hash", "map", "HashMap", "contains_key"]; -pub const HASHMAP_INSERT: [&str; 6] = ["std", "collections", "hash", "map", "HashMap", "insert"]; pub const HASHMAP_ITER: [&str; 5] = ["std", "collections", "hash", "map", "Iter"]; pub const HASHMAP_ITER_MUT: [&str; 5] = ["std", "collections", "hash", "map", "IterMut"]; pub const HASHMAP_KEYS: [&str; 5] = ["std", "collections", "hash", "map", "Keys"]; From 3ebff28f80cbbe5b51dd3d0edf8e5d0a8db1233f Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 19:36:12 +0100 Subject: [PATCH 02/20] [Clippy] Swap `lines_filter_map_ok` to use a diagnostic item instead of path --- compiler/rustc_span/src/symbol.rs | 1 + library/core/src/result.rs | 1 + src/tools/clippy/clippy_lints/src/lines_filter_map_ok.rs | 4 ++-- src/tools/clippy/clippy_utils/src/paths.rs | 1 - 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 6765ce73186c..bcebe1d39f51 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1575,6 +1575,7 @@ symbols! { residual, result, result_ffi_guarantees, + result_ok_method, resume, return_position_impl_trait_in_trait, return_type_notation, diff --git a/library/core/src/result.rs b/library/core/src/result.rs index 02f6f783b512..9edd58259ba0 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -653,6 +653,7 @@ impl Result { /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "result_ok_method")] pub fn ok(self) -> Option { match self { Ok(x) => Some(x), diff --git a/src/tools/clippy/clippy_lints/src/lines_filter_map_ok.rs b/src/tools/clippy/clippy_lints/src/lines_filter_map_ok.rs index 3d1c666dfea0..8206c75927b7 100644 --- a/src/tools/clippy/clippy_lints/src/lines_filter_map_ok.rs +++ b/src/tools/clippy/clippy_lints/src/lines_filter_map_ok.rs @@ -1,6 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::ty::is_type_diagnostic_item; -use clippy_utils::{is_diag_item_method, is_trait_method, match_def_path, path_to_local_id, paths}; +use clippy_utils::{is_diag_item_method, is_trait_method, path_to_local_id}; use rustc_errors::Applicability; use rustc_hir::{Body, Closure, Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; @@ -96,7 +96,7 @@ fn should_lint(cx: &LateContext<'_>, args: &[Expr<'_>], method_str: &str) -> boo ExprKind::Path(qpath) => cx .qpath_res(qpath, fm_arg.hir_id) .opt_def_id() - .is_some_and(|did| match_def_path(cx, did, &paths::CORE_RESULT_OK_METHOD)), + .is_some_and(|did| cx.tcx.is_diagnostic_item(sym::result_ok_method, did)), // Detect `|x| x.ok()` ExprKind::Closure(Closure { body, .. }) => { if let Body { diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index 59105383093d..0d1d43828750 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -12,7 +12,6 @@ pub const APPLICABILITY_VALUES: [[&str; 3]; 4] = [ ["rustc_lint_defs", "Applicability", "MachineApplicable"], ]; pub const DIAG: [&str; 2] = ["rustc_errors", "Diag"]; -pub const CORE_RESULT_OK_METHOD: [&str; 4] = ["core", "result", "Result", "ok"]; pub const CSTRING_AS_C_STR: [&str; 5] = ["alloc", "ffi", "c_str", "CString", "as_c_str"]; pub const EARLY_CONTEXT: [&str; 2] = ["rustc_lint", "EarlyContext"]; pub const EARLY_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "EarlyLintPass"]; From 5e4716888a731cdd929bdbaf94eeb3e192f1eace Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 21:09:06 +0100 Subject: [PATCH 03/20] [Clippy] Swap `option_as_ref_deref` to use diagnostic items instead of paths --- compiler/rustc_span/src/symbol.rs | 7 ++++++ library/alloc/src/ffi/c_str.rs | 1 + library/alloc/src/string.rs | 2 ++ library/alloc/src/vec/mod.rs | 2 ++ library/std/src/ffi/os_str.rs | 1 + library/std/src/path.rs | 1 + .../src/methods/option_as_ref_deref.rs | 24 +++++++++---------- src/tools/clippy/clippy_utils/src/paths.rs | 7 ------ 8 files changed, 26 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index bcebe1d39f51..73f260d35189 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -682,6 +682,7 @@ symbols! { crt_dash_static: "crt-static", csky_target_feature, cstr_type, + cstring_as_c_str, cstring_type, ctlz, ctlz_nonzero, @@ -1367,6 +1368,7 @@ symbols! { or, or_patterns, ord_cmp_method, + os_string_as_os_str, other, out, overflow_checks, @@ -1420,6 +1422,7 @@ symbols! { pat_param, patchable_function_entry, path, + pathbuf_as_path, pattern_complexity, pattern_parentheses, pattern_type, @@ -1871,6 +1874,8 @@ symbols! { str_trim_end, str_trim_start, strict_provenance, + string_as_mut_str, + string_as_str, string_deref_patterns, stringify, struct_field_attributes, @@ -2076,6 +2081,8 @@ symbols! { var, variant_count, vec, + vec_as_mut_slice, + vec_as_slice, vec_macro, vecdeque_iter, version, diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index e32676a65432..45037aa1615b 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -576,6 +576,7 @@ impl CString { #[inline] #[must_use] #[stable(feature = "as_c_str", since = "1.20.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "cstring_as_c_str")] pub fn as_c_str(&self) -> &CStr { &*self } diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 6daab5bc73a9..a2c70de3ed0e 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -1073,6 +1073,7 @@ impl String { #[inline] #[must_use] #[stable(feature = "string_as_str", since = "1.7.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "string_as_str")] pub fn as_str(&self) -> &str { self } @@ -1092,6 +1093,7 @@ impl String { #[inline] #[must_use] #[stable(feature = "string_as_str", since = "1.7.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "string_as_mut_str")] pub fn as_mut_str(&mut self) -> &mut str { self } diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 2afb5dd0d1a2..ac0db38eac3e 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1545,6 +1545,7 @@ impl Vec { /// ``` #[inline] #[stable(feature = "vec_as_slice", since = "1.7.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "vec_as_slice")] pub fn as_slice(&self) -> &[T] { self } @@ -1562,6 +1563,7 @@ impl Vec { /// ``` #[inline] #[stable(feature = "vec_as_slice", since = "1.7.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "vec_as_mut_slice")] pub fn as_mut_slice(&mut self) -> &mut [T] { self } diff --git a/library/std/src/ffi/os_str.rs b/library/std/src/ffi/os_str.rs index 99bea676e122..8e1147338b04 100644 --- a/library/std/src/ffi/os_str.rs +++ b/library/std/src/ffi/os_str.rs @@ -196,6 +196,7 @@ impl OsString { /// let os_str = OsStr::new("foo"); /// assert_eq!(os_string.as_os_str(), os_str); /// ``` + #[cfg_attr(not(test), rustc_diagnostic_item = "os_string_as_os_str")] #[stable(feature = "rust1", since = "1.0.0")] #[must_use] #[inline] diff --git a/library/std/src/path.rs b/library/std/src/path.rs index c94df9b5366b..6d6aa92711ba 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -1226,6 +1226,7 @@ impl PathBuf { /// let p = PathBuf::from("/test"); /// assert_eq!(Path::new("/test"), p.as_path()); /// ``` + #[cfg_attr(not(test), rustc_diagnostic_item = "pathbuf_as_path")] #[stable(feature = "rust1", since = "1.0.0")] #[must_use] #[inline] diff --git a/src/tools/clippy/clippy_lints/src/methods/option_as_ref_deref.rs b/src/tools/clippy/clippy_lints/src/methods/option_as_ref_deref.rs index cb57689b0c41..9a18d8a14219 100644 --- a/src/tools/clippy/clippy_lints/src/methods/option_as_ref_deref.rs +++ b/src/tools/clippy/clippy_lints/src/methods/option_as_ref_deref.rs @@ -2,12 +2,12 @@ use clippy_config::msrvs::{self, Msrv}; use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::snippet; use clippy_utils::ty::is_type_diagnostic_item; -use clippy_utils::{match_def_path, path_to_local_id, paths, peel_blocks}; +use clippy_utils::{path_to_local_id, peel_blocks}; use rustc_errors::Applicability; use rustc_hir as hir; use rustc_lint::LateContext; use rustc_middle::ty; -use rustc_span::sym; +use rustc_span::{sym, Symbol}; use super::OPTION_AS_REF_DEREF; @@ -31,14 +31,14 @@ pub(super) fn check( return; } - let deref_aliases: [&[&str]; 7] = [ - &paths::CSTRING_AS_C_STR, - &paths::OS_STRING_AS_OS_STR, - &paths::PATH_BUF_AS_PATH, - &paths::STRING_AS_STR, - &paths::STRING_AS_MUT_STR, - &paths::VEC_AS_SLICE, - &paths::VEC_AS_MUT_SLICE, + let deref_aliases: [Symbol; 7] = [ + sym::cstring_as_c_str, + sym::os_string_as_os_str, + sym::pathbuf_as_path, + sym::string_as_str, + sym::string_as_mut_str, + sym::vec_as_slice, + sym::vec_as_mut_slice, ]; let is_deref = match map_arg.kind { @@ -48,7 +48,7 @@ pub(super) fn check( .map_or(false, |fun_def_id| { cx.tcx.is_diagnostic_item(sym::deref_method, fun_def_id) || cx.tcx.is_diagnostic_item(sym::deref_mut_method, fun_def_id) - || deref_aliases.iter().any(|path| match_def_path(cx, fun_def_id, path)) + || deref_aliases.iter().any(|&sym| cx.tcx.is_diagnostic_item(sym, fun_def_id)) }) }, hir::ExprKind::Closure(&hir::Closure { body, .. }) => { @@ -69,7 +69,7 @@ pub(super) fn check( let method_did = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id).unwrap(); cx.tcx.is_diagnostic_item(sym::deref_method, method_did) || cx.tcx.is_diagnostic_item(sym::deref_mut_method, method_did) - || deref_aliases.iter().any(|path| match_def_path(cx, method_did, path)) + || deref_aliases.iter().any(|&sym| cx.tcx.is_diagnostic_item(sym, method_did)) } else { false } diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index 0d1d43828750..e0fa814d30b1 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -12,7 +12,6 @@ pub const APPLICABILITY_VALUES: [[&str; 3]; 4] = [ ["rustc_lint_defs", "Applicability", "MachineApplicable"], ]; pub const DIAG: [&str; 2] = ["rustc_errors", "Diag"]; -pub const CSTRING_AS_C_STR: [&str; 5] = ["alloc", "ffi", "c_str", "CString", "as_c_str"]; pub const EARLY_CONTEXT: [&str; 2] = ["rustc_lint", "EarlyContext"]; pub const EARLY_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "EarlyLintPass"]; pub const F32_EPSILON: [&str; 4] = ["core", "f32", "", "EPSILON"]; @@ -40,12 +39,10 @@ pub const LATE_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "LateLintPass"]; pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"]; pub const MSRV: [&str; 3] = ["clippy_config", "msrvs", "Msrv"]; pub const OPEN_OPTIONS_NEW: [&str; 4] = ["std", "fs", "OpenOptions", "new"]; -pub const OS_STRING_AS_OS_STR: [&str; 5] = ["std", "ffi", "os_str", "OsString", "as_os_str"]; pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "to_os_string"]; pub const PARKING_LOT_MUTEX_GUARD: [&str; 3] = ["lock_api", "mutex", "MutexGuard"]; pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockReadGuard"]; pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockWriteGuard"]; -pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"]; pub const PATH_MAIN_SEPARATOR: [&str; 3] = ["std", "path", "MAIN_SEPARATOR"]; pub const PATH_TO_PATH_BUF: [&str; 4] = ["std", "path", "Path", "to_path_buf"]; #[cfg_attr(not(unix), allow(clippy::invalid_paths))] @@ -62,8 +59,6 @@ pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"]; pub const SLICE_INTO_VEC: [&str; 4] = ["alloc", "slice", "", "into_vec"]; 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 STRING_AS_MUT_STR: [&str; 4] = ["alloc", "string", "String", "as_mut_str"]; -pub const STRING_AS_STR: [&str; 4] = ["alloc", "string", "String", "as_str"]; pub const STRING_NEW: [&str; 4] = ["alloc", "string", "String", "new"]; pub const STR_ENDS_WITH: [&str; 4] = ["core", "str", "", "ends_with"]; pub const STR_LEN: [&str; 4] = ["core", "str", "", "len"]; @@ -85,8 +80,6 @@ pub const TOKIO_IO_ASYNCWRITEEXT: [&str; 5] = ["tokio", "io", "util", "async_wri 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 pub const TOKIO_IO_OPEN_OPTIONS_NEW: [&str; 5] = ["tokio", "fs", "open_options", "OpenOptions", "new"]; -pub const VEC_AS_MUT_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_mut_slice"]; -pub const VEC_AS_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_slice"]; 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"]; From c89108202933fcdd6631a04b16bfebff3b9896d1 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 21:15:03 +0100 Subject: [PATCH 04/20] [Clippy] Swap `float_equality_without_abs` to use diagnostic items instead of paths --- compiler/rustc_span/src/symbol.rs | 2 ++ library/core/src/num/f32.rs | 1 + library/core/src/num/f64.rs | 1 + .../src/operators/float_equality_without_abs.rs | 6 +++--- src/tools/clippy/clippy_utils/src/paths.rs | 2 -- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 73f260d35189..17ff0bb86f8c 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -838,6 +838,7 @@ symbols! { f16_nan, f16c_target_feature, f32, + f32_epsilon, f32_legacy_const_digits, f32_legacy_const_epsilon, f32_legacy_const_infinity, @@ -854,6 +855,7 @@ symbols! { f32_legacy_const_radix, f32_nan, f64, + f64_epsilon, f64_legacy_const_digits, f64_legacy_const_epsilon, f64_legacy_const_infinity, diff --git a/library/core/src/num/f32.rs b/library/core/src/num/f32.rs index 4c2a4ee3b325..d0eb6bb3f287 100644 --- a/library/core/src/num/f32.rs +++ b/library/core/src/num/f32.rs @@ -415,6 +415,7 @@ impl f32 { /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon /// [`MANTISSA_DIGITS`]: f32::MANTISSA_DIGITS #[stable(feature = "assoc_int_consts", since = "1.43.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "f32_epsilon")] pub const EPSILON: f32 = 1.19209290e-07_f32; /// Smallest finite `f32` value. diff --git a/library/core/src/num/f64.rs b/library/core/src/num/f64.rs index 87fb5fe7ebee..4bc275ad1478 100644 --- a/library/core/src/num/f64.rs +++ b/library/core/src/num/f64.rs @@ -414,6 +414,7 @@ impl f64 { /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon /// [`MANTISSA_DIGITS`]: f64::MANTISSA_DIGITS #[stable(feature = "assoc_int_consts", since = "1.43.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "f64_epsilon")] pub const EPSILON: f64 = 2.2204460492503131e-16_f64; /// Smallest finite `f64` value. diff --git a/src/tools/clippy/clippy_lints/src/operators/float_equality_without_abs.rs b/src/tools/clippy/clippy_lints/src/operators/float_equality_without_abs.rs index cace85a24513..be97ad389bf6 100644 --- a/src/tools/clippy/clippy_lints/src/operators/float_equality_without_abs.rs +++ b/src/tools/clippy/clippy_lints/src/operators/float_equality_without_abs.rs @@ -1,12 +1,12 @@ use clippy_utils::diagnostics::span_lint_and_then; -use clippy_utils::{match_def_path, paths, sugg}; +use clippy_utils::sugg; use rustc_ast::util::parser::AssocOp; use rustc_errors::Applicability; use rustc_hir::def::{DefKind, Res}; use rustc_hir::{BinOpKind, Expr, ExprKind}; use rustc_lint::LateContext; use rustc_middle::ty; -use rustc_span::source_map::Spanned; +use rustc_span::{sym, source_map::Spanned}; use super::FLOAT_EQUALITY_WITHOUT_ABS; @@ -36,7 +36,7 @@ pub(crate) fn check<'tcx>( // right hand side matches either f32::EPSILON or f64::EPSILON && let ExprKind::Path(ref epsilon_path) = rhs.kind && let Res::Def(DefKind::AssocConst, def_id) = cx.qpath_res(epsilon_path, rhs.hir_id) - && (match_def_path(cx, def_id, &paths::F32_EPSILON) || match_def_path(cx, def_id, &paths::F64_EPSILON)) + && ([sym::f32_epsilon, sym::f64_epsilon].into_iter().any(|sym| cx.tcx.is_diagnostic_item(sym, def_id))) // values of the subtractions on the left hand side are of the type float && let t_val_l = cx.typeck_results().expr_ty(val_l) diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index e0fa814d30b1..ea7c19dec6d0 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -14,8 +14,6 @@ pub const APPLICABILITY_VALUES: [[&str; 3]; 4] = [ pub const DIAG: [&str; 2] = ["rustc_errors", "Diag"]; pub const EARLY_CONTEXT: [&str; 2] = ["rustc_lint", "EarlyContext"]; pub const EARLY_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "EarlyLintPass"]; -pub const F32_EPSILON: [&str; 4] = ["core", "f32", "", "EPSILON"]; -pub const F64_EPSILON: [&str; 4] = ["core", "f64", "", "EPSILON"]; pub const FILE_OPTIONS: [&str; 4] = ["std", "fs", "File", "options"]; #[expect(clippy::invalid_paths)] // internal lints do not know about all external crates pub const FUTURES_IO_ASYNCREADEXT: [&str; 3] = ["futures_util", "io", "AsyncReadExt"]; From afe79079144076240a4eb7db70b733ec49a787f7 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 21:21:17 +0100 Subject: [PATCH 05/20] [Clippy] Swap `redundant_clone` to use diagnostic items instead of paths --- compiler/rustc_span/src/symbol.rs | 2 ++ library/std/src/ffi/os_str.rs | 1 + library/std/src/path.rs | 1 + src/tools/clippy/clippy_lints/src/redundant_clone.rs | 6 +++--- src/tools/clippy/clippy_utils/src/paths.rs | 2 -- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 17ff0bb86f8c..c9b36d77e1ac 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1370,6 +1370,7 @@ symbols! { or, or_patterns, ord_cmp_method, + os_str_to_os_string, os_string_as_os_str, other, out, @@ -1424,6 +1425,7 @@ symbols! { pat_param, patchable_function_entry, path, + path_to_pathbuf, pathbuf_as_path, pattern_complexity, pattern_parentheses, diff --git a/library/std/src/ffi/os_str.rs b/library/std/src/ffi/os_str.rs index 8e1147338b04..0f905803bb8b 100644 --- a/library/std/src/ffi/os_str.rs +++ b/library/std/src/ffi/os_str.rs @@ -919,6 +919,7 @@ impl OsStr { #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] + #[cfg_attr(not(test), rustc_diagnostic_item = "os_str_to_os_string")] pub fn to_os_string(&self) -> OsString { OsString { inner: self.inner.to_owned() } } diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 6d6aa92711ba..2ba3a9cdca25 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -2265,6 +2265,7 @@ impl Path { #[must_use = "this returns the result of the operation, \ without modifying the original"] #[stable(feature = "rust1", since = "1.0.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "path_to_pathbuf")] pub fn to_path_buf(&self) -> PathBuf { PathBuf::from(self.inner.to_os_string()) } diff --git a/src/tools/clippy/clippy_lints/src/redundant_clone.rs b/src/tools/clippy/clippy_lints/src/redundant_clone.rs index bfdc1cbeed7e..4e24ddad83a5 100644 --- a/src/tools/clippy/clippy_lints/src/redundant_clone.rs +++ b/src/tools/clippy/clippy_lints/src/redundant_clone.rs @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_hir, span_lint_hir_and_then}; use clippy_utils::mir::{visit_local_usage, LocalUsage, PossibleBorrowerMap}; use clippy_utils::source::SpanRangeExt; use clippy_utils::ty::{has_drop, is_copy, is_type_diagnostic_item, is_type_lang_item, walk_ptrs_ty_depth}; -use clippy_utils::{fn_has_unsatisfiable_preds, match_def_path, paths}; +use clippy_utils::fn_has_unsatisfiable_preds; use rustc_errors::Applicability; use rustc_hir::intravisit::FnKind; use rustc_hir::{def_id, Body, FnDecl, LangItem}; @@ -102,8 +102,8 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone { && is_type_lang_item(cx, arg_ty, LangItem::String)); let from_deref = !from_borrow - && (match_def_path(cx, fn_def_id, &paths::PATH_TO_PATH_BUF) - || match_def_path(cx, fn_def_id, &paths::OS_STR_TO_OS_STRING)); + && (cx.tcx.is_diagnostic_item(sym::path_to_pathbuf, fn_def_id) + || cx.tcx.is_diagnostic_item(sym::os_str_to_os_string, fn_def_id)); if !from_borrow && !from_deref { continue; diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index ea7c19dec6d0..0a36d9a66649 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -37,12 +37,10 @@ pub const LATE_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "LateLintPass"]; pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"]; pub const MSRV: [&str; 3] = ["clippy_config", "msrvs", "Msrv"]; pub const OPEN_OPTIONS_NEW: [&str; 4] = ["std", "fs", "OpenOptions", "new"]; -pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "to_os_string"]; pub const PARKING_LOT_MUTEX_GUARD: [&str; 3] = ["lock_api", "mutex", "MutexGuard"]; pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockReadGuard"]; pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockWriteGuard"]; pub const PATH_MAIN_SEPARATOR: [&str; 3] = ["std", "path", "MAIN_SEPARATOR"]; -pub const PATH_TO_PATH_BUF: [&str; 4] = ["std", "path", "Path", "to_path_buf"]; #[cfg_attr(not(unix), allow(clippy::invalid_paths))] pub const PERMISSIONS_FROM_MODE: [&str; 6] = ["std", "os", "unix", "fs", "PermissionsExt", "from_mode"]; pub const PUSH_STR: [&str; 4] = ["alloc", "string", "String", "push_str"]; From 037b9784b68f7e11a05a722b6aa6c768484dec40 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 21:24:42 +0100 Subject: [PATCH 06/20] [Clippy] Swap `manual_main_separator_str` to use diagnostic item instead of path --- compiler/rustc_span/src/symbol.rs | 1 + library/std/src/path.rs | 1 + .../clippy/clippy_lints/src/manual_main_separator_str.rs | 4 ++-- src/tools/clippy/clippy_utils/src/paths.rs | 1 - 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index c9b36d77e1ac..cec833f2e562 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1425,6 +1425,7 @@ symbols! { pat_param, patchable_function_entry, path, + path_main_separator, path_to_pathbuf, pathbuf_as_path, pattern_complexity, diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 2ba3a9cdca25..aa9f63d915a2 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -263,6 +263,7 @@ pub fn is_separator(c: char) -> bool { /// /// For example, `/` on Unix and `\` on Windows. #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "path_main_separator")] pub const MAIN_SEPARATOR: char = crate::sys::path::MAIN_SEP; /// The primary separator of path components for the current platform. diff --git a/src/tools/clippy/clippy_lints/src/manual_main_separator_str.rs b/src/tools/clippy/clippy_lints/src/manual_main_separator_str.rs index db491a8c8f69..5198d7838a28 100644 --- a/src/tools/clippy/clippy_lints/src/manual_main_separator_str.rs +++ b/src/tools/clippy/clippy_lints/src/manual_main_separator_str.rs @@ -1,7 +1,7 @@ use clippy_config::msrvs::{self, Msrv}; use clippy_config::Conf; use clippy_utils::diagnostics::span_lint_and_sugg; -use clippy_utils::{is_trait_method, match_def_path, paths, peel_hir_expr_refs}; +use clippy_utils::{is_trait_method, peel_hir_expr_refs}; use rustc_errors::Applicability; use rustc_hir::def::{DefKind, Res}; use rustc_hir::{Expr, ExprKind, Mutability, QPath}; @@ -56,7 +56,7 @@ impl LateLintPass<'_> for ManualMainSeparatorStr { && let Res::Def(DefKind::Const, receiver_def_id) = path.res && is_trait_method(cx, target, sym::ToString) && self.msrv.meets(msrvs::PATH_MAIN_SEPARATOR_STR) - && match_def_path(cx, receiver_def_id, &paths::PATH_MAIN_SEPARATOR) + && cx.tcx.is_diagnostic_item(sym::path_main_separator, receiver_def_id) && let ty::Ref(_, ty, Mutability::Not) = cx.typeck_results().expr_ty_adjusted(expr).kind() && ty.is_str() { diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index 0a36d9a66649..5e8157ac0c30 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -40,7 +40,6 @@ pub const OPEN_OPTIONS_NEW: [&str; 4] = ["std", "fs", "OpenOptions", "new"]; pub const PARKING_LOT_MUTEX_GUARD: [&str; 3] = ["lock_api", "mutex", "MutexGuard"]; pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockReadGuard"]; pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockWriteGuard"]; -pub const PATH_MAIN_SEPARATOR: [&str; 3] = ["std", "path", "MAIN_SEPARATOR"]; #[cfg_attr(not(unix), allow(clippy::invalid_paths))] pub const PERMISSIONS_FROM_MODE: [&str; 6] = ["std", "os", "unix", "fs", "PermissionsExt", "from_mode"]; pub const PUSH_STR: [&str; 4] = ["alloc", "string", "String", "push_str"]; From 28f4c8293aa79989251d7035d312d13c8f374054 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 21:37:12 +0100 Subject: [PATCH 07/20] [Clippy] Swap `single_char_add_str`/`format_push_string` to use diagnostic items instead of paths --- compiler/rustc_span/src/symbol.rs | 2 ++ library/alloc/src/string.rs | 2 ++ src/tools/clippy/clippy_lints/src/format_push_string.rs | 4 ++-- .../clippy/clippy_lints/src/methods/single_char_add_str.rs | 6 +++--- src/tools/clippy/clippy_utils/src/paths.rs | 2 -- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index cec833f2e562..2194c4ac3b7f 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1882,6 +1882,8 @@ symbols! { string_as_mut_str, string_as_str, string_deref_patterns, + string_insert_str, + string_push_str, stringify, struct_field_attributes, struct_inherit, diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index a2c70de3ed0e..c7e9931ed2a5 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -1113,6 +1113,7 @@ impl String { #[inline] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_confusables("append", "push")] + #[cfg_attr(not(test), rustc_diagnostic_item = "string_push_str")] pub fn push_str(&mut self, string: &str) { self.vec.extend_from_slice(string.as_bytes()) } @@ -1747,6 +1748,7 @@ impl String { #[cfg(not(no_global_oom_handling))] #[inline] #[stable(feature = "insert_str", since = "1.16.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "string_insert_str")] pub fn insert_str(&mut self, idx: usize, string: &str) { assert!(self.is_char_boundary(idx)); diff --git a/src/tools/clippy/clippy_lints/src/format_push_string.rs b/src/tools/clippy/clippy_lints/src/format_push_string.rs index d05c5a01f41c..c6f03c3a7cf9 100644 --- a/src/tools/clippy/clippy_lints/src/format_push_string.rs +++ b/src/tools/clippy/clippy_lints/src/format_push_string.rs @@ -1,6 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::ty::is_type_lang_item; -use clippy_utils::{higher, match_def_path, paths}; +use clippy_utils::higher; use rustc_hir::{BinOpKind, Expr, ExprKind, LangItem, MatchSource}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::declare_lint_pass; @@ -70,7 +70,7 @@ impl<'tcx> LateLintPass<'tcx> for FormatPushString { let arg = match expr.kind { ExprKind::MethodCall(_, _, [arg], _) => { if let Some(fn_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) - && match_def_path(cx, fn_def_id, &paths::PUSH_STR) + && cx.tcx.is_diagnostic_item(sym::string_push_str, fn_def_id) { arg } else { diff --git a/src/tools/clippy/clippy_lints/src/methods/single_char_add_str.rs b/src/tools/clippy/clippy_lints/src/methods/single_char_add_str.rs index 81450fd8c6c3..ccdf5529d537 100644 --- a/src/tools/clippy/clippy_lints/src/methods/single_char_add_str.rs +++ b/src/tools/clippy/clippy_lints/src/methods/single_char_add_str.rs @@ -1,13 +1,13 @@ use crate::methods::{single_char_insert_string, single_char_push_string}; -use clippy_utils::{match_def_path, paths}; use rustc_hir as hir; use rustc_lint::LateContext; +use rustc_span::sym; pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, receiver: &hir::Expr<'_>, args: &[hir::Expr<'_>]) { if let Some(fn_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) { - if match_def_path(cx, fn_def_id, &paths::PUSH_STR) { + if cx.tcx.is_diagnostic_item(sym::string_push_str, fn_def_id) { single_char_push_string::check(cx, expr, receiver, args); - } else if match_def_path(cx, fn_def_id, &paths::INSERT_STR) { + } else if cx.tcx.is_diagnostic_item(sym::string_insert_str, fn_def_id) { single_char_insert_string::check(cx, expr, receiver, args); } } diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index 5e8157ac0c30..64540637cdbf 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -29,7 +29,6 @@ pub const HASHSET_ITER_TY: [&str; 5] = ["std", "collections", "hash", "set", "It pub const HASHSET_DRAIN: [&str; 5] = ["std", "collections", "hash", "set", "Drain"]; pub const IDENT: [&str; 3] = ["rustc_span", "symbol", "Ident"]; pub const IDENT_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Ident", "as_str"]; -pub const INSERT_STR: [&str; 4] = ["alloc", "string", "String", "insert_str"]; pub const ITERTOOLS_NEXT_TUPLE: [&str; 3] = ["itertools", "Itertools", "next_tuple"]; pub const KW_MODULE: [&str; 3] = ["rustc_span", "symbol", "kw"]; pub const LATE_CONTEXT: [&str; 2] = ["rustc_lint", "LateContext"]; @@ -42,7 +41,6 @@ pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwL pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockWriteGuard"]; #[cfg_attr(not(unix), allow(clippy::invalid_paths))] pub const PERMISSIONS_FROM_MODE: [&str; 6] = ["std", "os", "unix", "fs", "PermissionsExt", "from_mode"]; -pub const PUSH_STR: [&str; 4] = ["alloc", "string", "String", "push_str"]; pub const REGEX_BUILDER_NEW: [&str; 3] = ["regex", "RegexBuilder", "new"]; pub const REGEX_BYTES_BUILDER_NEW: [&str; 4] = ["regex", "bytes", "RegexBuilder", "new"]; pub const REGEX_BYTES_NEW: [&str; 4] = ["regex", "bytes", "Regex", "new"]; From 846ae57fc1853949ff95c537023ad9b4dad11504 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 21:42:38 +0100 Subject: [PATCH 08/20] [Clippy] Swap `VecArgs::hir` to use diagnostic items instead of paths --- compiler/rustc_span/src/symbol.rs | 3 +++ library/alloc/src/slice.rs | 1 + library/alloc/src/vec/mod.rs | 2 ++ .../clippy/clippy_lints/src/slow_vector_initialization.rs | 2 +- src/tools/clippy/clippy_utils/src/higher.rs | 8 ++++---- src/tools/clippy/clippy_utils/src/paths.rs | 3 --- 6 files changed, 11 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 2194c4ac3b7f..d449fac14bc2 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1836,6 +1836,7 @@ symbols! { slice, slice_from_raw_parts, slice_from_raw_parts_mut, + slice_into_vec, slice_iter, slice_len_fn, slice_patterns, @@ -2090,7 +2091,9 @@ symbols! { vec, vec_as_mut_slice, vec_as_slice, + vec_from_elem, vec_macro, + vec_new, vecdeque_iter, version, vfp2, diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs index 8cdba166c9df..45fb88969c69 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -496,6 +496,7 @@ impl [T] { #[rustc_allow_incoherent_impl] #[stable(feature = "rust1", since = "1.0.0")] #[inline] + #[cfg_attr(not(test), rustc_diagnostic_item = "slice_into_vec")] pub fn into_vec(self: Box) -> Vec { // N.B., see the `hack` module in this file for more details. hack::into_vec(self) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index ac0db38eac3e..29ddf939499a 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -416,6 +416,7 @@ impl Vec { /// ``` #[inline] #[rustc_const_stable(feature = "const_vec_new", since = "1.39.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "vec_new")] #[stable(feature = "rust1", since = "1.0.0")] #[must_use] pub const fn new() -> Self { @@ -3046,6 +3047,7 @@ impl Vec { #[doc(hidden)] #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "vec_from_elem")] pub fn from_elem(elem: T, n: usize) -> Vec { ::from_elem(elem, n, Global) } diff --git a/src/tools/clippy/clippy_lints/src/slow_vector_initialization.rs b/src/tools/clippy/clippy_lints/src/slow_vector_initialization.rs index b0e25c02265b..6c0b0e11c9e9 100644 --- a/src/tools/clippy/clippy_lints/src/slow_vector_initialization.rs +++ b/src/tools/clippy/clippy_lints/src/slow_vector_initialization.rs @@ -153,7 +153,7 @@ impl SlowVectorInit { && is_expr_path_def_path(cx, func, &paths::VEC_WITH_CAPACITY) { 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) } else { None diff --git a/src/tools/clippy/clippy_utils/src/higher.rs b/src/tools/clippy/clippy_utils/src/higher.rs index 8970b4d12298..e09cc9edf3ac 100644 --- a/src/tools/clippy/clippy_utils/src/higher.rs +++ b/src/tools/clippy/clippy_utils/src/higher.rs @@ -4,7 +4,7 @@ use crate::consts::{ConstEvalCtxt, Constant}; 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_hir as hir; @@ -297,10 +297,10 @@ impl<'a> VecArgs<'a> { && is_expn_of(fun.span, "vec").is_some() && 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 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 if let ExprKind::Call(_, [arg]) = &args[0].kind && let ExprKind::Array(args) = arg.kind @@ -309,7 +309,7 @@ impl<'a> VecArgs<'a> { } else { 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(&[])) } else { None diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index 64540637cdbf..1d5a22949789 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -49,7 +49,6 @@ pub const REGEX_NEW: [&str; 3] = ["regex", "Regex", "new"]; pub const REGEX_SET_NEW: [&str; 3] = ["regex", "RegexSet", "new"]; pub const SERDE_DESERIALIZE: [&str; 3] = ["serde", "de", "Deserialize"]; pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"]; -pub const SLICE_INTO_VEC: [&str; 4] = ["alloc", "slice", "", "into_vec"]; 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 STRING_NEW: [&str; 4] = ["alloc", "string", "String", "new"]; @@ -73,8 +72,6 @@ pub const TOKIO_IO_ASYNCWRITEEXT: [&str; 5] = ["tokio", "io", "util", "async_wri 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 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 INSTANT_NOW: [&str; 4] = ["std", "time", "Instant", "now"]; pub const VEC_IS_EMPTY: [&str; 4] = ["alloc", "vec", "Vec", "is_empty"]; From 15240a93c984fbd0f061066914547d12ce9e891c Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 21:53:28 +0100 Subject: [PATCH 09/20] [Clippy] Swap `repeat_vec_with_capacity` to use diagnostic item instead of path --- compiler/rustc_span/src/symbol.rs | 1 + library/alloc/src/vec/mod.rs | 1 + .../clippy/clippy_lints/src/repeat_vec_with_capacity.rs | 6 +++--- .../clippy/clippy_lints/src/slow_vector_initialization.rs | 6 +++--- src/tools/clippy/clippy_utils/src/paths.rs | 1 - 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index d449fac14bc2..c734cfd632a7 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -2094,6 +2094,7 @@ symbols! { vec_from_elem, vec_macro, vec_new, + vec_with_capacity, vecdeque_iter, version, vfp2, diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 29ddf939499a..a7e736299bed 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -477,6 +477,7 @@ impl Vec { #[inline] #[stable(feature = "rust1", since = "1.0.0")] #[must_use] + #[cfg_attr(not(test), rustc_diagnostic_item = "vec_with_capacity")] pub fn with_capacity(capacity: usize) -> Self { Self::with_capacity_in(capacity, Global) } diff --git a/src/tools/clippy/clippy_lints/src/repeat_vec_with_capacity.rs b/src/tools/clippy/clippy_lints/src/repeat_vec_with_capacity.rs index 678681ea4252..08de10f69b05 100644 --- a/src/tools/clippy/clippy_lints/src/repeat_vec_with_capacity.rs +++ b/src/tools/clippy/clippy_lints/src/repeat_vec_with_capacity.rs @@ -3,7 +3,7 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::higher::VecArgs; use clippy_utils::macros::matching_root_macro_call; 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_hir::{Expr, ExprKind}; 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<'_>) { 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) - && 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() && 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() && 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 - && 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() { emit_lint( diff --git a/src/tools/clippy/clippy_lints/src/slow_vector_initialization.rs b/src/tools/clippy/clippy_lints/src/slow_vector_initialization.rs index 6c0b0e11c9e9..04c16281ec4b 100644 --- a/src/tools/clippy/clippy_lints/src/slow_vector_initialization.rs +++ b/src/tools/clippy/clippy_lints/src/slow_vector_initialization.rs @@ -2,8 +2,8 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::macros::matching_root_macro_call; use clippy_utils::sugg::Sugg; use clippy_utils::{ - get_enclosing_block, is_expr_path_def_path, is_integer_literal, is_path_diagnostic_item, path_to_local, - path_to_local_id, paths, SpanlessEq, + get_enclosing_block, is_integer_literal, is_path_diagnostic_item, path_to_local, + path_to_local_id, SpanlessEq, }; use rustc_errors::Applicability; use rustc_hir::intravisit::{walk_block, walk_expr, walk_stmt, Visitor}; @@ -150,7 +150,7 @@ impl SlowVectorInit { } 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)) } else if matches!(expr.kind, ExprKind::Call(func, _) if is_path_diagnostic_item(cx, func, sym::vec_new)) { diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index 1d5a22949789..0a0749aa005f 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -72,7 +72,6 @@ pub const TOKIO_IO_ASYNCWRITEEXT: [&str; 5] = ["tokio", "io", "util", "async_wri 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 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 VEC_IS_EMPTY: [&str; 4] = ["alloc", "vec", "Vec", "is_empty"]; pub const VEC_POP: [&str; 4] = ["alloc", "vec", "Vec", "pop"]; From 25da0e2e5db45cb7d8d265ad90de6b4d213ce94c Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 21:59:31 +0100 Subject: [PATCH 10/20] [Clippy] Swap `manual_while_let_some` to use diagnostic items instead of paths --- compiler/rustc_span/src/symbol.rs | 4 ++++ library/alloc/src/vec/mod.rs | 2 ++ library/core/src/option.rs | 2 ++ .../src/loops/manual_while_let_some.rs | 14 +++++++------- src/tools/clippy/clippy_utils/src/paths.rs | 4 ---- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index c734cfd632a7..20991ce5d8c1 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1366,6 +1366,8 @@ symbols! { optin_builtin_traits, option, option_env, + option_expect, + option_unwrap, options, or, or_patterns, @@ -2092,8 +2094,10 @@ symbols! { vec_as_mut_slice, vec_as_slice, vec_from_elem, + vec_is_empty, vec_macro, vec_new, + vec_pop, vec_with_capacity, vecdeque_iter, version, diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index a7e736299bed..13b06584223b 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -2384,6 +2384,7 @@ impl Vec { /// Takes *O*(1) time. #[inline] #[stable(feature = "rust1", since = "1.0.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "vec_pop")] pub fn pop(&mut self) -> Option { if self.len == 0 { None @@ -2577,6 +2578,7 @@ impl Vec { /// assert!(!v.is_empty()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "vec_is_empty")] pub fn is_empty(&self) -> bool { self.len() == 0 } diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 265f056dc876..5ba13969605f 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -923,6 +923,7 @@ impl Option { #[inline] #[track_caller] #[stable(feature = "rust1", since = "1.0.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "option_expect")] #[rustc_const_unstable(feature = "const_option", issue = "67441")] pub const fn expect(self, msg: &str) -> T { match self { @@ -960,6 +961,7 @@ impl Option { #[inline(always)] #[track_caller] #[stable(feature = "rust1", since = "1.0.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "option_unwrap")] #[rustc_const_unstable(feature = "const_option", issue = "67441")] pub const fn unwrap(self) -> T { match self { diff --git a/src/tools/clippy/clippy_lints/src/loops/manual_while_let_some.rs b/src/tools/clippy/clippy_lints/src/loops/manual_while_let_some.rs index 57434f355444..55d1b9ee6767 100644 --- a/src/tools/clippy/clippy_lints/src/loops/manual_while_let_some.rs +++ b/src/tools/clippy/clippy_lints/src/loops/manual_while_let_some.rs @@ -1,10 +1,10 @@ use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::source::snippet; -use clippy_utils::{match_def_path, paths, SpanlessEq}; +use clippy_utils::SpanlessEq; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, Pat, Stmt, StmtKind, UnOp}; use rustc_lint::LateContext; -use rustc_span::Span; +use rustc_span::{sym, Symbol, Span}; use std::borrow::Cow; use super::MANUAL_WHILE_LET_SOME; @@ -47,20 +47,20 @@ fn report_lint(cx: &LateContext<'_>, pop_span: Span, pop_stmt_kind: PopStmt<'_>, ); } -fn match_method_call(cx: &LateContext<'_>, expr: &Expr<'_>, method: &[&str]) -> bool { +fn match_method_call(cx: &LateContext<'_>, expr: &Expr<'_>, method: Symbol) -> bool { if let ExprKind::MethodCall(..) = expr.kind && let Some(id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) { - match_def_path(cx, id, method) + cx.tcx.is_diagnostic_item(method, id) } else { false } } fn is_vec_pop_unwrap(cx: &LateContext<'_>, expr: &Expr<'_>, is_empty_recv: &Expr<'_>) -> bool { - if (match_method_call(cx, expr, &paths::OPTION_UNWRAP) || match_method_call(cx, expr, &paths::OPTION_EXPECT)) + if (match_method_call(cx, expr, sym::option_unwrap) || match_method_call(cx, expr, sym::option_expect)) && let ExprKind::MethodCall(_, unwrap_recv, ..) = expr.kind - && match_method_call(cx, unwrap_recv, &paths::VEC_POP) + && match_method_call(cx, unwrap_recv, sym::vec_pop) && let ExprKind::MethodCall(_, pop_recv, ..) = unwrap_recv.kind { // make sure they're the same `Vec` @@ -96,7 +96,7 @@ fn check_call_arguments(cx: &LateContext<'_>, stmt: &Stmt<'_>, is_empty_recv: &E pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, full_cond: &'tcx Expr<'_>, body: &'tcx Expr<'_>, loop_span: Span) { if let ExprKind::Unary(UnOp::Not, cond) = full_cond.kind && let ExprKind::MethodCall(_, is_empty_recv, _, _) = cond.kind - && match_method_call(cx, cond, &paths::VEC_IS_EMPTY) + && match_method_call(cx, cond, sym::vec_is_empty) && let ExprKind::Block(body, _) = body.kind && let Some(stmt) = body.stmts.first() { diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index 0a0749aa005f..4a93939d0cce 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -73,9 +73,5 @@ pub const TOKIO_IO_OPEN_OPTIONS: [&str; 4] = ["tokio", "fs", "open_options", "Op #[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 INSTANT_NOW: [&str; 4] = ["std", "time", "Instant", "now"]; -pub const VEC_IS_EMPTY: [&str; 4] = ["alloc", "vec", "Vec", "is_empty"]; -pub const VEC_POP: [&str; 4] = ["alloc", "vec", "Vec", "pop"]; pub const WAKER: [&str; 4] = ["core", "task", "wake", "Waker"]; -pub const OPTION_UNWRAP: [&str; 4] = ["core", "option", "Option", "unwrap"]; -pub const OPTION_EXPECT: [&str; 4] = ["core", "option", "Option", "expect"]; pub const BOOL_THEN: [&str; 4] = ["core", "bool", "", "then"]; From 5b552702258e537fea1d95119ff1d309697bdadb Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 22:01:52 +0100 Subject: [PATCH 11/20] [Clippy] Swap `filter_map_bool_then` to use diagnostic item instead of path --- compiler/rustc_span/src/symbol.rs | 1 + library/core/src/bool.rs | 1 + .../clippy/clippy_lints/src/methods/filter_map_bool_then.rs | 5 ++--- src/tools/clippy/clippy_utils/src/paths.rs | 1 - 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 20991ce5d8c1..e795b7fb25c6 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -501,6 +501,7 @@ symbols! { black_box, block, bool, + bool_then, borrowck_graphviz_format, borrowck_graphviz_postflow, box_new, diff --git a/library/core/src/bool.rs b/library/core/src/bool.rs index 03cdff9b13be..58a870d2e072 100644 --- a/library/core/src/bool.rs +++ b/library/core/src/bool.rs @@ -55,6 +55,7 @@ impl bool { /// assert_eq!(a, 1); /// ``` #[stable(feature = "lazy_bool_to_option", since = "1.50.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "bool_then")] #[inline] pub fn then T>(self, f: F) -> Option { if self { Some(f()) } else { None } diff --git a/src/tools/clippy/clippy_lints/src/methods/filter_map_bool_then.rs b/src/tools/clippy/clippy_lints/src/methods/filter_map_bool_then.rs index 4fbf661727da..77a62fbb4fb9 100644 --- a/src/tools/clippy/clippy_lints/src/methods/filter_map_bool_then.rs +++ b/src/tools/clippy/clippy_lints/src/methods/filter_map_bool_then.rs @@ -1,9 +1,8 @@ use super::FILTER_MAP_BOOL_THEN; use clippy_utils::diagnostics::span_lint_and_sugg; -use clippy_utils::paths::BOOL_THEN; use clippy_utils::source::SpanRangeExt; use clippy_utils::ty::is_copy; -use clippy_utils::{is_from_proc_macro, is_trait_method, match_def_path, peel_blocks}; +use clippy_utils::{is_from_proc_macro, is_trait_method, peel_blocks}; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LintContext}; @@ -35,7 +34,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: & && let ExprKind::Closure(then_closure) = then_arg.kind && let then_body = peel_blocks(cx.tcx.hir().body(then_closure.body).value) && let Some(def_id) = cx.typeck_results().type_dependent_def_id(value.hir_id) - && match_def_path(cx, def_id, &BOOL_THEN) + && cx.tcx.is_diagnostic_item(sym::bool_then, def_id) && !is_from_proc_macro(cx, expr) // Count the number of derefs needed to get to the bool because we need those in the suggestion && let needed_derefs = cx.typeck_results().expr_adjustments(recv) diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index 4a93939d0cce..cb0330d00461 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -74,4 +74,3 @@ pub const TOKIO_IO_OPEN_OPTIONS: [&str; 4] = ["tokio", "fs", "open_options", "Op pub const TOKIO_IO_OPEN_OPTIONS_NEW: [&str; 5] = ["tokio", "fs", "open_options", "OpenOptions", "new"]; pub const INSTANT_NOW: [&str; 4] = ["std", "time", "Instant", "now"]; pub const WAKER: [&str; 4] = ["core", "task", "wake", "Waker"]; -pub const BOOL_THEN: [&str; 4] = ["core", "bool", "", "then"]; From 372f68b6a6e0b6997d065cef4f6cef16a6fb53e3 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 22:05:02 +0100 Subject: [PATCH 12/20] [Clippy] Swap `waker_clone_wake` to use diagnostic item instead of path --- compiler/rustc_span/src/symbol.rs | 1 + library/core/src/task/wake.rs | 1 + src/tools/clippy/clippy_lints/src/methods/waker_clone_wake.rs | 4 ++-- src/tools/clippy/clippy_utils/src/paths.rs | 1 - 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index e795b7fb25c6..8a3ec7a8f718 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -342,6 +342,7 @@ symbols! { Upvars, Vec, VecDeque, + Waker, Wrapper, Wrapping, Yield, diff --git a/library/core/src/task/wake.rs b/library/core/src/task/wake.rs index 5e559ad8d2ca..a5103499c8a0 100644 --- a/library/core/src/task/wake.rs +++ b/library/core/src/task/wake.rs @@ -414,6 +414,7 @@ impl<'a> ContextBuilder<'a> { /// [`Wake`]: ../../alloc/task/trait.Wake.html #[repr(transparent)] #[stable(feature = "futures_api", since = "1.36.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "Waker")] pub struct Waker { waker: RawWaker, } diff --git a/src/tools/clippy/clippy_lints/src/methods/waker_clone_wake.rs b/src/tools/clippy/clippy_lints/src/methods/waker_clone_wake.rs index da66632d55f5..9b64cc7589cc 100644 --- a/src/tools/clippy/clippy_lints/src/methods/waker_clone_wake.rs +++ b/src/tools/clippy/clippy_lints/src/methods/waker_clone_wake.rs @@ -1,6 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::snippet_with_applicability; -use clippy_utils::{is_trait_method, match_def_path, paths}; +use clippy_utils::is_trait_method; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::LateContext; @@ -12,7 +12,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &' let ty = cx.typeck_results().expr_ty(recv); if let Some(did) = ty.ty_adt_def() - && match_def_path(cx, did.did(), &paths::WAKER) + && cx.tcx.is_diagnostic_item(sym::Waker, did.did()) && let ExprKind::MethodCall(_, waker_ref, &[], _) = recv.kind && is_trait_method(cx, recv, sym::Clone) { diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index cb0330d00461..89c1bd8a8a03 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -73,4 +73,3 @@ pub const TOKIO_IO_OPEN_OPTIONS: [&str; 4] = ["tokio", "fs", "open_options", "Op #[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 INSTANT_NOW: [&str; 4] = ["std", "time", "Instant", "now"]; -pub const WAKER: [&str; 4] = ["core", "task", "wake", "Waker"]; From 1890620b2611aaec28edc5537183a2c7734ae3ce Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 22:08:04 +0100 Subject: [PATCH 13/20] [Clippy] Swap `instant_subtraction` to use diagnostic item instead of path --- compiler/rustc_span/src/symbol.rs | 1 + library/std/src/time.rs | 1 + src/tools/clippy/clippy_lints/src/instant_subtraction.rs | 2 +- src/tools/clippy/clippy_utils/src/paths.rs | 1 - 4 files changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 8a3ec7a8f718..bac5bb965690 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1061,6 +1061,7 @@ symbols! { inline_const, inline_const_pat, inout, + instant_now, instruction_set, integer_: "integer", // underscore to avoid clashing with the function `sym::integer` below integral, diff --git a/library/std/src/time.rs b/library/std/src/time.rs index ae46670c25e6..f28a0568a3c3 100644 --- a/library/std/src/time.rs +++ b/library/std/src/time.rs @@ -280,6 +280,7 @@ impl Instant { /// ``` #[must_use] #[stable(feature = "time2", since = "1.8.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "instant_now")] pub fn now() -> Instant { Instant(time::Instant::now()) } diff --git a/src/tools/clippy/clippy_lints/src/instant_subtraction.rs b/src/tools/clippy/clippy_lints/src/instant_subtraction.rs index f41fdf3203c9..6d6820311b67 100644 --- a/src/tools/clippy/clippy_lints/src/instant_subtraction.rs +++ b/src/tools/clippy/clippy_lints/src/instant_subtraction.rs @@ -112,7 +112,7 @@ impl LateLintPass<'_> for InstantSubtraction { fn is_instant_now_call(cx: &LateContext<'_>, expr_block: &'_ Expr<'_>) -> bool { if let ExprKind::Call(fn_expr, []) = expr_block.kind && let Some(fn_id) = clippy_utils::path_def_id(cx, fn_expr) - && clippy_utils::match_def_path(cx, fn_id, &clippy_utils::paths::INSTANT_NOW) + && cx.tcx.is_diagnostic_item(sym::instant_now, fn_id) { true } else { diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index 89c1bd8a8a03..26f680e2666a 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -72,4 +72,3 @@ pub const TOKIO_IO_ASYNCWRITEEXT: [&str; 5] = ["tokio", "io", "util", "async_wri 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 pub const TOKIO_IO_OPEN_OPTIONS_NEW: [&str; 5] = ["tokio", "fs", "open_options", "OpenOptions", "new"]; -pub const INSTANT_NOW: [&str; 4] = ["std", "time", "Instant", "now"]; From 89532c0f30ab781351c1cfc59ad155a25ca4e102 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 22:10:05 +0100 Subject: [PATCH 14/20] [Clippy] Swap `unnecessary_to_owned` to use diagnostic item instead of path --- compiler/rustc_span/src/symbol.rs | 1 + library/alloc/src/string.rs | 1 + .../clippy/clippy_lints/src/methods/unnecessary_to_owned.rs | 4 ++-- src/tools/clippy/clippy_utils/src/paths.rs | 1 - 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index bac5bb965690..7c3544639b9b 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1888,6 +1888,7 @@ symbols! { string_as_mut_str, string_as_str, string_deref_patterns, + string_from_utf8, string_insert_str, string_push_str, stringify, diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index c7e9931ed2a5..0bad33c909d6 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -571,6 +571,7 @@ impl String { /// [`into_bytes`]: String::into_bytes #[inline] #[stable(feature = "rust1", since = "1.0.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "string_from_utf8")] pub fn from_utf8(vec: Vec) -> Result { match str::from_utf8(&vec) { Ok(..) => Ok(String { vec }), diff --git a/src/tools/clippy/clippy_lints/src/methods/unnecessary_to_owned.rs b/src/tools/clippy/clippy_lints/src/methods/unnecessary_to_owned.rs index 69c5bc57e299..bf7555a29e22 100644 --- a/src/tools/clippy/clippy_lints/src/methods/unnecessary_to_owned.rs +++ b/src/tools/clippy/clippy_lints/src/methods/unnecessary_to_owned.rs @@ -6,7 +6,7 @@ use clippy_utils::source::{snippet, SpanRangeExt}; use clippy_utils::ty::{get_iterator_item_ty, implements_trait, is_copy, is_type_diagnostic_item, is_type_lang_item}; use clippy_utils::visitors::find_all_ret_expressions; use clippy_utils::{ - fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item, match_def_path, paths, peel_middle_ty_refs, + fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item, peel_middle_ty_refs, return_ty, }; use rustc_errors::Applicability; @@ -250,7 +250,7 @@ fn check_string_from_utf8<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, if let Some((call, arg)) = skip_addr_of_ancestors(cx, expr) && !arg.span.from_expansion() && let ExprKind::Call(callee, _) = call.kind - && fn_def_id(cx, call).is_some_and(|did| match_def_path(cx, did, &paths::STRING_FROM_UTF8)) + && fn_def_id(cx, call).is_some_and(|did| cx.tcx.is_diagnostic_item(sym::string_from_utf8, did)) && let Some(unwrap_call) = get_parent_expr(cx, call) && let ExprKind::MethodCall(unwrap_method_name, ..) = unwrap_call.kind && matches!(unwrap_method_name.ident.name, sym::unwrap | sym::expect) diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index 26f680e2666a..646c7bc5df85 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -61,7 +61,6 @@ pub const SYMBOL_INTERN: [&str; 4] = ["rustc_span", "symbol", "Symbol", "intern" pub const SYMBOL_TO_IDENT_STRING: [&str; 4] = ["rustc_span", "symbol", "Symbol", "to_ident_string"]; pub const SYM_MODULE: [&str; 3] = ["rustc_span", "symbol", "sym"]; pub const SYNTAX_CONTEXT: [&str; 3] = ["rustc_span", "hygiene", "SyntaxContext"]; -pub const STRING_FROM_UTF8: [&str; 4] = ["alloc", "string", "String", "from_utf8"]; #[expect(clippy::invalid_paths)] // internal lints do not know about all external crates pub const TOKIO_FILE_OPTIONS: [&str; 5] = ["tokio", "fs", "file", "File", "options"]; #[expect(clippy::invalid_paths)] // internal lints do not know about all external crates From 5f42ae13c1f5b99cb35aadb4c20376ba64e69ffb Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 22:13:50 +0100 Subject: [PATCH 15/20] [Clippy] Swap `manual_strip` to use diagnostic items instead of paths --- compiler/rustc_span/src/symbol.rs | 3 +++ library/core/src/str/mod.rs | 3 +++ src/tools/clippy/clippy_lints/src/manual_strip.rs | 10 +++++----- src/tools/clippy/clippy_utils/src/paths.rs | 3 --- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 7c3544639b9b..69c6562ba935 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1876,11 +1876,14 @@ symbols! { store, str, str_chars, + str_ends_with, str_from_utf8, str_from_utf8_mut, str_from_utf8_unchecked, str_from_utf8_unchecked_mut, + str_len, str_split_whitespace, + str_starts_with, str_trim, str_trim_end, str_trim_start, diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs index 9373d807f44e..712bf011c273 100644 --- a/library/core/src/str/mod.rs +++ b/library/core/src/str/mod.rs @@ -134,6 +134,7 @@ impl str { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_stable(feature = "const_str_len", since = "1.39.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "str_len")] #[must_use] #[inline] pub const fn len(&self) -> usize { @@ -1157,6 +1158,7 @@ impl str { /// assert!(bananas.starts_with(&['a', 'b', 'c', 'd'])); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "str_starts_with")] pub fn starts_with(&self, pat: P) -> bool { pat.is_prefix_of(self) } @@ -1181,6 +1183,7 @@ impl str { /// assert!(!bananas.ends_with("nana")); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "str_ends_with")] pub fn ends_with(&self, pat: P) -> bool where for<'a> P::Searcher<'a>: ReverseSearcher<'a>, diff --git a/src/tools/clippy/clippy_lints/src/manual_strip.rs b/src/tools/clippy/clippy_lints/src/manual_strip.rs index 85cabd2800a6..02d433ecc5aa 100644 --- a/src/tools/clippy/clippy_lints/src/manual_strip.rs +++ b/src/tools/clippy/clippy_lints/src/manual_strip.rs @@ -4,7 +4,7 @@ use clippy_utils::consts::{ConstEvalCtxt, Constant}; use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::source::snippet; use clippy_utils::usage::mutated_variables; -use clippy_utils::{eq_expr_value, higher, match_def_path, paths}; +use clippy_utils::{eq_expr_value, higher}; use rustc_ast::ast::LitKind; use rustc_errors::Applicability; use rustc_hir::def::Res; @@ -14,7 +14,7 @@ use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty; use rustc_session::impl_lint_pass; use rustc_span::source_map::Spanned; -use rustc_span::Span; +use rustc_span::{sym, Span}; use std::iter; declare_clippy_lint! { @@ -76,9 +76,9 @@ impl<'tcx> LateLintPass<'tcx> for ManualStrip { && self.msrv.meets(msrvs::STR_STRIP_PREFIX) && let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(cond.hir_id) { - let strip_kind = if match_def_path(cx, method_def_id, &paths::STR_STARTS_WITH) { + let strip_kind = if cx.tcx.is_diagnostic_item(sym::str_starts_with, method_def_id) { StripKind::Prefix - } else if match_def_path(cx, method_def_id, &paths::STR_ENDS_WITH) { + } else if cx.tcx.is_diagnostic_item(sym::str_ends_with, method_def_id) { StripKind::Suffix } else { return; @@ -137,7 +137,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualStrip { fn len_arg<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> { if let ExprKind::MethodCall(_, arg, [], _) = expr.kind && let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) - && match_def_path(cx, method_def_id, &paths::STR_LEN) + && cx.tcx.is_diagnostic_item(sym::str_len, method_def_id) { Some(arg) } else { diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index 646c7bc5df85..986adddc6aec 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -52,9 +52,6 @@ pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"]; 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 STRING_NEW: [&str; 4] = ["alloc", "string", "String", "new"]; -pub const STR_ENDS_WITH: [&str; 4] = ["core", "str", "", "ends_with"]; -pub const STR_LEN: [&str; 4] = ["core", "str", "", "len"]; -pub const STR_STARTS_WITH: [&str; 4] = ["core", "str", "", "starts_with"]; pub const SYMBOL: [&str; 3] = ["rustc_span", "symbol", "Symbol"]; pub const SYMBOL_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Symbol", "as_str"]; pub const SYMBOL_INTERN: [&str; 4] = ["rustc_span", "symbol", "Symbol", "intern"]; From 5f85f73f6373e7aff382b72af279e2c0be556390 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 22:38:27 +0100 Subject: [PATCH 16/20] [Clippy] Swap `unnecessary_owned_empty_strings` to use diagnostic item instead of path --- compiler/rustc_span/src/symbol.rs | 1 + library/alloc/src/string.rs | 1 + .../clippy/clippy_lints/src/unnecessary_owned_empty_strings.rs | 3 +-- src/tools/clippy/clippy_utils/src/paths.rs | 1 - 4 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 69c6562ba935..5bc2c0491142 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1893,6 +1893,7 @@ symbols! { string_deref_patterns, string_from_utf8, string_insert_str, + string_new, string_push_str, stringify, struct_field_attributes, diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 0bad33c909d6..d58a016b502c 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -440,6 +440,7 @@ impl String { /// ``` #[inline] #[rustc_const_stable(feature = "const_string_new", since = "1.39.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "string_new")] #[stable(feature = "rust1", since = "1.0.0")] #[must_use] pub const fn new() -> String { diff --git a/src/tools/clippy/clippy_lints/src/unnecessary_owned_empty_strings.rs b/src/tools/clippy/clippy_lints/src/unnecessary_owned_empty_strings.rs index 6b5e6c6ab209..f01cb457af89 100644 --- a/src/tools/clippy/clippy_lints/src/unnecessary_owned_empty_strings.rs +++ b/src/tools/clippy/clippy_lints/src/unnecessary_owned_empty_strings.rs @@ -1,6 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::ty::is_type_lang_item; -use clippy_utils::{match_def_path, paths}; use rustc_ast::ast::LitKind; use rustc_errors::Applicability; use rustc_hir::{BorrowKind, Expr, ExprKind, LangItem, Mutability}; @@ -42,7 +41,7 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryOwnedEmptyStrings { && let ty::Ref(_, inner_str, _) = cx.typeck_results().expr_ty_adjusted(expr).kind() && inner_str.is_str() { - if match_def_path(cx, fun_def_id, &paths::STRING_NEW) { + if cx.tcx.is_diagnostic_item(sym::string_new, fun_def_id) { span_lint_and_sugg( cx, UNNECESSARY_OWNED_EMPTY_STRINGS, diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index 986adddc6aec..dd304e7aae87 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -51,7 +51,6 @@ pub const SERDE_DESERIALIZE: [&str; 3] = ["serde", "de", "Deserialize"]; pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"]; 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 STRING_NEW: [&str; 4] = ["alloc", "string", "String", "new"]; pub const SYMBOL: [&str; 3] = ["rustc_span", "symbol", "Symbol"]; pub const SYMBOL_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Symbol", "as_str"]; pub const SYMBOL_INTERN: [&str; 4] = ["rustc_span", "symbol", "Symbol", "intern"]; From 43b8e04d463477e3b990f9e6fd7551975256e60d Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 23:01:37 +0100 Subject: [PATCH 17/20] [Clippy] Swap `non_octal_unix_permissions` to use diagnostic item instead of path --- compiler/rustc_span/src/symbol.rs | 1 + library/std/src/os/unix/fs.rs | 1 + .../clippy/clippy_lints/src/non_octal_unix_permissions.rs | 3 +-- src/tools/clippy/clippy_utils/src/paths.rs | 2 -- 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 5bc2c0491142..e0bfab53e76b 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1437,6 +1437,7 @@ symbols! { pattern_parentheses, pattern_type, pattern_types, + permissions_from_mode, phantom_data, pic, pie, diff --git a/library/std/src/os/unix/fs.rs b/library/std/src/os/unix/fs.rs index caf6980afd91..a964db2e0ac3 100644 --- a/library/std/src/os/unix/fs.rs +++ b/library/std/src/os/unix/fs.rs @@ -334,6 +334,7 @@ pub trait PermissionsExt { /// assert_eq!(permissions.mode(), 0o644); /// ``` #[stable(feature = "fs_ext", since = "1.1.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "permissions_from_mode")] fn from_mode(mode: u32) -> Self; } diff --git a/src/tools/clippy/clippy_lints/src/non_octal_unix_permissions.rs b/src/tools/clippy/clippy_lints/src/non_octal_unix_permissions.rs index b915df527629..cfc15d927158 100644 --- a/src/tools/clippy/clippy_lints/src/non_octal_unix_permissions.rs +++ b/src/tools/clippy/clippy_lints/src/non_octal_unix_permissions.rs @@ -1,6 +1,5 @@ use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::{snippet_with_applicability, SpanRangeExt}; -use clippy_utils::{match_def_path, paths}; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; @@ -63,7 +62,7 @@ impl<'tcx> LateLintPass<'tcx> for NonOctalUnixPermissions { ExprKind::Call(func, [param]) => { if let ExprKind::Path(ref path) = func.kind && let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id() - && match_def_path(cx, def_id, &paths::PERMISSIONS_FROM_MODE) + && cx.tcx.is_diagnostic_item(sym::permissions_from_mode, def_id) && let ExprKind::Lit(_) = param.kind && param.span.eq_ctxt(expr.span) && param diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index dd304e7aae87..c7b1c01de1d8 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -39,8 +39,6 @@ pub const OPEN_OPTIONS_NEW: [&str; 4] = ["std", "fs", "OpenOptions", "new"]; pub const PARKING_LOT_MUTEX_GUARD: [&str; 3] = ["lock_api", "mutex", "MutexGuard"]; pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockReadGuard"]; pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockWriteGuard"]; -#[cfg_attr(not(unix), allow(clippy::invalid_paths))] -pub const PERMISSIONS_FROM_MODE: [&str; 6] = ["std", "os", "unix", "fs", "PermissionsExt", "from_mode"]; pub const REGEX_BUILDER_NEW: [&str; 3] = ["regex", "RegexBuilder", "new"]; pub const REGEX_BYTES_BUILDER_NEW: [&str; 4] = ["regex", "bytes", "RegexBuilder", "new"]; pub const REGEX_BYTES_NEW: [&str; 4] = ["regex", "bytes", "Regex", "new"]; From 364e5529403bff3847fc43cc18ff4c5bb357e784 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 23:21:35 +0100 Subject: [PATCH 18/20] [Clippy] Swap `iter_over_hash_type` to use diagnostic items instead of paths --- compiler/rustc_span/src/symbol.rs | 8 ++++ library/std/src/collections/hash/map.rs | 6 +++ library/std/src/collections/hash/set.rs | 2 + .../clippy_lints/src/iter_over_hash_type.rs | 38 +++++++------------ src/tools/clippy/clippy_utils/src/paths.rs | 8 ---- 5 files changed, 30 insertions(+), 32 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index e0bfab53e76b..bc973bac63ac 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -981,8 +981,16 @@ symbols! { half_open_range_patterns_in_slices, hash, hashmap_contains_key, + hashmap_drain_ty, hashmap_insert, + hashmap_iter_mut_ty, + hashmap_iter_ty, + hashmap_keys_ty, + hashmap_values_mut_ty, + hashmap_values_ty, + hashset_drain_ty, hashset_iter, + hashset_iter_ty, hexagon_target_feature, hidden, homogeneous_aggregate, diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index 0d7346c7f1ea..1a18721b15e9 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -1393,6 +1393,7 @@ where /// let iter = map.iter(); /// ``` #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_iter_ty")] pub struct Iter<'a, K: 'a, V: 'a> { base: base::Iter<'a, K, V>, } @@ -1431,6 +1432,7 @@ impl fmt::Debug for Iter<'_, K, V> { /// let iter = map.iter_mut(); /// ``` #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_iter_mut_ty")] pub struct IterMut<'a, K: 'a, V: 'a> { base: base::IterMut<'a, K, V>, } @@ -1491,6 +1493,7 @@ impl IntoIter { /// let iter_keys = map.keys(); /// ``` #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_keys_ty")] pub struct Keys<'a, K: 'a, V: 'a> { inner: Iter<'a, K, V>, } @@ -1529,6 +1532,7 @@ impl fmt::Debug for Keys<'_, K, V> { /// let iter_values = map.values(); /// ``` #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_values_ty")] pub struct Values<'a, K: 'a, V: 'a> { inner: Iter<'a, K, V>, } @@ -1567,6 +1571,7 @@ impl fmt::Debug for Values<'_, K, V> { /// let iter = map.drain(); /// ``` #[stable(feature = "drain", since = "1.6.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_drain_ty")] pub struct Drain<'a, K: 'a, V: 'a> { base: base::Drain<'a, K, V>, } @@ -1624,6 +1629,7 @@ where /// let iter_values = map.values_mut(); /// ``` #[stable(feature = "map_values_mut", since = "1.10.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_values_mut_ty")] pub struct ValuesMut<'a, K: 'a, V: 'a> { inner: IterMut<'a, K, V>, } diff --git a/library/std/src/collections/hash/set.rs b/library/std/src/collections/hash/set.rs index e63d90645f77..4a113ddea3a6 100644 --- a/library/std/src/collections/hash/set.rs +++ b/library/std/src/collections/hash/set.rs @@ -1271,6 +1271,7 @@ where /// let mut iter = a.iter(); /// ``` #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "hashset_iter_ty")] pub struct Iter<'a, K: 'a> { base: base::Iter<'a, K>, } @@ -1313,6 +1314,7 @@ pub struct IntoIter { /// let mut drain = a.drain(); /// ``` #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "hashset_drain_ty")] pub struct Drain<'a, K: 'a> { base: base::Drain<'a, K>, } diff --git a/src/tools/clippy/clippy_lints/src/iter_over_hash_type.rs b/src/tools/clippy/clippy_lints/src/iter_over_hash_type.rs index fb29d9824179..f162948bb445 100644 --- a/src/tools/clippy/clippy_lints/src/iter_over_hash_type.rs +++ b/src/tools/clippy/clippy_lints/src/iter_over_hash_type.rs @@ -1,10 +1,5 @@ use clippy_utils::diagnostics::span_lint; use clippy_utils::higher::ForLoop; -use clippy_utils::match_any_def_paths; -use clippy_utils::paths::{ - HASHMAP_DRAIN, HASHMAP_ITER, HASHMAP_ITER_MUT, HASHMAP_KEYS, HASHMAP_VALUES, HASHMAP_VALUES_MUT, HASHSET_DRAIN, - HASHSET_ITER_TY, -}; use clippy_utils::ty::is_type_diagnostic_item; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::declare_lint_pass; @@ -44,28 +39,23 @@ declare_lint_pass!(IterOverHashType => [ITER_OVER_HASH_TYPE]); impl LateLintPass<'_> for IterOverHashType { fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>) { + let hash_iter_tys = [ + sym::HashMap, + sym::HashSet, + sym::hashmap_keys_ty, + sym::hashmap_values_ty, + sym::hashmap_values_mut_ty, + sym::hashmap_iter_ty, + sym::hashmap_iter_mut_ty, + sym::hashmap_drain_ty, + sym::hashset_iter_ty, + sym::hashset_drain_ty, + ]; + if let Some(for_loop) = ForLoop::hir(expr) && !for_loop.body.span.from_expansion() && let ty = cx.typeck_results().expr_ty(for_loop.arg).peel_refs() - && let Some(adt) = ty.ty_adt_def() - && let did = adt.did() - && (match_any_def_paths( - cx, - did, - &[ - &HASHMAP_KEYS, - &HASHMAP_VALUES, - &HASHMAP_VALUES_MUT, - &HASHMAP_ITER, - &HASHMAP_ITER_MUT, - &HASHMAP_DRAIN, - &HASHSET_ITER_TY, - &HASHSET_DRAIN, - ], - ) - .is_some() - || is_type_diagnostic_item(cx, ty, sym::HashMap) - || is_type_diagnostic_item(cx, ty, sym::HashSet)) + && hash_iter_tys.into_iter().any(|sym| is_type_diagnostic_item(cx, ty, sym)) { span_lint( cx, diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index c7b1c01de1d8..ccaed3057f4d 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -19,14 +19,6 @@ pub const FILE_OPTIONS: [&str; 4] = ["std", "fs", "File", "options"]; pub const FUTURES_IO_ASYNCREADEXT: [&str; 3] = ["futures_util", "io", "AsyncReadExt"]; #[expect(clippy::invalid_paths)] // internal lints do not know about all external crates pub const FUTURES_IO_ASYNCWRITEEXT: [&str; 3] = ["futures_util", "io", "AsyncWriteExt"]; -pub const HASHMAP_ITER: [&str; 5] = ["std", "collections", "hash", "map", "Iter"]; -pub const HASHMAP_ITER_MUT: [&str; 5] = ["std", "collections", "hash", "map", "IterMut"]; -pub const HASHMAP_KEYS: [&str; 5] = ["std", "collections", "hash", "map", "Keys"]; -pub const HASHMAP_VALUES: [&str; 5] = ["std", "collections", "hash", "map", "Values"]; -pub const HASHMAP_DRAIN: [&str; 5] = ["std", "collections", "hash", "map", "Drain"]; -pub const HASHMAP_VALUES_MUT: [&str; 5] = ["std", "collections", "hash", "map", "ValuesMut"]; -pub const HASHSET_ITER_TY: [&str; 5] = ["std", "collections", "hash", "set", "Iter"]; -pub const HASHSET_DRAIN: [&str; 5] = ["std", "collections", "hash", "set", "Drain"]; pub const IDENT: [&str; 3] = ["rustc_span", "symbol", "Ident"]; pub const IDENT_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Ident", "as_str"]; pub const ITERTOOLS_NEXT_TUPLE: [&str; 3] = ["itertools", "Itertools", "next_tuple"]; From b2eebeeea982c544f6740fb3883ea046ded39e7f Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Thu, 19 Sep 2024 12:19:48 +0100 Subject: [PATCH 19/20] [Clippy] Swap `open_options` to use diagnostic items instead of paths --- compiler/rustc_span/src/symbol.rs | 2 ++ library/std/src/fs.rs | 2 ++ .../clippy_lints/src/methods/open_options.rs | 23 ++++++++++--------- src/tools/clippy/clippy_utils/src/paths.rs | 2 -- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index bc973bac63ac..402232a1720b 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -895,6 +895,7 @@ symbols! { field, field_init_shorthand, file, + file_options, float, float_to_int_unchecked, floorf128, @@ -1370,6 +1371,7 @@ symbols! { on, on_unimplemented, opaque, + open_options_new, ops, opt_out_copy, optimize, diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index abf7d5d294af..92d3838d9f28 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -466,6 +466,7 @@ impl File { /// ``` #[must_use] #[stable(feature = "with_options", since = "1.58.0")] + #[cfg_attr(not(test), rustc_diagnostic_item = "file_options")] pub fn options() -> OpenOptions { OpenOptions::new() } @@ -1009,6 +1010,7 @@ impl OpenOptions { /// let mut options = OpenOptions::new(); /// let file = options.read(true).open("foo.txt"); /// ``` + #[cfg_attr(not(test), rustc_diagnostic_item = "open_options_new")] #[stable(feature = "rust1", since = "1.0.0")] #[must_use] pub fn new() -> Self { diff --git a/src/tools/clippy/clippy_lints/src/methods/open_options.rs b/src/tools/clippy/clippy_lints/src/methods/open_options.rs index cbeb48b6cc37..f1a3c81cebbd 100644 --- a/src/tools/clippy/clippy_lints/src/methods/open_options.rs +++ b/src/tools/clippy/clippy_lints/src/methods/open_options.rs @@ -126,17 +126,18 @@ fn get_open_options( && let ExprKind::Path(path) = callee.kind && let Some(did) = cx.qpath_res(&path, callee.hir_id).opt_def_id() { - match_any_def_paths( - cx, - did, - &[ - &paths::TOKIO_IO_OPEN_OPTIONS_NEW, - &paths::OPEN_OPTIONS_NEW, - &paths::FILE_OPTIONS, - &paths::TOKIO_FILE_OPTIONS, - ], - ) - .is_some() + let std_file_options = [ + sym::file_options, + sym::open_options_new, + ]; + + let tokio_file_options: &[&[&str]] = &[ + &paths::TOKIO_IO_OPEN_OPTIONS_NEW, + &paths::TOKIO_FILE_OPTIONS, + ]; + + let is_std_options = std_file_options.into_iter().any(|sym| cx.tcx.is_diagnostic_item(sym, did)); + is_std_options || match_any_def_paths(cx, did, tokio_file_options).is_some() } else { false } diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index ccaed3057f4d..103a331a0a79 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -14,7 +14,6 @@ pub const APPLICABILITY_VALUES: [[&str; 3]; 4] = [ pub const DIAG: [&str; 2] = ["rustc_errors", "Diag"]; pub const EARLY_CONTEXT: [&str; 2] = ["rustc_lint", "EarlyContext"]; pub const EARLY_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "EarlyLintPass"]; -pub const FILE_OPTIONS: [&str; 4] = ["std", "fs", "File", "options"]; #[expect(clippy::invalid_paths)] // internal lints do not know about all external crates pub const FUTURES_IO_ASYNCREADEXT: [&str; 3] = ["futures_util", "io", "AsyncReadExt"]; #[expect(clippy::invalid_paths)] // internal lints do not know about all external crates @@ -27,7 +26,6 @@ pub const LATE_CONTEXT: [&str; 2] = ["rustc_lint", "LateContext"]; pub const LATE_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "LateLintPass"]; pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"]; pub const MSRV: [&str; 3] = ["clippy_config", "msrvs", "Msrv"]; -pub const OPEN_OPTIONS_NEW: [&str; 4] = ["std", "fs", "OpenOptions", "new"]; pub const PARKING_LOT_MUTEX_GUARD: [&str; 3] = ["lock_api", "mutex", "MutexGuard"]; pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockReadGuard"]; pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockWriteGuard"]; From 13d573281111fda85692623350a51640596847ee Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Thu, 19 Sep 2024 13:09:25 +0100 Subject: [PATCH 20/20] Categorise paths in `clippy_utils::paths` --- src/tools/clippy/clippy_utils/src/paths.rs | 33 +++++++++++++--------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/tools/clippy/clippy_utils/src/paths.rs b/src/tools/clippy/clippy_utils/src/paths.rs index 103a331a0a79..c8b59fca5241 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -4,6 +4,7 @@ //! Whenever possible, please consider diagnostic items over hardcoded paths. //! See for more information. +// Paths inside rustc pub const APPLICABILITY: [&str; 2] = ["rustc_lint_defs", "Applicability"]; pub const APPLICABILITY_VALUES: [[&str; 3]; 4] = [ ["rustc_lint_defs", "Applicability", "Unspecified"], @@ -14,18 +15,32 @@ pub const APPLICABILITY_VALUES: [[&str; 3]; 4] = [ pub const DIAG: [&str; 2] = ["rustc_errors", "Diag"]; pub const EARLY_CONTEXT: [&str; 2] = ["rustc_lint", "EarlyContext"]; pub const EARLY_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "EarlyLintPass"]; -#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates -pub const FUTURES_IO_ASYNCREADEXT: [&str; 3] = ["futures_util", "io", "AsyncReadExt"]; -#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates -pub const FUTURES_IO_ASYNCWRITEEXT: [&str; 3] = ["futures_util", "io", "AsyncWriteExt"]; pub const IDENT: [&str; 3] = ["rustc_span", "symbol", "Ident"]; pub const IDENT_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Ident", "as_str"]; -pub const ITERTOOLS_NEXT_TUPLE: [&str; 3] = ["itertools", "Itertools", "next_tuple"]; pub const KW_MODULE: [&str; 3] = ["rustc_span", "symbol", "kw"]; pub const LATE_CONTEXT: [&str; 2] = ["rustc_lint", "LateContext"]; pub const LATE_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "LateLintPass"]; pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"]; +pub const SYMBOL: [&str; 3] = ["rustc_span", "symbol", "Symbol"]; +pub const SYMBOL_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Symbol", "as_str"]; +pub const SYMBOL_INTERN: [&str; 4] = ["rustc_span", "symbol", "Symbol", "intern"]; +pub const SYMBOL_TO_IDENT_STRING: [&str; 4] = ["rustc_span", "symbol", "Symbol", "to_ident_string"]; +pub const SYM_MODULE: [&str; 3] = ["rustc_span", "symbol", "sym"]; +pub const SYNTAX_CONTEXT: [&str; 3] = ["rustc_span", "hygiene", "SyntaxContext"]; + +// Paths in `core`/`alloc`/`std`. This should be avoided and cleaned up by adding diagnostic items. +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"]; + +// Paths in clippy itself pub const MSRV: [&str; 3] = ["clippy_config", "msrvs", "Msrv"]; + +// Paths in external crates +#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates +pub const FUTURES_IO_ASYNCREADEXT: [&str; 3] = ["futures_util", "io", "AsyncReadExt"]; +#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates +pub const FUTURES_IO_ASYNCWRITEEXT: [&str; 3] = ["futures_util", "io", "AsyncWriteExt"]; +pub const ITERTOOLS_NEXT_TUPLE: [&str; 3] = ["itertools", "Itertools", "next_tuple"]; pub const PARKING_LOT_MUTEX_GUARD: [&str; 3] = ["lock_api", "mutex", "MutexGuard"]; pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockReadGuard"]; pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockWriteGuard"]; @@ -37,14 +52,6 @@ pub const REGEX_NEW: [&str; 3] = ["regex", "Regex", "new"]; pub const REGEX_SET_NEW: [&str; 3] = ["regex", "RegexSet", "new"]; pub const SERDE_DESERIALIZE: [&str; 3] = ["serde", "de", "Deserialize"]; pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"]; -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 SYMBOL: [&str; 3] = ["rustc_span", "symbol", "Symbol"]; -pub const SYMBOL_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Symbol", "as_str"]; -pub const SYMBOL_INTERN: [&str; 4] = ["rustc_span", "symbol", "Symbol", "intern"]; -pub const SYMBOL_TO_IDENT_STRING: [&str; 4] = ["rustc_span", "symbol", "Symbol", "to_ident_string"]; -pub const SYM_MODULE: [&str; 3] = ["rustc_span", "symbol", "sym"]; -pub const SYNTAX_CONTEXT: [&str; 3] = ["rustc_span", "hygiene", "SyntaxContext"]; #[expect(clippy::invalid_paths)] // internal lints do not know about all external crates pub const TOKIO_FILE_OPTIONS: [&str; 5] = ["tokio", "fs", "file", "File", "options"]; #[expect(clippy::invalid_paths)] // internal lints do not know about all external crates