Skip to content

Added a new lint path_comparison_to_empty - #17651

Open
aljazluci wants to merge 5 commits into
rust-lang:masterfrom
aljazluci:master
Open

aljazluci wants to merge 5 commits into
rust-lang:masterfrom
aljazluci:master

Conversation

@aljazluci

@aljazluci aljazluci commented Aug 29, 2026

Copy link
Copy Markdown

changelog: [path_comparison_to_empty] Added a new lint path_comparison_to_empty, which checks for comparing a path to Path::new("") or PathBuf::new() and suggests to use is_empty

fixes: #17615

@rustbot rustbot added the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Aug 29, 2026
@rustbot

rustbot commented Aug 29, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome!

You should hear from one of our reviewers after this PR gets at least 2 reviews from the community.

Please see the contribution instructions for more information.

@rustbot rustbot added needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Aug 29, 2026

@Gri-ffin Gri-ffin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lint could also benefit from checking PathBuf::from("").

View changes since this review

Comment thread clippy_lints/src/path_comparison_to_empty.rs Outdated
Comment thread clippy_lints/src/path_comparison_to_empty.rs Outdated
Comment thread clippy_lints/src/path_comparison_to_empty.rs

@CommanderStorm CommanderStorm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM, but I suspect that there are a few cases where I don't know if they are handled correctly, so could you add a testcase for them? 🤔

View changes since this review

Comment on lines +56 to +57
// in case of `Path::new("") == Path::new("")` the lint doesn't make much sense
if !(left_path_new_empty_string ^ right_path_new_empty_string) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotta love a nice XOR! 👍🏻

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usually, we try a bit to more split than the very long fn main.
Could you split this up into a few functions for the different cases in an mod main?
This way everything is organised.

Also, please add a test to ensure that macro expansions are correctly handled like in the rest of the codebase. I am not sure if the last part needs an return from the fn, I think we do.
I also suspect that there might be testcase missing when the path is coming from an const.

Co-authored-by: Gri-ffin <82527700+Gri-ffin@users.noreply.github.com>

@DanielEScherzer DanielEScherzer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

community review: thanks for writing this lint I suggested

View changes since this review

/// ```
#[clippy::version = "1.100.0"]
pub PATH_COMPARISON_TO_EMPTY,
style,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is also a performance improvement, suggest the performant category instead

@CommanderStorm CommanderStorm Aug 31, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is supprisingly true, rustc seens to not be able to see past this

https://godbolt.org/z/a16MEP47G

@asder8215 asder8215 Sep 7, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, the reason why using Path::is_empty is faster than doing path equality in this case is because you don't have to go through doing component based equality using Path::components (which is a bit more involved than just checking the length of the path). I'm currently working on optimizing it for Unix/non-prefixed based platforms here, but regardless Path::is_empty is better to use over path == Path::new("").

Which reminds me that I should swap out the path == Path::new("") for path.is_empty() in the create_dir_all update PR I was working on

@rustbot

rustbot commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly #17644) made this pull request unmergeable. Please resolve the merge conflicts.

@asder8215

asder8215 commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

I agree with @Gri-ffin that the lint could benefit from checking PathBuf::from(""). I will also note that it also has a benefit from checking with an empty string, "", directly as well because the PartialEq trait implementation for Path on str actually end up doing components based equality as well. It's the reason why this assertion:

assert_eq!(Path::new("//"), "/");

succeeds. You can see that in this rust playground link. Internally, they all end up triggering impl PartialEq for Path (or the impl PartialEq for PathBuf, which also does components based equality).

Technically, all the empty string variations (e.g. str, String, OsStr, OsString, Cow) comparing with a Path/PathBuf could all be checked and suggested to use Path::is_empty instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lint suggestion: use Path::is_empty() instead of manual comparisons

6 participants