rust-lang / rust-lang/rust-clippy

File path comparison without canonicalizing

Open
#17,155 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

When comparing paths, std does this purely on a string equality basis, which will fail with things like symlinks or relative paths. We should be recommending people use std::fs::canonicalize to avoid mistakes.

Advantage

Using canonicalize on a Path makes it so that there are no possibilities of paths that are supposed to be equal ever being considered different.

Drawbacks

No response

Example
    let p = std::path::Path::new("foo/../foo");
    p == Path::new("foo");
    p == "foo";

Could be written as:

    std::fs::canonicalize(p) == Path::new("foo");
    std::fs::canonicalize(p) == "foo";
Comparison with existing lints

No response

Additional Context

This is one of the bugs found in uutils by Canonical's audit: https://corrode.dev/blog/bugs-rust-wont-catch/#string-equality-on-paths-is-not-the-same-as-filesystem-identity

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named. Start by locating Clippy's existing lints for Path comparisons and review the two example comparisons alongside the canonicalize recommendation. Done means the lint's scope, applicability, and behavior for these cases are defined and covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.