Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

sorted-variants

Level: warn · Fixed by rabot fmt · Article: Sorting

One rule. Zero documentation required. Culturally neutral.

What it checks

Enum variants are in alphabetical order. Enums whose variant order is semantic are skipped without a diagnostic: #[repr(..)] enums, enums with explicit discriminants (A = 1), and enums deriving PartialOrd or Ord, where declaration order is the comparison order.

Don’t

#![allow(unused)]
fn main() {
enum UserRole {
    Member,
    Admin,
    Guest { invited_by: UserId, since: DateTime },
}
}

Do

#![allow(unused)]
fn main() {
enum UserRole {
    Admin,
    Guest { invited_by: UserId, since: DateTime },
    Member,
}
}

Fields of a struct-like variant are sorted too (that is sorted-fields).

Silence it

An enum whose order carries meaning usually says so already: derive PartialOrd and rabot steps aside. When the meaning is not a derive, write it down:

#![allow(unused)]
fn main() {
// rabot: allow(sorted-variants) matches the on-wire protocol numbering
enum Opcode { Connect, Publish, Subscribe, Disconnect }
}