From 0e87c7665f9d79887ecfdb0108daa859896ec8fe Mon Sep 17 00:00:00 2001 From: Mahdi Ali-Raihan Date: Tue, 4 Aug 2026 03:49:16 -0400 Subject: [PATCH] Adding diagnostic markers for multiple fs functions --- library/std/src/fs.rs | 24 ++++++++++++++++++++++++ library/std/src/os/unix/fs.rs | 1 + library/std/src/os/wasi/fs.rs | 1 + library/std/src/os/windows/fs.rs | 2 ++ 4 files changed, 28 insertions(+) diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 4c5cd0e0c9e6a..e266612c19e74 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -184,6 +184,7 @@ pub enum TryLockError { /// /// [TOCTOU]: self#time-of-check-to-time-of-use-toctou #[unstable(feature = "dirfd", issue = "120426")] +#[cfg_attr(not(test), rustc_diagnostic_item = "FsDir")] pub struct Dir { inner: fs_imp::Dir, } @@ -196,6 +197,7 @@ pub struct Dir { /// times, etc. #[stable(feature = "rust1", since = "1.0.0")] #[derive(Clone)] +#[cfg_attr(not(test), rustc_diagnostic_item = "FsMetadata")] pub struct Metadata(fs_imp::FileAttr); /// Iterator over the entries in a directory. @@ -213,6 +215,7 @@ pub struct Metadata(fs_imp::FileAttr); /// the next entry from the OS. #[stable(feature = "rust1", since = "1.0.0")] #[derive(Debug)] +#[cfg_attr(not(test), rustc_diagnostic_item = "FsReadDir")] pub struct ReadDir(fs_imp::ReadDir); /// Entries returned by the [`ReadDir`] iterator. @@ -231,6 +234,7 @@ pub struct ReadDir(fs_imp::ReadDir); /// /// [changes]: io#platform-specific-behavior #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "FsDirEntry")] pub struct DirEntry(fs_imp::DirEntry); /// Options and flags which can be used to configure how a file is opened. @@ -337,6 +341,7 @@ pub struct DirBuilder { /// } /// ``` #[stable(feature = "fs_read_write_bytes", since = "1.26.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_read")] pub fn read>(path: P) -> io::Result> { fn inner(path: &Path) -> io::Result> { let mut file = File::open(path)?; @@ -379,6 +384,7 @@ pub fn read>(path: P) -> io::Result> { /// } /// ``` #[stable(feature = "fs_read_write", since = "1.26.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_read_to_string")] pub fn read_to_string>(path: P) -> io::Result { fn inner(path: &Path) -> io::Result { let mut file = File::open(path)?; @@ -416,6 +422,7 @@ pub fn read_to_string>(path: P) -> io::Result { /// } /// ``` #[stable(feature = "fs_read_write_bytes", since = "1.26.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_write")] pub fn write, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> { fn inner(path: &Path, contents: &[u8]) -> io::Result<()> { File::create(path)?.write_all(contents) @@ -459,6 +466,7 @@ pub fn write, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result #[doc(alias = "utimens")] #[doc(alias = "utimes")] #[doc(alias = "utime")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_set_times")] pub fn set_times>(path: P, times: FileTimes) -> io::Result<()> { fs_imp::set_times(path.as_ref(), times.0) } @@ -500,6 +508,7 @@ pub fn set_times>(path: P, times: FileTimes) -> io::Result<()> { #[doc(alias = "utimensat")] #[doc(alias = "lutimens")] #[doc(alias = "lutimes")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_set_times_nofollow")] pub fn set_times_nofollow>(path: P, times: FileTimes) -> io::Result<()> { fs_imp::set_times_nofollow(path.as_ref(), times.0) } @@ -2761,6 +2770,7 @@ impl AsInner for DirEntry { /// ``` #[doc(alias = "rm", alias = "unlink", alias = "DeleteFile")] #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_remove_file")] pub fn remove_file>(path: P) -> io::Result<()> { fs_imp::remove_file(path.as_ref()) } @@ -2802,6 +2812,7 @@ pub fn remove_file>(path: P) -> io::Result<()> { /// ``` #[doc(alias = "stat")] #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_metadata")] pub fn metadata>(path: P) -> io::Result { fs_imp::metadata(path.as_ref()).map(Metadata) } @@ -2842,6 +2853,7 @@ pub fn metadata>(path: P) -> io::Result { /// ``` #[doc(alias = "lstat")] #[stable(feature = "symlink_metadata", since = "1.1.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_symlink_metadata")] pub fn symlink_metadata>(path: P) -> io::Result { fs_imp::symlink_metadata(path.as_ref()).map(Metadata) } @@ -2890,6 +2902,7 @@ pub fn symlink_metadata>(path: P) -> io::Result { /// ``` #[doc(alias = "mv", alias = "MoveFile", alias = "MoveFileEx")] #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_rename")] pub fn rename, Q: AsRef>(from: P, to: Q) -> io::Result<()> { fs_imp::rename(from.as_ref(), to.as_ref()) } @@ -2953,6 +2966,7 @@ pub fn rename, Q: AsRef>(from: P, to: Q) -> io::Result<()> #[doc(alias = "CopyFile", alias = "CopyFileEx")] #[doc(alias = "fclonefileat", alias = "fcopyfile")] #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_copy")] pub fn copy, Q: AsRef>(from: P, to: Q) -> io::Result { fs_imp::copy(from.as_ref(), to.as_ref()) } @@ -2999,6 +3013,7 @@ pub fn copy, Q: AsRef>(from: P, to: Q) -> io::Result { /// ``` #[doc(alias = "CreateHardLink", alias = "linkat")] #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_hard_link")] pub fn hard_link, Q: AsRef>(original: P, link: Q) -> io::Result<()> { fs_imp::hard_link(original.as_ref(), link.as_ref()) } @@ -3065,6 +3080,7 @@ pub fn soft_link, Q: AsRef>(original: P, link: Q) -> io::Re /// } /// ``` #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_read_link")] pub fn read_link>(path: P) -> io::Result { fs_imp::read_link(path.as_ref()) } @@ -3108,6 +3124,7 @@ pub fn read_link>(path: P) -> io::Result { #[doc(alias = "realpath")] #[doc(alias = "GetFinalPathNameByHandle")] #[stable(feature = "fs_canonicalize", since = "1.5.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_canonicalize")] pub fn canonicalize>(path: P) -> io::Result { fs_imp::canonicalize(path.as_ref()) } @@ -3196,6 +3213,7 @@ pub fn create_dir>(path: P) -> io::Result<()> { /// } /// ``` #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_create_dir_all")] pub fn create_dir_all>(path: P) -> io::Result<()> { DirBuilder::new().recursive(true).create(path.as_ref()) } @@ -3241,6 +3259,7 @@ pub fn create_dir_all>(path: P) -> io::Result<()> { /// ``` #[doc(alias = "rmdir", alias = "RemoveDirectory")] #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_remove_dir")] pub fn remove_dir>(path: P) -> io::Result<()> { fs_imp::remove_dir(path.as_ref()) } @@ -3305,6 +3324,7 @@ pub fn remove_dir>(path: P) -> io::Result<()> { /// } /// ``` #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_remove_dir_all")] pub fn remove_dir_all>(path: P) -> io::Result<()> { fs_imp::remove_dir_all(path.as_ref()) } @@ -3384,6 +3404,7 @@ pub fn remove_dir_all>(path: P) -> io::Result<()> { /// ``` #[doc(alias = "ls", alias = "opendir", alias = "FindFirstFile", alias = "FindNextFile")] #[stable(feature = "rust1", since = "1.0.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_read_dir")] pub fn read_dir>(path: P) -> io::Result { fs_imp::read_dir(path.as_ref()).map(ReadDir) } @@ -3435,6 +3456,7 @@ pub fn read_dir>(path: P) -> io::Result { /// ``` #[doc(alias = "chmod", alias = "SetFileAttributes")] #[stable(feature = "set_permissions", since = "1.1.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_set_permissions")] pub fn set_permissions>(path: P, perm: Permissions) -> io::Result<()> { fs_imp::set_permissions(path.as_ref(), perm.0) } @@ -3496,6 +3518,7 @@ pub fn set_permissions>(path: P, perm: Permissions) -> io::Result /// ``` #[doc(alias = "fchmodat", alias = "SetFileInformationByHandle")] #[unstable(feature = "set_permissions_nofollow", issue = "141607")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_set_permissions_nofollow")] pub fn set_permissions_nofollow>(path: P, perm: Permissions) -> io::Result<()> { fs_imp::set_permissions_nofollow(path.as_ref(), perm.0) } @@ -3643,6 +3666,7 @@ impl AsInnerMut for DirBuilder { /// [`Path::exists`]: crate::path::Path::exists /// [TOCTOU]: self#time-of-check-to-time-of-use-toctou #[stable(feature = "fs_try_exists", since = "1.81.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_exists")] #[inline] pub fn exists>(path: P) -> io::Result { fs_imp::exists(path.as_ref()) diff --git a/library/std/src/os/unix/fs.rs b/library/std/src/os/unix/fs.rs index c119912c3b022..2d2e782c0671a 100644 --- a/library/std/src/os/unix/fs.rs +++ b/library/std/src/os/unix/fs.rs @@ -1060,6 +1060,7 @@ impl DirEntryExt2 for fs::DirEntry { /// } /// ``` #[stable(feature = "symlink", since = "1.1.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_symlink")] pub fn symlink, Q: AsRef>(original: P, link: Q) -> io::Result<()> { sys::fs::symlink(original.as_ref(), link.as_ref()) } diff --git a/library/std/src/os/wasi/fs.rs b/library/std/src/os/wasi/fs.rs index fc9e6c925f0bd..369e5d4b0fb9d 100644 --- a/library/std/src/os/wasi/fs.rs +++ b/library/std/src/os/wasi/fs.rs @@ -451,6 +451,7 @@ pub fn symlink, U: AsRef>( /// /// This is a convenience API similar to `std::os::unix::fs::symlink` and /// `std::os::windows::fs::symlink_file` and `std::os::windows::fs::symlink_dir`. +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_symlink_path")] pub fn symlink_path, U: AsRef>(old_path: P, new_path: U) -> io::Result<()> { crate::sys::fs::symlink(old_path.as_ref(), new_path.as_ref()) } diff --git a/library/std/src/os/windows/fs.rs b/library/std/src/os/windows/fs.rs index dfa9236a7e428..7d8602762530a 100644 --- a/library/std/src/os/windows/fs.rs +++ b/library/std/src/os/windows/fs.rs @@ -719,6 +719,7 @@ impl FileTimesExt for fs::FileTimes { /// /// [symlink-security]: https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/create-symbolic-links #[stable(feature = "symlink", since = "1.1.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_symlink_file")] pub fn symlink_file, Q: AsRef>(original: P, link: Q) -> io::Result<()> { sys::fs::symlink_inner(original.as_ref(), link.as_ref(), false) } @@ -758,6 +759,7 @@ pub fn symlink_file, Q: AsRef>(original: P, link: Q) -> io: /// /// [symlink-security]: https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/create-symbolic-links #[stable(feature = "symlink", since = "1.1.0")] +#[cfg_attr(not(test), rustc_diagnostic_item = "fs_symlink_dir")] pub fn symlink_dir, Q: AsRef>(original: P, link: Q) -> io::Result<()> { sys::fs::symlink_inner(original.as_ref(), link.as_ref(), true) }