enum_constants_ordering
This rule flags an enum whose constants are not in the configured order.
An enum is a list a reader scans rather than reads, and finding a constant in an unordered list of twenty means checking all twenty. Ordering also makes an addition show up in a diff as one line in the middle rather than one appended at the end, which is where duplicates hide.
Outside preset: pedantic, this rule reports nothing until configured, because the useful order for an enum is often semantic rather than alphabetical — small, medium, large is correctly ordered and alphabetising it would be a regression. The pedantic preset deliberately chooses alphabetical order for mechanical consistency.
Only the first out-of-order constant is reported: one misplaced name makes every later name look wrong too, and reporting them all would turn one edit into a wall of diagnostics.
This rule is in the pedantic preset.
enum Fruit { banana, apple, cherry }enum Fruit { apple, banana, cherry }
// Still correct with no `order:` configured — a semantic order is// exactly why less strict presets leave this rule disabled.enum Size { small, medium, large }Enabling this rule
Section titled “Enabling this rule”Outside preset: pedantic, enabling it by name is not enough — set order::
rules: enum_constants_ordering: order: alphabeticalOptions
Section titled “Options”many_lints: rules: enum_constants_ordering: order: alphabeticalrules: enum_constants_ordering: order: alphabetical| Option | Type | Default | Description |
|---|---|---|---|
order |
string | — |
alphabetical, alphabetical_case_sensitive or by_length. Unset means the rule is silent |
Turning this rule off
Section titled “Turning this rule off”To disable this rule:
rules: enum_constants_ordering: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”pattern_fields_ordering— Keep pattern fields in a configured order.record_fields_ordering— Keep record named fields in a configured order.avoid_missing_enum_constant_in_map— Cover every enum constant in a map keyed by that enum.prefer_enums_by_name— Use .byName() instead of .firstWhere() to look up enum values by name.