rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

Odd formatting of empty `where` clause

Open joshlf opened this issue 9 months ago • 1 comments

The following is rustfmt's formatting on 2025-02-17 4d91de4e48 (the version currently on the Rust Playground):

impl<'a, T, I> Ptr<'a, T, I>
where
    T: 'a + TransparentWrapper<I, UnsafeCellVariance = Covariant> + ?Sized,
    I: Invariants,
{
    pub(crate) fn transparent_wrapper_into_inner(
        self,
    ) -> Ptr<
        'a,
        T::Inner,
        (
            I::Aliasing,
            <T::AlignmentVariance as AlignmentVariance<I::Alignment>>::Applied,
            <T::ValidityVariance as ValidityVariance<I::Validity>>::Applied,
        ),
    >
where {
    }
}

The where clause should probably be indented by four spaces.

Also note that, when wrapped in a module, the where clause is still not indented at all:

mod foo {
    impl<'a, T, I> Ptr<'a, T, I>
    where
        T: 'a + TransparentWrapper<I, UnsafeCellVariance = Covariant> + ?Sized,
        I: Invariants,
    {
        pub(crate) fn transparent_wrapper_into_inner(
            self,
        ) -> Ptr<
            'a,
            T::Inner,
            (
                I::Aliasing,
                <T::AlignmentVariance as AlignmentVariance<I::Alignment>>::Applied,
                <T::ValidityVariance as ValidityVariance<I::Validity>>::Applied,
            ),
        >
where {
        }
    }
}

joshlf avatar Feb 25 '25 21:02 joshlf

Smaller example:

struct MyStruct;
impl MyStruct {
    pub const unsafe fn wow_this_is_so_long_i_might_have_to_do_some_line_wrapping(x: u8) -> u8
where {
        x
    }
}

Rustfmt currently accepts this. It appears that it simply doesn't move the where clause at all if it's empty, since this also is unchanged by rustfmt:

struct MyStruct;
impl MyStruct {
    pub const unsafe fn wow_this_is_so_long_i_might_have_to_do_some_line_wrapping(x: u8) -> u8 where
    {
        x
    }
}

clarfonthey avatar Jun 17 '25 03:06 clarfonthey

Think this might be a duplicate of https://github.com/rust-lang/rustfmt/issues/5407

ytmimi avatar Jun 21 '25 07:06 ytmimi