rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

Feature request: alphabetic sorting of struct/enum fields

Open PvdBerg1998 opened this issue 6 years ago • 18 comments

Whenever the order of a struct or enum would be completely irrelevant, I like to keep them in alphabetic order. However, this gets slightly annoying to maintain when adding new fields. Therefore I propose an optional automatic sorting attribute, think #[rustfmt::sort].

My first idea was to create a clippy lint for this, but having this done automatically would be the best.

PvdBerg1998 avatar Feb 27 '19 15:02 PvdBerg1998

On a related note, sorting the arms of a match based on the order in the enum declaration (alphabetical or not), would be quite handy.

mwillsey avatar Mar 27 '19 23:03 mwillsey

@mwillsey you mean to reflect the enum's order on the match ? I don't think that is possible since we have only information coming from the AST

scampi avatar Mar 28 '19 09:03 scampi

In addition to being able to sort fields, it would also be nice to be able to sort top-level items (Structs, Traits, etc.) in a file.

jeremyBanks avatar Apr 24 '19 16:04 jeremyBanks

This would be nice!!

bsodmike avatar Jan 30 '22 17:01 bsodmike

I would also like to see this for methods of a trait.

jendrikw avatar Jul 18 '22 10:07 jendrikw

I hope this will be added someday. It will save the minutes spent sorting them manually.

realsama avatar Jan 11 '23 18:01 realsama

:+1: for opt-in to apply this to all structs, enums, unions, and simple match statements on enums, except repr(C) and friends, enum Enum { Variant = # }, or #[rustfmt(skip)].

finnbear avatar Sep 18 '23 04:09 finnbear

Another +1 here, I already naturally try to sort stuff alphabetically, so this would be a time saver and ensure consistency. I also +1 having it optionally available for top-level items as @jeremyBanks suggested.

WinkelCode avatar Jan 04 '24 01:01 WinkelCode

fwiw jetbrains ides have a sort lines command (select fields, maybe with opt+up arrow idk for windows, double press shift, type sort lines, hit enter or assign a keybind to sort lines) not ideal but better than manual sorting

laralove143 avatar Jul 25 '24 13:07 laralove143

+1, I need this as I'm working with massive structs to accept an API's response!

TYTheBeast avatar Oct 02 '24 12:10 TYTheBeast

I've asked for this 3-years ago. Oh my... 🤦‍♂️🤦‍♂️🤦‍♂️

bsodmike avatar Oct 02 '24 13:10 bsodmike

The complexity is in, ‘Whenever the order of a struct or enum would be completely irrelevant’ ...

sanmai-NL avatar Oct 02 '24 13:10 sanmai-NL

@sanmai-NL I don't think this is a real concern, as the OP suggests, this can be done via an opt-in attribute (#[rustfmt::sort]), in which case a user decides if the order matters or not.

WaffleLapkin avatar Oct 17 '24 23:10 WaffleLapkin

You presuppose users would want to invest time in specifying such an attribute. And what about repr(C) structs?

sanmai-NL avatar Oct 18 '24 07:10 sanmai-NL

You presuppose users would want to invest time in specifying such an attribute. And what about repr(C) structs?

Is it possible to have the opt-in attribute (#[rustfmt::sort]) for normal record structs only?

bsodmike avatar Oct 18 '24 07:10 bsodmike

You presuppose users would want to invest time in specifying such an attribute.

Yes. I know I would use it in a few places. It would be too annoying as an opt-out (rustfmt shouldn't change semantics by default), but it seems just right as an opt-in.

And what about repr(C) structs?

What about them? If you added the #[rustfmt::sort] attribute to a repr(C) ADT, then you are responsible to making sure that the sorted order of fields/variants doesn't break any assumptions.

WaffleLapkin avatar Oct 18 '24 13:10 WaffleLapkin

@WaffleLapkin not sure what design you intended, but fields should be sorted by default, and this will generally break repr(C) items. Opting out of it shouldn't require an extra attribute.

sanmai-NL avatar Oct 18 '24 14:10 sanmai-NL

why not implement this as an opt-in rule and also a tag (since rustfmt doesnt support rules being enabled per scope like in clippy yet)? that way people can opt-in to the rule and opt-out for scopes they dont want with #[rustfmt::skip], or opt-in only for scopes they want with #[rustfmt::sorted]

for some libraries, such as api wrappers, not having an option to enable the feature globally would require us to add #[rustfmt::sorted] to nearly every struct

laralove143 avatar Oct 18 '24 15:10 laralove143