rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

Rustfmt can generate invalid code when using `space_after_colon=false`

Open lever1209 opened this issue 11 months ago • 2 comments

formatting the following code with space_after_colon=false will generate invalid code

struct SomeStruct {
    some_field: ::some_crate::Thing,
}

since it removes the space between the field colon and the :: prefix on some_crate i cant see this as very high priority because i dont know anyone who prefixes things with :: asides from maybe alloc or std, and space_after_colon=false isnt widely used, but this should probably be looked into

lever1209 avatar Feb 11 '25 16:02 lever1209

Thanks for the report! Confirming that I can reproduce this when using space_after_colon=false with rustfmt 1.8.0-nightly (054efddd0f 2025-02-07)

ytmimi avatar Feb 11 '25 20:02 ytmimi

This is an issue anywhere that space_after_colon applies. For example:

Input:

const THING: ::some_crate::SomeType  = ::some_crate::SomeType::default();

fn main() {
    let x: ::some_crate::SomeType = ::some_crate::SomeType::default();
}

Output:

const THING:::some_crate::SomeType = ::some_crate::SomeType::default();

fn main() {
    let x:::some_crate::SomeType = ::some_crate::SomeType::default();
}

ytmimi avatar Feb 11 '25 20:02 ytmimi