#[rustfmt::skip] issue

There are situations where it is important to me that the "Format Document" command in VSCode skips certain code areas. I can also do this with the command: #[rustfmt::skip]
But when I use it before the match command I always get this ugly warning.

Can this be deactivated?

image

That's not a warning, that's an error. You can turn off errors in your editor but the file still won't compile.

I don't have a solution for this.

Well… the following seems to work as a hack :sweat_smile:

enum Obst {
    Apfel,
    Birne,
    Pflaume,
}

fn main() {
    use Obst::*;

    #[cfg_attr(any(), rustfmt::skip)]
    match Apfel {
        Apfel | Birne => println!("'n Apfel oder so"),
        Pflaume       => println!("was kleines Buntes"),
    }
}

Is looks like rustfmt doesn’t evaluate cft_attr logic, but unconditionally skip the expression marked this way, whereas cfg_attr with any() (evaluating to false) evaluates to no attribute at all before the compiler has a change to error about the custom attribute on an expression. (Whereas attributes in general are allowed on statement expressions like blocks or this match, so the cfg_attr itself creates no issue.)

Subtle stuff… no idea by the way if this “hack” will keep working indefinitely.

5 Likes

Vielen Dank du coole Socke!! :slight_smile:

I wonder if there is some parsing ambiguity between ExpressionWithBlock and ExpressionStatement when attributes are attached. Adding an explicit semicolon to the end of the match statement makes the error go away: Rust Playground

The reference specifically says outer attributes can be applied to expression statements and the playground link above seems to agree with that: Expressions - The Rust Reference

5 Likes

That’s a neat workaround. I’d call that cleaner and more future-proof compared to the approach I could come up with.

2 Likes