Email me

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-ext

The [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
Suspiciousgroup
warndefault level
nofixed by cargo clippy --fix
1.74.0added in Rust

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

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?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