Unused variable in macro generated code
I wrote a macro that implements Scala-like for understanding in Rust. This will do the following:
map_for!{
x <- 0..4;
y = 2*x;
z <- 0..1;
=> y+z
}
in it:
((0..4).map (move |x| { let y = 2 * x; (x, y) }))
.flat_map (move |params| {
let (x, y) = params;
(0..1).map (move |z| { y + z })
})
This works, but the compiler issues an "unused variable" warning because it is x
not used internally flat_map
. I can turn off the warning by adding #[allow(unused_variables)]
before the operations let
in the macro, but then it removes all unused variable warnings so that this:
map_for!{
x <- 0..4;
y = 2;
z <- 0..1;
=> y+z
}
will expand to:
((0..4).map (move |x| { let y = 2; (x, y) }))
.flat_map (move |params| {
#[allow(unused_variables)]
let (x, y) = params;
(0..1).map (move |z| { y + z })
})
and will not generate a warning even if x
not actually used.
Is there a way to make it so that the first example doesn't raise a warning, but the second will?
The complete macro code with warnings is available , as well as the complete code with the warning suppressed .
source to share