-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Detecting mutation in debug_assert!
#1526
Comments
Heh, this is a pretty neat bug. |
I'd lint this in any kind of assert macro. (Also works nicely as underhanded code btw) |
Maybe one could generalize that: If a method call with |
This shouldn't be that hard to implement anymore. With rust-clippy/clippy_lints/src/utils/mod.rs Lines 747 to 756 in df80193
we can find expressions that come from direct expansions of |
How to exactly do that? I tried it with a
where you can see the result of How can I receive the mutability of the arguments passed to the function? Is it even possible in HIR? |
@flip1995 can you give me a hint to my question, please? :) |
Oh sorry, I missed that one. I'm not really sure myself on how to do this. What I would try is to get the I don't think this is a problem if something mutable is passed by move, since either
|
Problem
Hey there. I stumbled upon a possible use case for clippy: detecting mutation inside
debug_assert!
(and its two sister macros).Why might this be a problem? Consider this program:
And notice the difference when it is compiled with an without optimization
Generally this is undesirable.
Solution
I wanted to ask how solvable this is. As I understand it, by the time the compiler has type information (which would be needed to approximately detect mutation), macros have long-since been expanded. Would it be possible to find out whether a potentially-mutating statement
debug_assert*
macros?Any advice would be much appreciated!
Other Things
Perhaps this is not worth linting -- is there a significant class of false positive?
Also, the embarrassing evidence that this can be a problem.
The text was updated successfully, but these errors were encountered: