Skip to content

Commit d31b8ba

Browse files
authored
Rollup merge of rust-lang#73243 - poliorcetics:discourage-is-file, r=Amanieu
Add documentation to point to `File::open` or `OpenOptions::open` instead of `is_file` to check read/write possibility Fixes rust-lang#64170. This adds documentation to point user towards `!is_dir` instead of `is_file` when all they want to is read from a source. I ran `rg "fn is_file\("` to find all `is_file` methods, I hope I did not miss one.
2 parents e5343ec + 8e8c54a commit d31b8ba

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

Diff for: src/libstd/fs.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1033,8 +1033,16 @@ impl Metadata {
10331033
/// [`is_dir`], and will be false for symlink metadata
10341034
/// obtained from [`symlink_metadata`].
10351035
///
1036+
/// When the goal is simply to read from (or write to) the source, the most
1037+
/// reliable way to test the source can be read (or written to) is to open
1038+
/// it. Only using `is_file` can break workflows like `diff <( prog_a )` on
1039+
/// a Unix-like system for example. See [`File::open`] or
1040+
/// [`OpenOptions::open`] for more information.
1041+
///
10361042
/// [`is_dir`]: struct.Metadata.html#method.is_dir
10371043
/// [`symlink_metadata`]: fn.symlink_metadata.html
1044+
/// [`File::open`]: struct.File.html#method.open
1045+
/// [`OpenOptions::open`]: struct.OpenOptions.html#method.open
10381046
///
10391047
/// # Examples
10401048
///
@@ -1307,8 +1315,16 @@ impl FileType {
13071315
/// [`is_dir`] and [`is_symlink`]; only zero or one of these
13081316
/// tests may pass.
13091317
///
1318+
/// When the goal is simply to read from (or write to) the source, the most
1319+
/// reliable way to test the source can be read (or written to) is to open
1320+
/// it. Only using `is_file` can break workflows like `diff <( prog_a )` on
1321+
/// a Unix-like system for example. See [`File::open`] or
1322+
/// [`OpenOptions::open`] for more information.
1323+
///
13101324
/// [`is_dir`]: struct.FileType.html#method.is_dir
13111325
/// [`is_symlink`]: struct.FileType.html#method.is_symlink
1326+
/// [`File::open`]: struct.File.html#method.open
1327+
/// [`OpenOptions::open`]: struct.OpenOptions.html#method.open
13121328
///
13131329
/// # Examples
13141330
///

Diff for: src/libstd/path.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -2503,11 +2503,20 @@ impl Path {
25032503
/// # See Also
25042504
///
25052505
/// This is a convenience function that coerces errors to false. If you want to
2506-
/// check errors, call [fs::metadata] and handle its Result. Then call
2507-
/// [fs::Metadata::is_file] if it was Ok.
2508-
///
2509-
/// [fs::metadata]: ../../std/fs/fn.metadata.html
2510-
/// [fs::Metadata::is_file]: ../../std/fs/struct.Metadata.html#method.is_file
2506+
/// check errors, call [`fs::metadata`] and handle its Result. Then call
2507+
/// [`fs::Metadata::is_file`] if it was Ok.
2508+
///
2509+
/// When the goal is simply to read from (or write to) the source, the most
2510+
/// reliable way to test the source can be read (or written to) is to open
2511+
/// it. Only using `is_file` can break workflows like `diff <( prog_a )` on
2512+
/// a Unix-like system for example. See [`File::open`] or
2513+
/// [`OpenOptions::open`] for more information.
2514+
///
2515+
/// [`fs::metadata`]: ../../std/fs/fn.metadata.html
2516+
/// [`fs::Metadata`]: ../../std/fs/struct.Metadata.html
2517+
/// [`fs::Metadata::is_file`]: ../../std/fs/struct.Metadata.html#method.is_file
2518+
/// [`File::open`]: ../../std/fs/struct.File.html#method.open
2519+
/// [`OpenOptions::open`]: ../../std/fs/struct.OpenOptions.html#method.open
25112520
#[stable(feature = "path_ext", since = "1.5.0")]
25122521
pub fn is_file(&self) -> bool {
25132522
fs::metadata(self).map(|m| m.is_file()).unwrap_or(false)

0 commit comments

Comments
 (0)