rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

`struct_field_align_threshold = 30` doesn't apply when using `use_field_init_shorthand = true`

Open saying121 opened this issue 1 year ago • 1 comments
trafficstars

Formatting incorrectly

I have this code

pub struct Test {
    one:         i32,
    two:         i32,
    three_three: i32,
    four:        i32,
}

impl Test {
    pub fn new(one: i32, bbb: i32, ccccc: i32, ddddd: i32) -> Self {
        Self {
            one:         one,
            two:         bbb,
            three_three: ccccc,
            four:        ddddd,
        }
    }
}

Executing formatting

pub struct Test {
    one:         i32,
    two:         i32,
    three_three: i32,
    four:        i32,
}

impl Test {
    pub fn new(one: i32, bbb: i32, ccccc: i32, ddddd: i32) -> Self {
        Self {
            one,
            two:         bbb,
            three_three: ccccc,
            four:        ddddd,
        }
    }
}

Formatting again

pub struct Test {
    one:         i32,
    two:         i32,
    three_three: i32,
    four:        i32,
}

impl Test {
    pub fn new(one: i32, bbb: i32, ccccc: i32, ddddd: i32) -> Self {
        Self {
            one,
            two: bbb,
            three_three: ccccc,
            four: ddddd,
        }
    }
}

When use_field_init_shorthand is in effect, the struct_field_align_threshold behaves incorrectly.

Version

rustfmt 1.7.0-nightly (0ecbd06 2024-02-25)

Rustfmt Config

struct_field_align_threshold        = 30
use_field_init_shorthand            = true

saying121 avatar Feb 26 '24 13:02 saying121

Thanks for the report!

This is definitely a bug since it takes rustfmt two runs before it settles on stable formatting. Seems like a strange interaction with these two options. Also, linking the tracking issue for struct_field_align_threshold #3371

ytmimi avatar Feb 26 '24 14:02 ytmimi