Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Clippy] Get rid of most std match_def_path usage, swap to diagnostic items. #130553

Merged
merged 20 commits into from
Sep 19, 2024
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a786be5
[Clippy] Swap `map_entry` to use diagnostic items instead of paths
GnomedDev Sep 18, 2024
3ebff28
[Clippy] Swap `lines_filter_map_ok` to use a diagnostic item instead …
GnomedDev Sep 18, 2024
5e47168
[Clippy] Swap `option_as_ref_deref` to use diagnostic items instead o…
GnomedDev Sep 18, 2024
c891082
[Clippy] Swap `float_equality_without_abs` to use diagnostic items in…
GnomedDev Sep 18, 2024
afe7907
[Clippy] Swap `redundant_clone` to use diagnostic items instead of paths
GnomedDev Sep 18, 2024
037b978
[Clippy] Swap `manual_main_separator_str` to use diagnostic item inst…
GnomedDev Sep 18, 2024
28f4c82
[Clippy] Swap `single_char_add_str`/`format_push_string` to use diagn…
GnomedDev Sep 18, 2024
846ae57
[Clippy] Swap `VecArgs::hir` to use diagnostic items instead of paths
GnomedDev Sep 18, 2024
15240a9
[Clippy] Swap `repeat_vec_with_capacity` to use diagnostic item inste…
GnomedDev Sep 18, 2024
25da0e2
[Clippy] Swap `manual_while_let_some` to use diagnostic items instead…
GnomedDev Sep 18, 2024
5b55270
[Clippy] Swap `filter_map_bool_then` to use diagnostic item instead o…
GnomedDev Sep 18, 2024
372f68b
[Clippy] Swap `waker_clone_wake` to use diagnostic item instead of path
GnomedDev Sep 18, 2024
1890620
[Clippy] Swap `instant_subtraction` to use diagnostic item instead of…
GnomedDev Sep 18, 2024
89532c0
[Clippy] Swap `unnecessary_to_owned` to use diagnostic item instead o…
GnomedDev Sep 18, 2024
5f42ae1
[Clippy] Swap `manual_strip` to use diagnostic items instead of paths
GnomedDev Sep 18, 2024
5f85f73
[Clippy] Swap `unnecessary_owned_empty_strings` to use diagnostic ite…
GnomedDev Sep 18, 2024
43b8e04
[Clippy] Swap `non_octal_unix_permissions` to use diagnostic item ins…
GnomedDev Sep 18, 2024
364e552
[Clippy] Swap `iter_over_hash_type` to use diagnostic items instead o…
GnomedDev Sep 18, 2024
b2eebee
[Clippy] Swap `open_options` to use diagnostic items instead of paths
GnomedDev Sep 19, 2024
13d5732
Categorise paths in `clippy_utils::paths`
GnomedDev Sep 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Clippy] Swap option_as_ref_deref to use diagnostic items instead o…
…f paths
  • Loading branch information
