Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upCatching unnecessary clones #17
Comments
Manishearth
added
E-hard
T-middle
L-lint
C-needs-discussion
labels
Aug 11, 2015
This comment has been minimized.
This comment has been minimized.
|
As you recently noted that you are currently thinking on how to implement this, why not pool our neuronal resources and think in the open? To create a good lint against unnecessary clones, we have a few problems ranging from very easy to near impossible: '1. (ridiculously easy) Finding a .clone() call. At this point we could already lint against clones of non internally mutable values just for borrowing them immutably. It's not much, but it's a start. There is one caveat however: We'd somehow need to be sure to rule out side effects on cloning or mutating the value (e.g. some global registry could be updated). '5. (medium) Finding out if the original value is used elsewhere afterwards At this point we could lint against clones for those by-value ops, with the aforementioned caveat. '6. (hard) Finding out if the op/call mutates the value (if it is internally mutable or borrowed mutably) At this point we could add cases where the value isn't mutated to our lint (for mutably borrowed or internally mutable values). The caveat against side effects from above applies here, too. '7. (near impossible) Finding out if cloning or the checked operation on the value has any side effects. If we can rule this out, the caveat no longer applies, and we have a lint that warns against unnecessary clones without any false positives. There is still one problem left: '8. (near impossible) Creating a good suggestion depending on how the value is available and is used – for example we may have to make lifetimes line up to ensure the original value can even be used. Perhaps we should add an [E-impossible] label for such things? |
This comment has been minimized.
This comment has been minimized.
|
At least for '4, '6 and '7, blacklisting would give us a good bang for quite little buck. For those cases, '8 would probably also be tractable. Perhaps we should ask the compiler to annotate types with "is internally mutable" flags (edit: On the other hand, it would probably suffice to look at the public fields and look for mutable interior there), method arguments with "is mutated during this method" and methods with "mutate global state". This would allow us to do all the above analysis (apart from '8, which I think is still hard) in a reliable way. |
This comment has been minimized.
This comment has been minimized.
|
Oh, that's not the type of lint I meant. I was focusing on moving and clones. Basically, in many cases folks clone things when the variable could be moved out. For example:
Such things can lead to massive perf improvements are actually quite a common pitfall since Rust often makes folks I suspect the "unused variable" lint (which uses ExprUseVisitor) can be tweaked so that a clone is not counted as a "use" (this fixes the above case). But, this may not be perfect when it comes to branching (I need to think about this more). The alternative is to set up a bit vector framework similar to live variables analysis with a couple more states, and feed it the control flow graph. This isn't too hard either, but needs me to sit down with a whiteboard. |
This comment has been minimized.
This comment has been minimized.
|
But what if too or bar modify the vec? |
llogiq
closed this
Aug 31, 2015
llogiq
reopened this
Aug 31, 2015
This comment has been minimized.
This comment has been minimized.
|
Oops, wrong button. |
This comment has been minimized.
This comment has been minimized.
|
We just have to take care not to count
would be done within the rustc framework. |
This comment has been minimized.
This comment has been minimized.
Well, you can't use the vec after the function ends anyway, so it doesn't matter if |
This comment has been minimized.
This comment has been minimized.
|
True that. In that case the analysis is actually easier than I thought. |
This comment has been minimized.
This comment has been minimized.
|
Yep. We don't care about mutation. This is about liveness/use. |
This comment has been minimized.
This comment has been minimized.
ericsink
commented
Sep 20, 2015
|
I came here to see if this lint was available, so I'm glad to see it being discussed. I've got a bunch of Rust code ported from F#. In cases where ownership wasn't clear in the original code, I temporarily threw in a clone(). I recently fixed one situation where I was cloning stuff from a hierarchy that was gonna get dropped anyway. A lint in this area would be super helpful for this kind of situation. |
This comment has been minimized.
This comment has been minimized.
|
I'd also like to see something that looks for let mut x = ...
...
x = y.clone();and suggests changing it to let mut x = ...
...
x.clone_from(y); |
This comment has been minimized.
This comment has been minimized.
|
We should be able to write this as a MIR lint. If anyone wants to take a crack at that, I'll happily mentor. |
This comment has been minimized.
This comment has been minimized.
jfirebaugh
commented
Feb 18, 2019
|
Should this be closed now that #3355 is merged? |
Manishearth commentedNov 21, 2014
Not sure what the heuristics for this should be. Will have to think.