Skip to content

Convert redundant_clone to an analysis pass take 2 #14599

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
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Jarcho
Copy link
Contributor

@Jarcho Jarcho commented Apr 12, 2025

supersedes #11364
fixes #10940
fixes #10893
fixes #10870
fixes #10577
fixes #10545
fixes #10517

Don't worry about reviewing this as a whole, I'll be splitting it up into smaller chunks to be reviewed separately.

This comes with a separate dataflow framework than rustc's since many changes would be needed to track values correctly. As an example a mutable borrow of a pair of strings &mut (String, String) would need to cause both strings to hold different values and the only way to do that in rustc's framework is to track sub-statement positions. The new framework lets us easily do a preprocessing pass at the expense of not trivially supporting cursors.

Arena allocation is used heavily throughout. This simplifies some lifetimes both by using only a single lifetime parameter and not having to store as many owning values. It also avoids having a large number of small allocations that need to also be dropped.

No suggestions are give with the lint. This is unfortunate but they are non-trivial to add since removing the clone call is only correct in a subset of cases. Future uses of a projection may need to be changed; the clone call might be changed to a move of a different name (x.clone() -> y) or changed to a borrow. mem::take is needed in some cases as well.

The diagnostic output is currently a stand-in. I will be adding notes about where the values diverge and where the lifetimes of the pair of values end. Basically a message saying one value is modified, but the other isn't used after that point before it's dropped.

changelog: redundant_clone: Fix false positives where the clone result is stored in a local.
changelog: redundant_clone: Track the use of the clone throughout the function body rather than only checking for immediate consumption.
changelog: redundant_clone: Extended to analyze code in loops
changelog: redundant_clone: Track values inside fields rather than just tracking top level locals.

@rustbot
Copy link
Collaborator

rustbot commented Apr 12, 2025

r? @blyxyas

rustbot has assigned @blyxyas.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 12, 2025
@Jarcho Jarcho force-pushed the rclone branch 2 times, most recently from 2a20b61 to 6ec4a89 Compare April 12, 2025 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment