Skip to content

enum_constants_ordering

v1.0.0WarningConfigurableCode Organization

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 }

Outside preset: pedantic, enabling it by name is not enough — set order::

many_lints.yaml
rules:
enum_constants_ordering:
order: alphabetical
analysis_options.yaml
many_lints:
rules:
enum_constants_ordering:
order: alphabetical
Option Type Default Description
order string alphabetical, alphabetical_case_sensitive or by_length. Unset means the rule is silent

To disable this rule:

many_lints.yaml
rules:
enum_constants_ordering: false

To keep the rule on but skip certain paths, use per-rule exclude.