Skip to main content

avoid-nested-switches

added in: 1.1.0

Warns when a switch case body has another switch statement.

Nested switches are difficult to understand because you can easily confuse the cases of an inner switch as belonging to an outer statement.

Example

❌ Bad:

void func() {
final str = 'someString';

switch (str) {
case 'string':
{
// LINT
switch (str) {
case 'another-one':
{
break;
}
}

break;
}

case 'anotherString':
{
break;
}
}
}

✅ Good:

void func() {
final str = 'someString';

switch (str) {
case 'string':
{
...

break;
}

case 'anotherString':
{
break;
}
}
}