-
Notifications
You must be signed in to change notification settings - Fork 1.8k
new lint: [useless_pathbuf_conversion
]
#15640
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
dbaranov34
wants to merge
9
commits into
rust-lang:master
Choose a base branch
from
dbaranov34:a-lint-useless_pathbuf_conversion
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9665f66
implemeted lint, but the other lint now fails expectedly
dbaranov34 99e9718
rm useless pathbuf check from unnecessary_to_owned_check
dbaranov34 4380e83
cleanup and clippy
dbaranov34 e735110
update lints in changelog
dbaranov34 a43202b
addressed pr comments
dbaranov34 54211e6
addressed pr comments
dbaranov34 e3ec8c0
addressed pr comments
dbaranov34 aaa4e16
correct usage of snippet_with_context
dbaranov34 5412590
fmt
dbaranov34 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::is_path_diagnostic_item; | ||
use clippy_utils::source::snippet_with_context; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
|
||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind, QPath}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::declare_lint_pass; | ||
use rustc_span::symbol::sym; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Detects unnecessary `&PathBuf::from(...)` when `&Path` would suffice. | ||
/// | ||
/// **Why is this bad?** `PathBuf::from` allocates a new heap buffer unnecessarily. | ||
/// | ||
/// **Example:** | ||
/// ```rust | ||
/// fn use_path(p: &std::path::Path) {} | ||
/// use_path(&std::path::PathBuf::from("abc")); | ||
/// ``` | ||
/// Could be written as: | ||
/// ```rust | ||
/// fn use_path(p: &std::path::Path) {} | ||
/// use_path(std::path::Path::new("abc")); | ||
/// ``` | ||
#[clippy::version = "1.92.0"] | ||
pub USELESS_PATHBUF_CONVERSION, | ||
complexity, | ||
"creating a `PathBuf` only to take a reference, where `Path::new` would suffice" | ||
} | ||
|
||
declare_lint_pass!(UselessPathbufConversion => [USELESS_PATHBUF_CONVERSION]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for UselessPathbufConversion { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | ||
if let ExprKind::AddrOf(_, _, inner) = &expr.kind | ||
&& let ExprKind::Call(func, [arg]) = &inner.kind | ||
&& let ExprKind::Path(ref qpath) = func.kind | ||
&& let QPath::TypeRelative(pathbuf, from) = qpath | ||
&& is_path_diagnostic_item(cx, *pathbuf, sym::PathBuf) | ||
&& from.ident.name == sym::from | ||
&& let expr_ty = cx.typeck_results().expr_ty_adjusted(expr) | ||
&& let rustc_middle::ty::Ref(_, inner_ty, _) = expr_ty.kind() | ||
&& is_type_diagnostic_item(cx, *inner_ty, sym::Path) | ||
{ | ||
let mut app = Applicability::MachineApplicable; | ||
let arg_snippet = snippet_with_context(cx, arg.span, inner.span.ctxt(), "..", &mut app).0; | ||
|
||
let sugg = format!("Path::new({arg_snippet})"); | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
USELESS_PATHBUF_CONVERSION, | ||
expr.span, | ||
"unnecessary `PathBuf::from` when a `&Path` is enough", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, this lint doesn't actually check whether a fn takes_ref_pathbuf(_: &PathBuf) {}
fn main() {
takes_ref_pathbuf(&PathBuf::from("path"));
} |
||
"consider using", | ||
sugg, | ||
app, | ||
); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most functions working with
Path
s will have the parameter specified asimpl AsRef<Path>
, which this lint unfortunately wouldn't catch right now.This is very similar to a lint I suggested myself: #14668, and I'm not sure what the interplay between them should be. That lint basically checks two things:
impl AsRef<Path>
isn't used elsewhere in the functionx
inPath::new(x)
implements all the trait bounds of that generic (notablyAsRef
, but often alsoSized
), and therefore could be used directlySo this lint would probably need to copy the first check from that lint; and at this point one could argue that it could go the extra mile and also check the second thing, which would allow it to replace
&PathBuf::from(x)
directly withx
-- otherwise, one would first createPath::new(x)
, and then need to repeat the entirety of the first check on that.Now that I've written this out though, the first check isn't actually all that big, and also it's preferable to let the lints compose, so this is probably fine after all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for breaking it down. I agree, let the lints complement each other.