Skip to content

avoid_nested_conditional_expressions

v1.0.0WarningConfigurableControl Flow

This rule flags a conditional expression nested inside another one.

This rule is in the opinionated preset.

a ? b : (c ? d : e) packs a decision tree onto one line, and the reader has to track which ? each : belongs to. A switch expression lines the branches up, and an early return removes the nesting entirely.

The outermost conditional carries the diagnostic, so one nested chain produces one report rather than one per level.

String label(int score) =>
score > 100 ? 'high' : (score > 50 ? 'medium' : 'low');
String label(int score) => switch (score) {
> 100 => 'high',
> 50 => 'medium',
_ => 'low',
};
analysis_options.yaml
many_lints:
rules:
avoid_nested_conditional_expressions:
max_depth: 2
Option Type Default Description
max_depth int 1 Maximum allowed conditional-expression nesting depth

To disable this rule:

many_lints.yaml
rules:
avoid_nested_conditional_expressions: false

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