The following snippet (playground)
// (note everything is public. changing this is not on the table)
pub struct HCons<A, B> { pub head: A, pub tail: B }
#[macro_export]
macro_rules! pat {
($a:pat, $b:pat) => { HCons { head: $a, tail: $b } };
}
fn main() {
let list = HCons { head: 0, tail: 0 };
let pat!(head, tail) = list;
println!("{:?} {:?}", head, tail);
}
produces these warnings:
Compiling playground v0.0.1 (file:///playground)
warning: the `head:` in this pattern is redundant
--> src/main.rs:5:18
|
5 | => { HCons { head: $a, tail: $b }};
| ^^^^ help: remove this
...
11 | let pat!(head, tail) = list;
| ---------------- in this macro invocation
|
= note: #[warn(non_shorthand_field_patterns)] on by default
warning: the `tail:` in this pattern is redundant
--> src/main.rs:5:28
|
5 | => { HCons { head: $a, tail: $b }};
| ^^^^ help: remove this
...
11 | let pat!(head, tail) = list;
| ---------------- in this macro invocation
Finished dev [unoptimized + debuginfo] target(s) in 0.50 secs
Running `target/debug/playground`
It does not appear that I can do anything inside the macro to prevent this. Patterns cannot contain attributes, and I can't think of any sort of trick that would let me bind these to some temporary names first (it's a pattern macro!). Any ideas?