From b2eebeeea982c544f6740fb3883ea046ded39e7f Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Thu, 19 Sep 2024 12:19:48 +0100 Subject: [PATCH] [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"];