Skip to content

Inconsistent indentation of #[rustfmt::skip] #4699

Closed
@Maxidejf

Description

@Maxidejf

I ended up using a lot of #[rustfmt::skip] attributes in my code to preserve our coding standards until rust fmt (hopefully) evolves its output config options enough so that they can at some point be deleted.
However, Rust fmt does not always indent the skip attributes to where I'd expect.

This is a (somewhat) minimal example of what I mean:

Original Input

pub enum CalibrationType {
    AlphaBeta,
    BinShift,
    Enum,
    None,
}

impl CalibrationType {
    #[rustfmt::skip] // In this case aligning the match branches makes for more readable code.
    pub fn parse(value: &str) -> Option<Self> {
        match value {
            "alpha-beta" => Some(Self::AlphaBeta),
            "bin-shift"  => Some(Self::BinShift),
            "enum"       => Some(Self::Enum),
            "none"       => Some(Self::None),
            _            => None
        }
    }
    
    #[rustfmt::skip] // In this case aligning the match branches makes for more readable code.
    pub fn to_str(&self) -> &'static str {
        match self {
            Self::AlphaBeta => "alpha-beta",
            Self::BinShift  => "bin-shift",
            Self::Enum      => "enum",
            Self::None      => "none"
        }
    }
}

Suggested Output
While checking what the changes would be via cargo +nightly fmt -- --check the suggested output looks like this (second skip attribute loses its indent):

pub enum CalibrationType {
    AlphaBeta,
    BinShift,
    Enum,
    None,
}

impl CalibrationType {
    #[rustfmt::skip] // In this case aligning the match branches makes for more readable code.
    pub fn parse(value: &str) -> Option<Self> {
        match value {
            "alpha-beta" => Some(Self::AlphaBeta),
            "bin-shift"  => Some(Self::BinShift),
            "enum"       => Some(Self::Enum),
            "none"       => Some(Self::None),
            _            => None
        }
    }
    
#[rustfmt::skip] // In this case aligning the match branches makes for more readable code.
    pub fn to_str(&self) -> &'static str {
        match self {
            Self::AlphaBeta => "alpha-beta",
            Self::BinShift  => "bin-shift",
            Self::Enum      => "enum",
            Self::None      => "none"
        }
    }
}

It seems that unless the skip attribute is on the first line within the impl block, the indentation is lost. The indentation seems to work fine within the functions.

Meta

  • rustfmt version: rustfmt 1.4.20-stable (48f6c32e 2020-08-09)
  • From where did you install rustfmt?: rustup component add rustfmt --toolchain nightly

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions