[Clippy] Swap option_as_ref_deref to use diagnostic items instead of paths

This commit is contained in:
GnomedDev 2024-09-18 21:09:06 +01:00
parent 3ebff28f80
commit 5e4716888a
No known key found for this signature in database
GPG Key ID: 9BF10F8372B254D1
8 changed files with 26 additions and 19 deletions

View File

@ -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,

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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]

View File

@ -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]

View File

@ -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
}

View File

@ -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"];