Skip to main content

prefer-correct-switch-length

added in: 1.3.0
⚙️
Pro+

Warns when a switch has too few or too many cases.

⚙️ Config

Set min-length (default is 3) to configure the minimum number of cases.

Set max-length (default is 10) to configure the maximum number of cases after which the rule should trigger.

Set ignore-fallthrough-cases (default is false) to ignore fallthrough cases (with no body) (example).

analysis_options.yaml
dart_code_metrics:
rules:
- prefer-correct-switch-length:
min-length: 3
max-length: 10
ignore-fallthrough-cases: false

Example

❌ Bad:

// LINT: This switch has 1 case which is below the configured minimum of 3.
switch (value) {
case '1':
{
print('hello world');
break;
}
}

✅ Good:

switch (value) {
case '1':
{
...
}

case '2':
{
...
}

case '3':
{
...
}
}

Example with "ignore-fallthrough-cases"

Config
analysis_options.yaml
dart_code_metrics:
rules:
- prefer-correct-switch-length:
ignore-fallthrough-cases: true

❌ Bad:

// LINT: This switch has 1 case which is below the configured minimum of 3.
switch (value) {
case '1':
case '2':
case '3':
case '4':
{
print('hello world');
break;
}
}