Skip to content

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

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

Closed
kornelski opened this issue Sep 10, 2023 · 0 comments · Fixed by #11483
Closed

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

kornelski opened this issue Sep 10, 2023 · 0 comments · Fixed by #11483
Labels
A-lint Area: New lints

Comments

@kornelski
Copy link
Contributor

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 ;)
}
@kornelski kornelski added the A-lint Area: New lints label Sep 10, 2023
@bors bors closed this as completed in daadab5 Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant