Rustfmt setting to avoid moving comment

I have something like this:

pub enum SomeEnum {
    // Comment 1
    SomeThing(String)
    // Comment 2
}

When I run cargo +nightly fmt it moves that last comment to the previous line:

pub enum SomeEnum {
    // Comment 1
    SomeThing(String) // Comment 2
}

Is there a setting I can change so it doesn't do that?

This seems like a bug. I would recommend checking for existing issues on the rustfmt repo, and if there isn't one already, open one.

In the meantime, you can put #[rustfmt::skip] on the enum itself.

If you try it with the playground, that's not actually what it does (see the comma):

pub enum SomeEnum {
    // Comment 1
    SomeThing(String), // Comment 2
}

And then if you put the comment on the newline from this state, it stays.

pub enum SomeEnum {
    // Comment 1
    SomeThing(String),
    // Comment 2
}

I still think it shouldn't move your comment, but this keeps you from needing rustfmt::skip.

3 Likes