-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
File and Metadata should document reasons to prefer *not* to use is_file()
#64170
Comments
This isn't specific to I think it would be better to add a more general warning to the top-level documentation that symlink/file/directory is not exhaustive and one has to take the platform-extensions into account if one wants to deal with arbitrary user-provided files or directory-trees. |
Sure, as long as the documentation conveys "If you are using this for usecase X, then !is_dir() is probably more appropriate". Just because the average novice programmer who first encounters the need to differentiate that a given thing is what they consider to be "a file", and they'll point ( People have been making this mistake in many languages for a long time, and it would be nice if rust had some way to encourage better systems development in this regard, good docs and applicable lints would go far ) |
…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.
…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.
…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.
As per rust-lang/rust-clippy#4503, there's a bit of a trap you can fall into if you use
is_file()
as a predictor for "can I probably open and read bytes from this thing".You generally don't want to assert
is_file()
for user specified paths, especially if they come from command line arguments, where they might be Unix FIFO File Descriptors.If for example you were implementing
cat
in rust, usingis_file()
instead of!is_dir()
would prevent you from reading block devices, character devices, pipes, and more:And this would break your ability to be composed nicely in Unix flows where patterns like:
Would become broken if diff was a rust program that ensured all its arguments were
is_file()
The text was updated successfully, but these errors were encountered: