Skip to content

path.ends_with(".ext") won't match file extension #11479

Closed
@kornelski

Description

@kornelski

What it does

Path.ends_with works differently than str.ends_with, and an attempt to match a file extension can unexpectedly only compare to the whole file name.

Advantage

It catches a potential gotcha.

Drawbacks

path.ends_with(".ext") is a mistake when used with file name extensions, but could be valid for a dot-file, like path.ends_with(".rustup").

Example

fn is_markdown(path: &Path) -> bool {
    path.ends_with(".md")
}

Could be written as:

fn is_markdown(path: &Path) -> bool {
    path.extension().map_or(false, |ext| ext == "md") // then run clippy --fix to make it case-insensitive ;)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions