From 467dbcba60bb109636a31af3d298e253850422dc Mon Sep 17 00:00:00 2001 From: Kappa322 Date: Sat, 20 Jul 2024 13:05:55 +0200 Subject: [PATCH 01/24] Improve documentation for ::from_str_radix Two improvements to the documentation: - Document `-` as a valid character for signed integer destinations - Make the documentation even more clear that extra whitespace and non-digit characters is invalid. Many other languages, e.g. c++, are very permissive in string to integer routines and simply try to consume as much as they can, ignoring the rest. This is trying to make the transition for developers who are used to the conversion semantics in these languages a bit easier. --- library/core/src/num/mod.rs | 69 +++++++++++++++++++++------- library/core/src/num/nonzero.rs | 10 ---- library/core/tests/num/int_macros.rs | 2 + 3 files changed, 55 insertions(+), 26 deletions(-) diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs index e9e5324666a..2bcfe43a55b 100644 --- a/library/core/src/num/mod.rs +++ b/library/core/src/num/mod.rs @@ -23,6 +23,16 @@ macro_rules! unlikely { }; } +// Use this when the generated code should differ between signed and unsigned types. +macro_rules! sign_dependent_expr { + (signed ? if signed { $signed_case:expr } if unsigned { $unsigned_case:expr } ) => { + $signed_case + }; + (unsigned ? if signed { $signed_case:expr } if unsigned { $unsigned_case:expr } ) => { + $unsigned_case + }; +} + // All these modules are technically private and only exposed for coretests: #[cfg(not(no_fp_fmt_parse))] pub mod bignum; @@ -1410,15 +1420,25 @@ const fn from_str_radix_panic(radix: u32) { } macro_rules! from_str_radix { - ($($int_ty:ty)+) => {$( + ($signedness:ident $($int_ty:ty)+) => {$( impl $int_ty { /// Converts a string slice in a given base to an integer. /// - /// The string is expected to be an optional `+` sign - /// followed by digits. - /// Leading and trailing whitespace represent an error. - /// Digits are a subset of these characters, depending on `radix`: + /// The string is expected to be an optional + #[doc = sign_dependent_expr!{ + $signedness ? + if signed { + " `+` or `-` " + } + if unsigned { + " `+` " + } + }] + /// sign followed by only digits. Leading and trailing non-digit characters (including + /// whitespace) represent an error. Underscores (which are accepted in rust literals) + /// also represent an error. /// + /// Digits are a subset of these characters, depending on `radix`: /// * `0-9` /// * `a-z` /// * `A-Z` @@ -1430,10 +1450,13 @@ impl $int_ty { /// # Examples /// /// Basic usage: - /// /// ``` #[doc = concat!("assert_eq!(", stringify!($int_ty), "::from_str_radix(\"A\", 16), Ok(10));")] /// ``` + /// Trailing space returns error: + /// ``` + #[doc = concat!("assert!(", stringify!($int_ty), "::from_str_radix(\"1 \", 10).is_err());")] + /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_stable(feature = "const_int_from_str", since = "CURRENT_RUSTC_VERSION")] pub const fn from_str_radix(src: &str, radix: u32) -> Result<$int_ty, ParseIntError> { @@ -1535,20 +1558,31 @@ macro_rules! run_checked_loop { )+} } -from_str_radix! { i8 u8 i16 u16 i32 u32 i64 u64 i128 u128 } +from_str_radix! { unsigned u8 u16 u32 u64 u128 } +from_str_radix! { signed i8 i16 i32 i64 i128 } // Re-use the relevant implementation of from_str_radix for isize and usize to avoid outputting two // identical functions. macro_rules! from_str_radix_size_impl { - ($($t:ident $size:ty),*) => {$( + ($($signedness:ident $t:ident $size:ty),*) => {$( impl $size { /// Converts a string slice in a given base to an integer. /// - /// The string is expected to be an optional `+` sign - /// followed by digits. - /// Leading and trailing whitespace represent an error. - /// Digits are a subset of these characters, depending on `radix`: + /// The string is expected to be an optional + #[doc = sign_dependent_expr!{ + $signedness ? + if signed { + " `+` or `-` " + } + if unsigned { + " `+` " + } + }] + /// sign followed by only digits. Leading and trailing non-digit characters (including + /// whitespace) represent an error. Underscores (which are accepted in rust literals) + /// also represent an error. /// + /// Digits are a subset of these characters, depending on `radix`: /// * `0-9` /// * `a-z` /// * `A-Z` @@ -1560,10 +1594,13 @@ impl $size { /// # Examples /// /// Basic usage: - /// /// ``` #[doc = concat!("assert_eq!(", stringify!($size), "::from_str_radix(\"A\", 16), Ok(10));")] /// ``` + /// Trailing space returns error: + /// ``` + #[doc = concat!("assert!(", stringify!($size), "::from_str_radix(\"1 \", 10).is_err());")] + /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_stable(feature = "const_int_from_str", since = "CURRENT_RUSTC_VERSION")] pub const fn from_str_radix(src: &str, radix: u32) -> Result<$size, ParseIntError> { @@ -1576,8 +1613,8 @@ pub const fn from_str_radix(src: &str, radix: u32) -> Result<$size, ParseIntErro } #[cfg(target_pointer_width = "16")] -from_str_radix_size_impl! { i16 isize, u16 usize } +from_str_radix_size_impl! { signed i16 isize, unsigned u16 usize } #[cfg(target_pointer_width = "32")] -from_str_radix_size_impl! { i32 isize, u32 usize } +from_str_radix_size_impl! { signed i32 isize, unsigned u32 usize } #[cfg(target_pointer_width = "64")] -from_str_radix_size_impl! { i64 isize, u64 usize } +from_str_radix_size_impl! { signed i64 isize, unsigned u64 usize } diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 8b888f12da0..e5c9a7e086a 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -1972,16 +1972,6 @@ pub const fn wrapping_neg(self) -> Self { }; } -// Use this when the generated code should differ between signed and unsigned types. -macro_rules! sign_dependent_expr { - (signed ? if signed { $signed_case:expr } if unsigned { $unsigned_case:expr } ) => { - $signed_case - }; - (unsigned ? if signed { $signed_case:expr } if unsigned { $unsigned_case:expr } ) => { - $unsigned_case - }; -} - nonzero_integer! { Self = NonZeroU8, Primitive = unsigned u8, diff --git a/library/core/tests/num/int_macros.rs b/library/core/tests/num/int_macros.rs index 830a96204ca..6a26bd15a66 100644 --- a/library/core/tests/num/int_macros.rs +++ b/library/core/tests/num/int_macros.rs @@ -244,6 +244,8 @@ fn test_from_str_radix() { assert_eq!($T::from_str_radix("Z", 35).ok(), None::<$T>); assert_eq!($T::from_str_radix("-9", 2).ok(), None::<$T>); + assert_eq!($T::from_str_radix("10_0", 10).ok(), None::<$T>); + assert_eq!(u32::from_str_radix("-9", 10).ok(), None::); } #[test] From a786be5d0657a675ed6ddd5179ec66688e801610 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 17:58:18 +0100 Subject: [PATCH 02/24] [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 b588b4b6e97..6765ce73186 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -512,6 +512,8 @@ breakpoint, bridge, bswap, + btreemap_contains_key, + btreemap_insert, btreeset_iter, builtin_syntax, c, @@ -973,6 +975,8 @@ 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 60e08b47e3d..0eadc9ecac8 100644 --- a/library/alloc/src/collections/btree/map.rs +++ b/library/alloc/src/collections/btree/map.rs @@ -916,6 +916,7 @@ pub fn pop_last(&mut self) -> Option<(K, V)> /// 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 @@ pub fn get_mut(&mut self, key: &Q) -> Option<&mut V> /// ``` #[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 7d21d37f40d..0d7346c7f1e 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -1037,6 +1037,7 @@ pub unsafe fn get_many_unchecked_mut( /// ``` #[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 @@ pub fn get_mut(&mut self, k: &Q) -> Option<&mut V> #[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 b7c9d3d0385..9c5100a8c1a 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::{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 fd28ca12d50..59105383093 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -12,8 +12,6 @@ ["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 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 03/24] [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 6765ce73186..bcebe1d39f5 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1575,6 +1575,7 @@ 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 02f6f783b51..9edd58259ba 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -653,6 +653,7 @@ pub fn is_err_and(self, f: impl FnOnce(E) -> bool) -> bool { /// ``` #[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 3d1c666dfea..8206c75927b 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 59105383093..0d1d4382875 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -12,7 +12,6 @@ ["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 078b067c0d9dbe1dd42569554b7d9bb967d59f46 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Thu, 19 Sep 2024 20:26:43 +0900 Subject: [PATCH 04/24] Support 128-bit atomics on s390x --- .../rustc_target/src/spec/targets/s390x_unknown_linux_gnu.rs | 2 +- .../rustc_target/src/spec/targets/s390x_unknown_linux_musl.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_target/src/spec/targets/s390x_unknown_linux_gnu.rs b/compiler/rustc_target/src/spec/targets/s390x_unknown_linux_gnu.rs index e0a6c230fd6..71086daaf2c 100644 --- a/compiler/rustc_target/src/spec/targets/s390x_unknown_linux_gnu.rs +++ b/compiler/rustc_target/src/spec/targets/s390x_unknown_linux_gnu.rs @@ -10,7 +10,7 @@ pub(crate) fn target() -> Target { // ABI. Pass the -vector feature string to LLVM to respect this assumption. On LLVM < 16, we // also strip v128 from the data_layout below to match the older LLVM's expectation. base.features = "-vector".into(); - base.max_atomic_width = Some(64); + base.max_atomic_width = Some(128); base.min_global_align = Some(16); base.stack_probes = StackProbeType::Inline; base.supported_sanitizers = diff --git a/compiler/rustc_target/src/spec/targets/s390x_unknown_linux_musl.rs b/compiler/rustc_target/src/spec/targets/s390x_unknown_linux_musl.rs index 6aabe9ca519..016ff5abe4c 100644 --- a/compiler/rustc_target/src/spec/targets/s390x_unknown_linux_musl.rs +++ b/compiler/rustc_target/src/spec/targets/s390x_unknown_linux_musl.rs @@ -10,7 +10,7 @@ pub(crate) fn target() -> Target { // ABI. Pass the -vector feature string to LLVM to respect this assumption. On LLVM < 16, we // also strip v128 from the data_layout below to match the older LLVM's expectation. base.features = "-vector".into(); - base.max_atomic_width = Some(64); + base.max_atomic_width = Some(128); base.min_global_align = Some(16); base.static_position_independent_executables = true; base.stack_probes = StackProbeType::Inline; From 5e4716888a731cdd929bdbaf94eeb3e192f1eace Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Wed, 18 Sep 2024 21:09:06 +0100 Subject: [PATCH 05/24] [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 bcebe1d39f5..73f260d3518 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -682,6 +682,7 @@ crt_dash_static: "crt-static", csky_target_feature, cstr_type, + cstring_as_c_str, cstring_type, ctlz, ctlz_nonzero, @@ -1367,6 +1368,7 @@ or, or_patterns, ord_cmp_method, + os_string_as_os_str, other, out, overflow_checks, @@ -1420,6 +1422,7 @@ pat_param, patchable_function_entry, path, + pathbuf_as_path, pattern_complexity, pattern_parentheses, pattern_type, @@ -1871,6 +1874,8 @@ 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 @@ 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 e32676a6543..45037aa1615 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -576,6 +576,7 @@ pub fn as_bytes_with_nul(&self) -> &[u8] { #[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 6daab5bc73a..a2c70de3ed0 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -1073,6 +1073,7 @@ pub fn into_bytes(self) -> Vec { #[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 @@ pub fn as_str(&self) -> &str { #[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 2afb5dd0d1a..ac0db38eac3 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1545,6 +1545,7 @@ pub fn truncate(&mut self, len: usize) { /// ``` #[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 @@ pub fn as_slice(&self) -> &[T] { /// ``` #[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 99bea676e12..8e1147338b0 100644 --- a/library/std/src/ffi/os_str.rs +++ b/library/std/src/ffi/os_str.rs @@ -196,6 +196,7 @@ pub unsafe fn from_encoded_bytes_unchecked(bytes: Vec) -> Self { /// 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 c94df9b5366..6d6aa92711b 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -1226,6 +1226,7 @@ pub fn with_capacity(capacity: usize) -> 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 cb57689b0c4..9a18d8a1421 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_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 0d1d4382875..e0fa814d30b 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -12,7 +12,6 @@ ["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 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 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_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 06/24] [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 73f260d3518..17ff0bb86f8 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -838,6 +838,7 @@ f16_nan, f16c_target_feature, f32, + f32_epsilon, f32_legacy_const_digits, f32_legacy_const_epsilon, f32_legacy_const_infinity, @@ -854,6 +855,7 @@ 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 4c2a4ee3b32..d0eb6bb3f28 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 87fb5fe7ebe..4bc275ad147 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 cace85a2451..be97ad389bf 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 e0fa814d30b..ea7c19dec6d 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 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 07/24] [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 17ff0bb86f8..c9b36d77e1a 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1370,6 +1370,7 @@ or, or_patterns, ord_cmp_method, + os_str_to_os_string, os_string_as_os_str, other, out, @@ -1424,6 +1425,7 @@ 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 8e1147338b0..0f905803bb8 100644 --- a/library/std/src/ffi/os_str.rs +++ b/library/std/src/ffi/os_str.rs @@ -919,6 +919,7 @@ pub fn to_string_lossy(&self) -> Cow<'_, str> { #[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 6d6aa92711b..2ba3a9cdca2 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -2265,6 +2265,7 @@ pub fn to_string_lossy(&self) -> Cow<'_, str> { #[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 bfdc1cbeed7..4e24ddad83a 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::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 @@ fn check_fn( && 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 ea7c19dec6d..0a36d9a6664 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 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 08/24] [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 c9b36d77e1a..cec833f2e56 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1425,6 +1425,7 @@ 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 2ba3a9cdca2..aa9f63d915a 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 db491a8c8f6..5198d7838a2 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 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { && 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 0a36d9a6664..5e8157ac0c3 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 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 09/24] [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 cec833f2e56..2194c4ac3b7 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1882,6 +1882,8 @@ 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 a2c70de3ed0..c7e9931ed2a 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -1113,6 +1113,7 @@ pub fn as_mut_str(&mut self) -> &mut str { #[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 @@ unsafe fn insert_bytes(&mut self, idx: usize, bytes: &[u8]) { #[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 d05c5a01f41..c6f03c3a7cf 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 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { 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 81450fd8c6c..ccdf5529d53 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 5e8157ac0c3..64540637cdb 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_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_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 10/24] [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 2194c4ac3b7..d449fac14bc 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1836,6 +1836,7 @@ slice, slice_from_raw_parts, slice_from_raw_parts_mut, + slice_into_vec, slice_iter, slice_len_fn, slice_patterns, @@ -2090,7 +2091,9 @@ 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 8cdba166c9d..45fb88969c6 100644 --- a/library/alloc/src/slice.rs +++ b/library/alloc/src/slice.rs @@ -496,6 +496,7 @@ pub fn to_vec_in(&self, alloc: A) -> Vec #[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 ac0db38eac3..29ddf939499 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 @@ pub fn dedup(&mut self) { #[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 b0e25c02265..6c0b0e11c9e 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 @@ fn as_vec_initializer<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'tcx>) -> Opt && 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 8970b4d1229..e09cc9edf3a 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 @@ pub fn hir(cx: &LateContext<'_>, expr: &'a Expr<'_>) -> Option> { && 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 @@ pub fn hir(cx: &LateContext<'_>, expr: &'a Expr<'_>) -> Option> { } 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 64540637cdb..1d5a2294978 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_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_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 11/24] [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 d449fac14bc..c734cfd632a 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -2094,6 +2094,7 @@ 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 29ddf939499..a7e736299be 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -477,6 +477,7 @@ pub const fn new() -> Self { #[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 678681ea425..08de10f69b0 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::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 6c0b0e11c9e..04c16281ec4 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::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 @@ fn as_vec_initializer<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'tcx>) -> Opt } 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 1d5a2294978..0a0749aa005 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_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 12/24] [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 c734cfd632a..20991ce5d8c 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1366,6 +1366,8 @@ optin_builtin_traits, option, option_env, + option_expect, + option_unwrap, options, or, or_patterns, @@ -2092,8 +2094,10 @@ 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 a7e736299be..13b06584223 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -2384,6 +2384,7 @@ pub fn push_within_capacity(&mut self, value: T) -> Result<(), T> { /// 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 @@ pub fn len(&self) -> usize { /// 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 265f056dc87..5ba13969605 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -923,6 +923,7 @@ pub const fn as_mut_slice(&mut self) -> &mut [T] { #[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 @@ pub const fn expect(self, msg: &str) -> T { #[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 57434f35544..55d1b9ee676 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 0a0749aa005..4a93939d0cc 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -73,9 +73,5 @@ #[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 13/24] [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 20991ce5d8c..e795b7fb25c 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -501,6 +501,7 @@ 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 03cdff9b13b..58a870d2e07 100644 --- a/library/core/src/bool.rs +++ b/library/core/src/bool.rs @@ -55,6 +55,7 @@ pub fn then_some(self, t: T) -> Option { /// 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 4fbf661727d..77a62fbb4fb 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 4a93939d0cc..cb0330d0046 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_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 14/24] [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 e795b7fb25c..8a3ec7a8f71 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -342,6 +342,7 @@ Upvars, Vec, VecDeque, + Waker, Wrapper, Wrapping, Yield, diff --git a/library/core/src/task/wake.rs b/library/core/src/task/wake.rs index 5e559ad8d2c..a5103499c8a 100644 --- a/library/core/src/task/wake.rs +++ b/library/core/src/task/wake.rs @@ -414,6 +414,7 @@ pub const fn build(self) -> Context<'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 da66632d55f..9b64cc7589c 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 cb0330d0046..89c1bd8a8a0 100644 --- a/src/tools/clippy/clippy_utils/src/paths.rs +++ b/src/tools/clippy/clippy_utils/src/paths.rs @@ -73,4 +73,3 @@ #[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 15/24] [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 8a3ec7a8f71..bac5bb96569 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1061,6 +1061,7 @@ 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 ae46670c25e..f28a0568a3c 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 f41fdf3203c..6d6820311b6 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 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) { 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 89c1bd8a8a0..26f680e2666 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_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 16/24] [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 bac5bb96569..7c3544639b9 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1888,6 +1888,7 @@ 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 c7e9931ed2a..0bad33c909d 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -571,6 +571,7 @@ pub fn from_str(_: &str) -> 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 69c5bc57e29..bf7555a29e2 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::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 26f680e2666..646c7bc5df8 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_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 17/24] [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 7c3544639b9..69c6562ba93 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1876,11 +1876,14 @@ 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 9373d807f44..712bf011c27 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 @@ pub fn contains(&self, pat: P) -> bool { /// 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 @@ pub fn starts_with(&self, pat: P) -> bool { /// 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 85cabd2800a..02d433ecc5a 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::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_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 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { && 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 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { 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 646c7bc5df8..986adddc6ae 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 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 18/24] [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 69c6562ba93..5bc2c049114 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1893,6 +1893,7 @@ 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 0bad33c909d..d58a016b502 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 6b5e6c6ab20..f01cb457af8 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 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { && 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 986adddc6ae..dd304e7aae8 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_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 19/24] [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 5bc2c049114..e0bfab53e76 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1437,6 +1437,7 @@ 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 caf6980afd9..a964db2e0ac 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 b915df52762..cfc15d92715 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 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { 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 dd304e7aae8..c7b1c01de1d 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 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 20/24] [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 e0bfab53e76..bc973bac63a 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -981,8 +981,16 @@ 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 0d7346c7f1e..1a18721b15e 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -1393,6 +1393,7 @@ fn index(&self, key: &Q) -> &V { /// 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 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// 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 @@ pub(super) fn iter(&self) -> Iter<'_, K, V> { /// 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 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// 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 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// 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 @@ pub struct ExtractIf<'a, K, V, F> /// 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 e63d90645f7..4a113ddea3a 100644 --- a/library/std/src/collections/hash/set.rs +++ b/library/std/src/collections/hash/set.rs @@ -1271,6 +1271,7 @@ fn sub(self, rhs: &HashSet) -> HashSet { /// 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 fb29d982417..f162948bb44 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 @@ 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 c7b1c01de1d..ccaed3057f4 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 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 21/24] [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 bc973bac63a..402232a1720 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -895,6 +895,7 @@ field, field_init_shorthand, file, + file_options, float, float_to_int_unchecked, floorf128, @@ -1370,6 +1371,7 @@ 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 abf7d5d294a..92d3838d9f2 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -466,6 +466,7 @@ pub fn create_new>(path: P) -> io::Result { /// ``` #[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 cbeb48b6cc3..f1a3c81cebb 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 ccaed3057f4..103a331a0a7 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 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_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 22/24] 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 103a331a0a7..c8b59fca524 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 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_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 From dc628c8ecb8d24f88850894e01e7bd027cc9af61 Mon Sep 17 00:00:00 2001 From: Lieselotte <52315535+she3py@users.noreply.github.com> Date: Thu, 19 Sep 2024 14:22:50 +0200 Subject: [PATCH 23/24] `pal::unsupported::process::ExitCode`: use an `u8` instead of a `bool` --- library/std/src/sys/pal/unsupported/process.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/library/std/src/sys/pal/unsupported/process.rs b/library/std/src/sys/pal/unsupported/process.rs index 40231bfc90b..fee81744f09 100644 --- a/library/std/src/sys/pal/unsupported/process.rs +++ b/library/std/src/sys/pal/unsupported/process.rs @@ -255,11 +255,11 @@ pub fn code(self) -> Option> { } #[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub struct ExitCode(bool); +pub struct ExitCode(u8); impl ExitCode { - pub const SUCCESS: ExitCode = ExitCode(false); - pub const FAILURE: ExitCode = ExitCode(true); + pub const SUCCESS: ExitCode = ExitCode(0); + pub const FAILURE: ExitCode = ExitCode(1); pub fn as_i32(&self) -> i32 { self.0 as i32 @@ -268,10 +268,7 @@ pub fn as_i32(&self) -> i32 { impl From for ExitCode { fn from(code: u8) -> Self { - match code { - 0 => Self::SUCCESS, - 1..=255 => Self::FAILURE, - } + Self(code) } } From b67485e196ac87833d6a30285133f5e6bb6218f3 Mon Sep 17 00:00:00 2001 From: Urgau Date: Thu, 19 Sep 2024 15:56:27 +0200 Subject: [PATCH 24/24] Make `link_cfg` internal because it's in perme-unstable --- compiler/rustc_feature/src/unstable.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs index d0c0460ddfe..fa3a7049f4a 100644 --- a/compiler/rustc_feature/src/unstable.rs +++ b/compiler/rustc_feature/src/unstable.rs @@ -204,7 +204,7 @@ pub fn internal(&self, feature: Symbol) -> bool { /// Changes `impl Trait` to capture all lifetimes in scope. (unstable, lifetime_capture_rules_2024, "1.76.0", None), /// Allows `#[link(..., cfg(..))]`; perma-unstable per #37406 - (unstable, link_cfg, "1.14.0", None), + (internal, link_cfg, "1.14.0", None), /// Allows using `?Trait` trait bounds in more contexts. (internal, more_maybe_bounds, "1.82.0", None), /// Allows the `multiple_supertrait_upcastable` lint.