Skip to content

File and Metadata should document reasons to prefer *not* to use is_file() #64170

Closed
@kentfredric

Description

@kentfredric

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, using is_file() instead of !is_dir() would prevent you from reading block devices, character devices, pipes, and more:

           S_IFSOCK   0140000   socket
           S_IFLNK    0120000   symbolic link
           S_IFREG    0100000   regular file
           S_IFBLK    0060000   block device
           S_IFDIR    0040000   directory
           S_IFCHR    0020000   character device
           S_IFIFO    0010000   FIFO

And this would break your ability to be composed nicely in Unix flows where patterns like:

diff <( prog_a ) <( prog_b )

Would become broken if diff was a rust program that ensured all its arguments were is_file()

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions