avoid_nested_conditional_expressions
v1.0.0WarningConfigurableControl Flow
analysis_options.yaml many_lints.yaml
many_lints.yaml
This rule flags a conditional expression nested inside another one.
This rule is in the opinionated preset.
Why use this rule
Section titled “Why use this rule”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',};Options
Section titled “Options”many_lints: rules: avoid_nested_conditional_expressions: max_depth: 2rules: avoid_nested_conditional_expressions: max_depth: 2| Option | Type | Default | Description |
|---|---|---|---|
max_depth |
int | 1 |
Maximum allowed conditional-expression nesting depth |
Turning this rule off
Section titled “Turning this rule off”To disable this rule:
rules: avoid_nested_conditional_expressions: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_conditional_expressions— Collapse a two-way if/else into a conditional expression.prefer_switch_expression— Suggest converting switch statements to switch expressions.prefer_switch_with_enums— Use a switch instead of an if-else chain over enum constants.avoid_contradictory_expressions— Detect logical AND conditions that always evaluate to false.