Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5e47168

Browse files
committed
[Clippy] Swap option_as_ref_deref to use diagnostic items instead of paths
1 parent 3ebff28 commit 5e47168

File tree

8 files changed

+26
-19
lines changed

8 files changed

+26
-19
lines changed

compiler/rustc_span/src/symbol.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ symbols! {
682682
crt_dash_static: "crt-static",
683683
csky_target_feature,
684684
cstr_type,
685+
cstring_as_c_str,
685686
cstring_type,
686687
ctlz,
687688
ctlz_nonzero,
@@ -1367,6 +1368,7 @@ symbols! {
13671368
or,
13681369
or_patterns,
13691370
ord_cmp_method,
1371+
os_string_as_os_str,
13701372
other,
13711373
out,
13721374
overflow_checks,
@@ -1420,6 +1422,7 @@ symbols! {
14201422
pat_param,
14211423
patchable_function_entry,
14221424
path,
1425+
pathbuf_as_path,
14231426
pattern_complexity,
14241427
pattern_parentheses,
14251428
pattern_type,
@@ -1871,6 +1874,8 @@ symbols! {
18711874
str_trim_end,
18721875
str_trim_start,
18731876
strict_provenance,
1877+
string_as_mut_str,
1878+
string_as_str,
18741879
string_deref_patterns,
18751880
stringify,
18761881
struct_field_attributes,
@@ -2076,6 +2081,8 @@ symbols! {
20762081
var,
20772082
variant_count,
20782083
vec,
2084+
vec_as_mut_slice,
2085+
vec_as_slice,
20792086
vec_macro,
20802087
vecdeque_iter,
20812088
version,

library/alloc/src/ffi/c_str.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ impl CString {
576576
#[inline]
577577
#[must_use]
578578
#[stable(feature = "as_c_str", since = "1.20.0")]
579+
#[cfg_attr(not(test), rustc_diagnostic_item = "cstring_as_c_str")]
579580
pub fn as_c_str(&self) -> &CStr {
580581
&*self
581582
}

library/alloc/src/string.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,7 @@ impl String {
10731073
#[inline]
10741074
#[must_use]
10751075
#[stable(feature = "string_as_str", since = "1.7.0")]
1076+
#[cfg_attr(not(test), rustc_diagnostic_item = "string_as_str")]
10761077
pub fn as_str(&self) -> &str {
10771078
self
10781079
}
@@ -1092,6 +1093,7 @@ impl String {
10921093
#[inline]
10931094
#[must_use]
10941095
#[stable(feature = "string_as_str", since = "1.7.0")]
1096+
#[cfg_attr(not(test), rustc_diagnostic_item = "string_as_mut_str")]
10951097
pub fn as_mut_str(&mut self) -> &mut str {
10961098
self
10971099
}

library/alloc/src/vec/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,7 @@ impl<T, A: Allocator> Vec<T, A> {
15451545
/// ```
15461546
#[inline]
15471547
#[stable(feature = "vec_as_slice", since = "1.7.0")]
1548+
#[cfg_attr(not(test), rustc_diagnostic_item = "vec_as_slice")]
15481549
pub fn as_slice(&self) -> &[T] {
15491550
self
15501551
}
@@ -1562,6 +1563,7 @@ impl<T, A: Allocator> Vec<T, A> {
15621563
/// ```
15631564
#[inline]
15641565
#[stable(feature = "vec_as_slice", since = "1.7.0")]
1566+
#[cfg_attr(not(test), rustc_diagnostic_item = "vec_as_mut_slice")]
15651567
pub fn as_mut_slice(&mut self) -> &mut [T] {
15661568
self
15671569
}

library/std/src/ffi/os_str.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ impl OsString {
196196
/// let os_str = OsStr::new("foo");
197197
/// assert_eq!(os_string.as_os_str(), os_str);
198198
/// ```
199+
#[cfg_attr(not(test), rustc_diagnostic_item = "os_string_as_os_str")]
199200
#[stable(feature = "rust1", since = "1.0.0")]
200201
#[must_use]
201202
#[inline]

library/std/src/path.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,7 @@ impl PathBuf {
12261226
/// let p = PathBuf::from("/test");
12271227
/// assert_eq!(Path::new("/test"), p.as_path());
12281228
/// ```
1229+
#[cfg_attr(not(test), rustc_diagnostic_item = "pathbuf_as_path")]
12291230
#[stable(feature = "rust1", since = "1.0.0")]
12301231
#[must_use]
12311232
#[inline]

src/tools/clippy/clippy_lints/src/methods/option_as_ref_deref.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use clippy_config::msrvs::{self, Msrv};
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::source::snippet;
44
use clippy_utils::ty::is_type_diagnostic_item;
5-
use clippy_utils::{match_def_path, path_to_local_id, paths, peel_blocks};
5+
use clippy_utils::{path_to_local_id, peel_blocks};
66
use rustc_errors::Applicability;
77
use rustc_hir as hir;
88
use rustc_lint::LateContext;
99
use rustc_middle::ty;
10-
use rustc_span::sym;
10+
use rustc_span::{sym, Symbol};
1111

1212
use super::OPTION_AS_REF_DEREF;
1313

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

34-
let deref_aliases: [&[&str]; 7] = [
35-
&paths::CSTRING_AS_C_STR,
36-
&paths::OS_STRING_AS_OS_STR,
37-
&paths::PATH_BUF_AS_PATH,
38-
&paths::STRING_AS_STR,
39-
&paths::STRING_AS_MUT_STR,
40-
&paths::VEC_AS_SLICE,
41-
&paths::VEC_AS_MUT_SLICE,
34+
let deref_aliases: [Symbol; 7] = [
35+
sym::cstring_as_c_str,
36+
sym::os_string_as_os_str,
37+
sym::pathbuf_as_path,
38+
sym::string_as_str,
39+
sym::string_as_mut_str,
40+
sym::vec_as_slice,
41+
sym::vec_as_mut_slice,
4242
];
4343

4444
let is_deref = match map_arg.kind {
@@ -48,7 +48,7 @@ pub(super) fn check(
4848
.map_or(false, |fun_def_id| {
4949
cx.tcx.is_diagnostic_item(sym::deref_method, fun_def_id)
5050
|| cx.tcx.is_diagnostic_item(sym::deref_mut_method, fun_def_id)
51-
|| deref_aliases.iter().any(|path| match_def_path(cx, fun_def_id, path))
51+
|| deref_aliases.iter().any(|&sym| cx.tcx.is_diagnostic_item(sym, fun_def_id))
5252
})
5353
},
5454
hir::ExprKind::Closure(&hir::Closure { body, .. }) => {
@@ -69,7 +69,7 @@ pub(super) fn check(
6969
let method_did = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id).unwrap();
7070
cx.tcx.is_diagnostic_item(sym::deref_method, method_did)
7171
|| cx.tcx.is_diagnostic_item(sym::deref_mut_method, method_did)
72-
|| deref_aliases.iter().any(|path| match_def_path(cx, method_did, path))
72+
|| deref_aliases.iter().any(|&sym| cx.tcx.is_diagnostic_item(sym, method_did))
7373
} else {
7474
false
7575
}

src/tools/clippy/clippy_utils/src/paths.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pub const APPLICABILITY_VALUES: [[&str; 3]; 4] = [
1212
["rustc_lint_defs", "Applicability", "MachineApplicable"],
1313
];
1414
pub const DIAG: [&str; 2] = ["rustc_errors", "Diag"];
15-
pub const CSTRING_AS_C_STR: [&str; 5] = ["alloc", "ffi", "c_str", "CString", "as_c_str"];
1615
pub const EARLY_CONTEXT: [&str; 2] = ["rustc_lint", "EarlyContext"];
1716
pub const EARLY_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "EarlyLintPass"];
1817
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"];
4039
pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"];
4140
pub const MSRV: [&str; 3] = ["clippy_config", "msrvs", "Msrv"];
4241
pub const OPEN_OPTIONS_NEW: [&str; 4] = ["std", "fs", "OpenOptions", "new"];
43-
pub const OS_STRING_AS_OS_STR: [&str; 5] = ["std", "ffi", "os_str", "OsString", "as_os_str"];
4442
pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "to_os_string"];
4543
pub const PARKING_LOT_MUTEX_GUARD: [&str; 3] = ["lock_api", "mutex", "MutexGuard"];
4644
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockReadGuard"];
4745
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockWriteGuard"];
48-
pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"];
4946
pub const PATH_MAIN_SEPARATOR: [&str; 3] = ["std", "path", "MAIN_SEPARATOR"];
5047
pub const PATH_TO_PATH_BUF: [&str; 4] = ["std", "path", "Path", "to_path_buf"];
5148
#[cfg_attr(not(unix), allow(clippy::invalid_paths))]
@@ -62,8 +59,6 @@ pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"];
6259
pub const SLICE_INTO_VEC: [&str; 4] = ["alloc", "slice", "<impl [T]>", "into_vec"];
6360
pub const STD_IO_SEEK_FROM_CURRENT: [&str; 4] = ["std", "io", "SeekFrom", "Current"];
6461
pub const STD_IO_SEEKFROM_START: [&str; 4] = ["std", "io", "SeekFrom", "Start"];
65-
pub const STRING_AS_MUT_STR: [&str; 4] = ["alloc", "string", "String", "as_mut_str"];
66-
pub const STRING_AS_STR: [&str; 4] = ["alloc", "string", "String", "as_str"];
6762
pub const STRING_NEW: [&str; 4] = ["alloc", "string", "String", "new"];
6863
pub const STR_ENDS_WITH: [&str; 4] = ["core", "str", "<impl str>", "ends_with"];
6964
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
8580
pub const TOKIO_IO_OPEN_OPTIONS: [&str; 4] = ["tokio", "fs", "open_options", "OpenOptions"];
8681
#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates
8782
pub const TOKIO_IO_OPEN_OPTIONS_NEW: [&str; 5] = ["tokio", "fs", "open_options", "OpenOptions", "new"];
88-
pub const VEC_AS_MUT_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_mut_slice"];
89-
pub const VEC_AS_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_slice"];
9083
pub const VEC_FROM_ELEM: [&str; 3] = ["alloc", "vec", "from_elem"];
9184
pub const VEC_NEW: [&str; 4] = ["alloc", "vec", "Vec", "new"];
9285
pub const VEC_WITH_CAPACITY: [&str; 4] = ["alloc", "vec", "Vec", "with_capacity"];

0 commit comments

Comments
 (0)