Skip to content

destructure Self in generated Debug impls. #22597

@programmerjake

Description

@programmerjake

Generally when I write a manual Debug implementation, I almost always destructure self so I know to update the implementation when I add/remove/rename fields. I think when rust-analyzer generates a manual Debug implementation, it should always do that or at least have an option to make it always do that.

This came up on Zulip here.

So, to be clear, I don't want it to destructure self in the function argument if that becomes a feature, instead it should generate code like:

struct MyStruct<T: ?Sized> {
    field1: Foo,
    field2: Bar,
    field3: Baz,
    field4: T,
}

impl<T: ?Sized + fmt::Debug> fmt::Debug for MyStruct<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Self {
            field1,
            field2,
            field3,
            field4,
        } = self;
        f.debug_struct("MyStruct")
            .field("field1", field1)
            .field("field2", field2)
            .field("field3", field3)
            .field("field4", &field4) // reference needed since T: ?Sized
            .finish()
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-featureCategory: feature request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions