Skip to content

New lint: needless Path::new #14668

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

Open
ada4a opened this issue Apr 22, 2025 · 4 comments · May be fixed by #14895
Open

New lint: needless Path::new #14668

ada4a opened this issue Apr 22, 2025 · 4 comments · May be fixed by #14895
Labels
A-lint Area: New lints

Comments

@ada4a
Copy link
Contributor

ada4a commented Apr 22, 2025

What it does

Checks whether a &str is wrapped in Path::new before being passed to a function that accepts impl AsRef<Path>. So somewhat similar to needless_borrow

Advantage

  • more concise
  • avoids the import

Drawbacks

can't think of any...

Example

std::fs::write(Path::new("foo.txt"), "foo");

Could be written as:

std::fs::write("foo.txt", "foo");
@ada4a ada4a added the A-lint Area: New lints label Apr 22, 2025
@ada4a
Copy link
Contributor Author

ada4a commented Apr 22, 2025

I've never implemented a lint before, but would like to try implementing this one if it's deemed useful:)

@ada4a ada4a changed the title needless_path_new New lint: needless Path::new Apr 22, 2025
@ada4a
Copy link
Contributor Author

ada4a commented Apr 25, 2025

The same goes for wrapping in &PathBuf::from, which is even since it makes a completely needless allocation

@ada4a
Copy link
Contributor Author

ada4a commented Apr 25, 2025

Reading through the guide, it looks like this lint will need to be late-pass, and so somewhat more complicated. The guide mentions LatePassLint, but it's hard to understand what its methods are supposed to do since there doesn't seem to be any documentation whatsoever. I would appreciate pointers to similar lints so that I could hopefully imitate them...

@Alexendoo
Copy link
Member

A lint pass visits the whole crate, whenever the visitor sees an expression for example it calls check_expr, when it sees an item it will call check_item etc.

The individual docs for Item, Expr and so on have more details

@ada4a ada4a linked a pull request May 25, 2025 that will close this issue
2 tasks
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.

2 participants