avoid-collapsible-if
Warns when multiple nested if statements can be merged into one.
Nested if statements add extra levels of indentation and can reduce readability.
Example
❌ Bad:
void main () {
int value1 = 1;
int value2 = 2;
if (value1 == 1) {
// LINT: Avoid collapsible 'if' statements. Try merging them into one 'if' statement.
if (value2 == 2) {
return;
}
}
if (value1 == 1 && value2 == 3) {
// LINT: Avoid collapsible 'if' statements. Try merging them into one 'if' statement.
if (value2 == 2) {
return;
}
}
if (value1 == 1 || value2 == 3) {
// LINT: Avoid collapsible 'if' statements. Try merging them into one 'if' statement.
if (value2 == 2) {
return;
}
}
if (value1 == 1 || value2 == 3) {
// LINT: Avoid collapsible 'if' statements. Try merging them into one 'if' statement.
if (value2 == 2 || value1 == 4) {
return;
}
}
}
✅ Good:
void main () {
int value1 = 1;
int value2 = 2;
// Correct, one 'if' statement with compound condition
if (value1 == 1 && value2 == 2) {
return;
}
if (value1 == 1 && value2 == 3 && value2 == 2) {
return;
}
if ((value1 == 1 || value2 == 3) && value2 == 2) {
return;
}
if ((value1 == 1 || value2 == 3) && (value2 == 2 || value1 == 4)) {
return;
}
if (value == 1 && value2 == 2) return;
}