Closed
Description
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:
fn main() {
let mut l = vec![0];
debug_assert!(l.pop().is_some());
println!("{:?}", l.len());
}
And notice the difference when it is compiled with an without optimization
$ rustc -O debug.rs && ./debug
1
$ rustc debug.rs && ./debug
0
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
- Was in the user's original source?
- Was originally inside one of the
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.