rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

matches! macro with ranges prevent formating inside other macro

Open freerig opened this issue 2 months ago • 2 comments

See in this playground, there's a comment explaining the bug. I think this bug is a bit too specific, so maybe there's a bigger one underneath.

freerig avatar Oct 25 '25 15:10 freerig

Reproducing snippet for locality:

// The only difference between the two macros here is that one uses
// `matches!('a', 'a'..='z' | '0'..='9')` while the other use
// `matches!('a', 'a'..='z' | '0')`, without the second range. The problem is
// that the first isn't formated by rustfmt.
// You can test it yourself by using the Rustfmt tool, top right
//
// (Note that in this specific example, I could have used
// `matches!('a', 'a'..='z') || matches!('a', '0'..='9')`, which *is* formated,
// but that's not the point)

macro_rules! this_macro_will_not_be_formated {
    () => {

let _      =
"some ugly not formated code"
;

let _ = matches!('a', 'a'..='z' | '0'..='9');

    };
}

macro_rules! this_macro_will_be_formated {
    () => {

let _      =
"some ugly not formated code"
;

let _ = matches!('a', 'a'..='z' | '0');

    };
}

jieyouxu avatar Oct 30 '25 15:10 jieyouxu

I checked and this would be resolved by https://github.com/rust-lang/rustfmt/pull/5554

ytmimi avatar Nov 20 '25 19:11 ytmimi