-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add assigning_clones
lint
#12077
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
Add assigning_clones
lint
#12077
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @xFrednet (or someone else) soon. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
Hey, thank you for the picking up the issue and creating a new PR. I'm currently in the middle of my exams and will probably not have time to review this in a timely manner. Therefore, I'll reassign it, please excuse the delay thus far. r? rust-lang/clippy |
r? rust-lang/clippy |
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.
Meow meow very good first iteration! (●ↀωↀ●)✧ Just a few notes, mainly to improve performance (those 3 CPU cycles are important! /s)
It was actually a second iteration, thanks to earlier work by kpreid, which made my work a lot easier :) Pushed a commit with review changes. Thanks for taking a look! |
Before any more reviewing, could you fix dogfood? |
Done. Yeah, the lint shouldn't fire in macros I suppose. I saw the |
It can lint in local macros definitions, don't worry about that. You can use |
I tried to use it, but then it was auto-fixing code inside a macro, which was broken. |
?? Try this diff
It worked perfectly for me, not a single error ฅ(•ㅅ•❀)ฅ meow |
Oh, yeah, this will work, because before that I already do this: let expn_data = assign_expr.span().ctxt().outer_expn_data();
match expn_data.kind {
ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) | ExpnKind::Macro(..) => return,
ExpnKind::Root => {},
} which filters all macro invocations, so if we're inside a macro, we won't ever get to the The issue that I was describing is that if I removed these 5 lines from the beginning, and only used macro_rules! clone_inside {
($a:expr, $b: expr) => {
$a = $b.clone();
};
}
// turns into v
macro_rules! clone_inside {
($a:expr, $b: expr) => {
a.clone_from(&b);
};
} It replaces the body within the macro, which is not ideal. |
☔ The latest upstream changes (presumably #12160) made this pull request unmergeable. Please resolve the merge conflicts. |
2bcb00d
to
a881d48
Compare
Rebased to fix conflicts. |
a881d48
to
cf2e79f
Compare
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.
For a first iteration, this LGTM.
Today I'll check for that TODO
, but this could be merged right now. Sorry for the delay, I kinda forgot about this PR for a while and getting a 1K PR back to speed isn't the easiest thing in the world 😅
}; | ||
if is_trait_method(cx, expr, sym::Clone) && path.ident.name == sym::clone { | ||
(TargetTrait::Clone, CallKind::MethodCall { receiver }, resolved_method) | ||
} else if is_trait_method(cx, expr, sym::ToOwned) && path.ident.name.as_str() == "to_owned" { |
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.
Curious here how, even though we check that expr
is from ToOwned
, we cannot check for sym::to_owned_method
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.
We could, I guess that I just wanted to make it a bit more consistent with the checking of the clone
method.
☔ The latest upstream changes (presumably #12010) made this pull request unmergeable. Please resolve the merge conflicts. |
Rebased to fix conflicts. |
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.
LGTM, thanks! ❤️
@bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
I have wondered lately if it should be enabled by default in perf, it's unclear whether it helps more often than being annoying. |
This PR is a "revival" of #10613 (with @kpreid's permission).
I tried to resolve most of the unresolved things from the mentioned PR:
std::clone::Clone::clone
orstd::borrow::ToOwned::to_owned
.clone_from/clone_into
. Notably, it will not trigger for types that use#[derive(Clone)]
.Deref
handling has been (hopefully) a bit improved, but I'm not sure if it's ideal yet.I also added a bunch of additional tests.
There are a few things that could be improved, but shouldn't be blockers:
::std::clone::Clone::clone(...)
. It would be nice to either auto-import theClone
trait or use the original path and modify it (e.g.clone::Clone::clone
->clone::Clone::clone_from
). I don't know how to modify theQPath
to do that though.These cases probably won't be super common, but it would be nice to make the lint more precise. I'm not sure how to do that though, I'd need access to some dataflow analytics or something like that.
changelog: new lint [
assigning_clones
]