GnomedDev committed Sep 19, 2024
commit 5e4716888a731cdd929bdbaf94eeb3e192f1eace
7 changes: 7 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
@@ -682,6 +682,7 @@ symbols! {
crt_dash_static: "crt-static",
csky_target_feature,
cstr_type,
cstring_as_c_str,
cstring_type,
ctlz,
ctlz_nonzero,
@@ -1367,6 +1368,7 @@ symbols! {
or,
or_patterns,
ord_cmp_method,
os_string_as_os_str,
other,
out,
overflow_checks,
@@ -1420,6 +1422,7 @@ symbols! {
pat_param,
patchable_function_entry,
path,
pathbuf_as_path,
pattern_complexity,
pattern_parentheses,
pattern_type,
@@ -1871,6 +1874,8 @@ symbols! {
str_trim_end,
str_trim_start,
strict_provenance,
string_as_mut_str,
string_as_str,
string_deref_patterns,
stringify,
struct_field_attributes,
@@ -2076,6 +2081,8 @@ symbols! {
var,
variant_count,
vec,
vec_as_mut_slice,
vec_as_slice,
vec_macro,
vecdeque_iter,
version,
1 change: 1 addition & 0 deletions library/alloc/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
@@ -576,6 +576,7 @@ impl CString {
#[inline]
#[must_use]
#[stable(feature = "as_c_str", since = "1.20.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "cstring_as_c_str")]
pub fn as_c_str(&self) -> &CStr {
&*self
}
2 changes: 2 additions & 0 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
@@ -1073,6 +1073,7 @@ impl String {
#[inline]
#[must_use]
#[stable(feature = "string_as_str", since = "1.7.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "string_as_str")]
pub fn as_str(&self) -> &str {
self
}
@@ -1092,6 +1093,7 @@ impl String {
#[inline]
#[must_use]
#[stable(feature = "string_as_str", since = "1.7.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "string_as_mut_str")]
pub fn as_mut_str(&mut self) -> &mut str {
self
}
2 changes: 2 additions & 0 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
@@ -1545,6 +1545,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// ```
#[inline]
#[stable(feature = "vec_as_slice", since = "1.7.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "vec_as_slice")]
pub fn as_slice(&self) -> &[T] {
self
}
@@ -1562,6 +1563,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// ```
#[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
}
1 change: 1 addition & 0 deletions library/std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
@@ -196,6 +196,7 @@ impl OsString {
/// let os_str = OsStr::new("foo");
/// assert_eq!(os_string.as_os_str(), os_str);
/// ```
#[cfg_attr(not(test), rustc_diagnostic_item = "os_string_as_os_str")]
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use]
#[inline]
1 change: 1 addition & 0 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
@@ -1226,6 +1226,7 @@ impl PathBuf {
/// let p = PathBuf::from("/test");
/// assert_eq!(Path::new("/test"), p.as_path());
/// ```
#[cfg_attr(not(test), rustc_diagnostic_item = "pathbuf_as_path")]
#[stable(feature = "rust1", since = "1.0.0")]
#[must_use]
#[inline]
24 changes: 12 additions & 12 deletions src/tools/clippy/clippy_lints/src/methods/option_as_ref_deref.rs
Original file line number Diff line number Diff line change
@@ -2,12 +2,12 @@ use clippy_config::msrvs::{self, Msrv};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{match_def_path, path_to_local_id, paths, peel_blocks};
use clippy_utils::{path_to_local_id, peel_blocks};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_span::sym;
use rustc_span::{sym, Symbol};

use super::OPTION_AS_REF_DEREF;

@@ -31,14 +31,14 @@ pub(super) fn check(
return;
}

let deref_aliases: [&[&str]; 7] = [
&paths::CSTRING_AS_C_STR,
&paths::OS_STRING_AS_OS_STR,
&paths::PATH_BUF_AS_PATH,
&paths::STRING_AS_STR,
&paths::STRING_AS_MUT_STR,
&paths::VEC_AS_SLICE,
&paths::VEC_AS_MUT_SLICE,
let deref_aliases: [Symbol; 7] = [
sym::cstring_as_c_str,
sym::os_string_as_os_str,
sym::pathbuf_as_path,
sym::string_as_str,
sym::string_as_mut_str,
sym::vec_as_slice,
sym::vec_as_mut_slice,
];

let is_deref = match map_arg.kind {
@@ -48,7 +48,7 @@ pub(super) fn check(
.map_or(false, |fun_def_id| {
cx.tcx.is_diagnostic_item(sym::deref_method, fun_def_id)
|| cx.tcx.is_diagnostic_item(sym::deref_mut_method, fun_def_id)
|| deref_aliases.iter().any(|path| match_def_path(cx, fun_def_id, path))
|| deref_aliases.iter().any(|&sym| cx.tcx.is_diagnostic_item(sym, fun_def_id))
})
},
hir::ExprKind::Closure(&hir::Closure { body, .. }) => {
@@ -69,7 +69,7 @@ pub(super) fn check(
let method_did = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id).unwrap();
cx.tcx.is_diagnostic_item(sym::deref_method, method_did)
|| cx.tcx.is_diagnostic_item(sym::deref_mut_method, method_did)
|| deref_aliases.iter().any(|path| match_def_path(cx, method_did, path))
|| deref_aliases.iter().any(|&sym| cx.tcx.is_diagnostic_item(sym, method_did))
} else {
false
}
7 changes: 0 additions & 7 deletions src/tools/clippy/clippy_utils/src/paths.rs
Original file line number Diff line number Diff line change
@@ -12,7 +12,6 @@ pub const APPLICABILITY_VALUES: [[&str; 3]; 4] = [
["rustc_lint_defs", "Applicability", "MachineApplicable"],
];
pub const DIAG: [&str; 2] = ["rustc_errors", "Diag"];
pub const CSTRING_AS_C_STR: [&str; 5] = ["alloc", "ffi", "c_str", "CString", "as_c_str"];
pub const EARLY_CONTEXT: [&str; 2] = ["rustc_lint", "EarlyContext"];
pub const EARLY_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "EarlyLintPass"];
pub const F32_EPSILON: [&str; 4] = ["core", "f32", "<impl f32>", "EPSILON"];
@@ -40,12 +39,10 @@ pub const LATE_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "LateLintPass"];
pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"];
pub const MSRV: [&str; 3] = ["clippy_config", "msrvs", "Msrv"];
pub const OPEN_OPTIONS_NEW: [&str; 4] = ["std", "fs", "OpenOptions", "new"];
pub const OS_STRING_AS_OS_STR: [&str; 5] = ["std", "ffi", "os_str", "OsString", "as_os_str"];
pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "to_os_string"];
pub const PARKING_LOT_MUTEX_GUARD: [&str; 3] = ["lock_api", "mutex", "MutexGuard"];
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockReadGuard"];
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockWriteGuard"];
pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"];
pub const PATH_MAIN_SEPARATOR: [&str; 3] = ["std", "path", "MAIN_SEPARATOR"];
pub const PATH_TO_PATH_BUF: [&str; 4] = ["std", "path", "Path", "to_path_buf"];
#[cfg_attr(not(unix), allow(clippy::invalid_paths))]
@@ -62,8 +59,6 @@ pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"];
pub const SLICE_INTO_VEC: [&str; 4] = ["alloc", "slice", "<impl [T]>", "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", "<impl str>", "ends_with"];
pub const STR_LEN: [&str; 4] = ["core", "str", "<impl str>", "len"];
@@ -85,8 +80,6 @@ pub const TOKIO_IO_ASYNCWRITEEXT: [&str; 5] = ["tokio", "io", "util", "async_wri
pub const TOKIO_IO_OPEN_OPTIONS: [&str; 4] = ["tokio", "fs", "open_options", "OpenOptions"];
#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates
pub const TOKIO_IO_OPEN_OPTIONS_NEW: [&str; 5] = ["tokio", "fs", "open_options", "OpenOptions", "new"];
pub const VEC_AS_MUT_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_mut_slice"];
pub const VEC_AS_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_slice"];
pub const VEC_FROM_ELEM: [&str; 3] = ["alloc", "vec", "from_elem"];
pub const VEC_NEW: [&str; 4] = ["alloc", "vec", "Vec", "new"];
pub const VEC_WITH_CAPACITY: [&str; 4] = ["alloc", "vec", "Vec", "with_capacity"];