Skip to content

Commit b2eebee

Browse files
committedSep 19, 2024
[Clippy] Swap open_options to use diagnostic items instead of paths
1 parent 364e552 commit b2eebee

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed
 

‎compiler/rustc_span/src/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,7 @@ symbols! {
895895
field,
896896
field_init_shorthand,
897897
file,
898+
file_options,
898899
float,
899900
float_to_int_unchecked,
900901
floorf128,
@@ -1370,6 +1371,7 @@ symbols! {
13701371
on,
13711372
on_unimplemented,
13721373
opaque,
1374+
open_options_new,
13731375
ops,
13741376
opt_out_copy,
13751377
optimize,

‎library/std/src/fs.rs

+2
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ impl File {
466466
/// ```
467467
#[must_use]
468468
#[stable(feature = "with_options", since = "1.58.0")]
469+
#[cfg_attr(not(test), rustc_diagnostic_item = "file_options")]
469470
pub fn options() -> OpenOptions {
470471
OpenOptions::new()
471472
}
@@ -1009,6 +1010,7 @@ impl OpenOptions {
10091010
/// let mut options = OpenOptions::new();
10101011
/// let file = options.read(true).open("foo.txt");
10111012
/// ```
1013+
#[cfg_attr(not(test), rustc_diagnostic_item = "open_options_new")]
10121014
#[stable(feature = "rust1", since = "1.0.0")]
10131015
#[must_use]
10141016
pub fn new() -> Self {

‎src/tools/clippy/clippy_lints/src/methods/open_options.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,18 @@ fn get_open_options(
126126
&& let ExprKind::Path(path) = callee.kind
127127
&& let Some(did) = cx.qpath_res(&path, callee.hir_id).opt_def_id()
128128
{
129-
match_any_def_paths(
130-
cx,
131-
did,
132-
&[
133-
&paths::TOKIO_IO_OPEN_OPTIONS_NEW,
134-
&paths::OPEN_OPTIONS_NEW,
135-
&paths::FILE_OPTIONS,
136-
&paths::TOKIO_FILE_OPTIONS,
137-
],
138-
)
139-
.is_some()
129+
let std_file_options = [
130+
sym::file_options,
131+
sym::open_options_new,
132+
];
133+
134+
let tokio_file_options: &[&[&str]] = &[
135+
&paths::TOKIO_IO_OPEN_OPTIONS_NEW,
136+
&paths::TOKIO_FILE_OPTIONS,
137+
];
138+
139+
let is_std_options = std_file_options.into_iter().any(|sym| cx.tcx.is_diagnostic_item(sym, did));
140+
is_std_options || match_any_def_paths(cx, did, tokio_file_options).is_some()
140141
} else {
141142
false
142143
}

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

-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub const APPLICABILITY_VALUES: [[&str; 3]; 4] = [
1414
pub const DIAG: [&str; 2] = ["rustc_errors", "Diag"];
1515
pub const EARLY_CONTEXT: [&str; 2] = ["rustc_lint", "EarlyContext"];
1616
pub const EARLY_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "EarlyLintPass"];
17-
pub const FILE_OPTIONS: [&str; 4] = ["std", "fs", "File", "options"];
1817
#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates
1918
pub const FUTURES_IO_ASYNCREADEXT: [&str; 3] = ["futures_util", "io", "AsyncReadExt"];
2019
#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates
@@ -27,7 +26,6 @@ pub const LATE_CONTEXT: [&str; 2] = ["rustc_lint", "LateContext"];
2726
pub const LATE_LINT_PASS: [&str; 3] = ["rustc_lint", "passes", "LateLintPass"];
2827
pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"];
2928
pub const MSRV: [&str; 3] = ["clippy_config", "msrvs", "Msrv"];
30-
pub const OPEN_OPTIONS_NEW: [&str; 4] = ["std", "fs", "OpenOptions", "new"];
3129
pub const PARKING_LOT_MUTEX_GUARD: [&str; 3] = ["lock_api", "mutex", "MutexGuard"];
3230
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockReadGuard"];
3331
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockWriteGuard"];

0 commit comments

Comments
 (0)
Please sign in to comment.