You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Detect assignments to Copy values that are immediately discarded.
rustc is pretty good at detecting when variables are assigned but never read, and Clippy has a temporary_assignments lint to catch assignments to temporaries, but neither of them catch the following case:
Whether or not new_point has side-effects should be irrelevant: assigning to its .x field and then immediately throwing it away is definitely pointless (no pun intended 😏). Things are more complicated if Point has a drop implementation, hence the lint should probably be restricted to types that implement Copy.
Categories
Kind: correctness
Drawbacks
Potentially difficult to implement
Example
This example is more similar to the one I encountered in the real world: a Copy type with a mutable getter and a copying getter, where the copying getter is called by mistake:
I think it may be worthwhile to implement as a hard error in rustc itself. I couldn't come with any valid scenario even for Drop types. And ever then it could be rewritten with temporary local
Well you can always add explicit drop(p) in the end. It's a bit quircky I agree but how many times anyone would write this mutate-and-call-drop-to-make-some-side-effect? It something really weird and weird things deserve to be longer to make people think why they are doing this (hence long unsafe method names btw) and they also may be longer for sake of saving general case, which is the case in our case.
What it does
Detect assignments to
Copy
values that are immediately discarded.rustc
is pretty good at detecting when variables are assigned but never read, and Clippy has atemporary_assignments
lint to catch assignments to temporaries, but neither of them catch the following case:Whether or not
new_point
has side-effects should be irrelevant: assigning to its.x
field and then immediately throwing it away is definitely pointless (no pun intended 😏). Things are more complicated ifPoint
has a drop implementation, hence the lint should probably be restricted to types that implementCopy
.Categories
Drawbacks
Potentially difficult to implement
Example
This example is more similar to the one I encountered in the real world: a
Copy
type with a mutable getter and a copying getter, where the copying getter is called by mistake:The text was updated successfully, but these errors were encountered: