Clippy lints / Suspicious
Clippy lint · Suspicious · checked with Clippy 0.1.99
clippy::path_ends_with_ext: Looks for calls to Path::ends_with calls where the argument looks like a file extension. By default, Clippy has a short list of known filenames that start with a dot but aren’t necessarily file extensions (e.g
What it meansLooks for calls to Path::ends_with calls where the argument looks like a file extension. By default, Clippy has a short list of known filenames that start with a dot but aren’t necessarily file extensions (e.g. the .git folder), which are allowed by default. The allowed-dotfiles configuration can be used to allow additional file extensions that Clippy should not lint. It is in the suspicious group and warns by default.
Does it matter?
This doesn’t actually compare file extensions. Rather, ends_with compares the given argument to the last component of the path and checks if it matches exactly.
Code that is most likely wrong or useless. Warns by default.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
fn is_markdown(path: &Path) -> bool {
path.ends_with(".md")
}
Write this instead
fn is_markdown(path: &Path) -> bool {
path.extension().is_some_and(|ext| ext == "md")
}
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::path_ends_with_ext)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
path_ends_with_ext = "allow"One run
cargo clippy -- -A clippy::path-ends-with-extThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Settings in clippy.toml
allowed-dotfiles: Additional dotfiles (files or directories starting with a dot) to allow
Clippy's own documentation
What it does
Looks for calls to Path::ends_with calls where the argument looks like a file extension.
By default, Clippy has a short list of known filenames that start with a dot
but aren’t necessarily file extensions (e.g. the .git folder), which are allowed by default.
The allowed-dotfiles configuration can be used to allow additional
file extensions that Clippy should not lint.
Why is this bad?
This doesn’t actually compare file extensions. Rather, ends_with compares the given argument
to the last component of the path and checks if it matches exactly.
Known issues
File extensions are often at most three characters long, so this only lints in those cases in an attempt to avoid false positives. Any extension names longer than that are assumed to likely be real path components and are therefore ignored.
Example
fn is_markdown(path: &Path) -> bool {
path.ends_with(".md")
}
Use instead:
fn is_markdown(path: &Path) -> bool {
path.extension().is_some_and(|ext| ext == "md")
}
Configuration
-
allowed-dotfiles: Additional dotfiles (files or directories starting with a dot) to allow(default:
[])
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- non_canonical_clone_implnon-canonical implementation of `clone` on a `Copy` type
- non_canonical_partial_ord_implChecks for non-canonical implementations of PartialOrd when Ord is already implemented
- octal_escapesoctal-looking escape in a literal
- permissions_set_readonly_falsecall to `set_readonly` with argument `false`
- pointers_in_nomem_asm_blockpassing pointers to nomem asm block
- possible_missing_elsethis looks like an `else {..}` but the `else` is missing
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::non_canonical_clone_implnon-canonical implementation of `clone` on a `Copy` typeRead it next →Did this answer your question?