Skip to main content

avoid-unnecessary-if

added in: 1.9.0
🛠
Pro+
preset: recommended

Warns when a return statement inside an if block is equal to the return statement after it.

Example

❌ Bad:

final value = 1;

int function() {
if (value == 2) { // LINT: Return statement inside this 'if' block is equal to the return statement after this 'if' block. Try removing this block or checking the return statement for a potential mistake.
return 1;
}

return 1;
}

✅ Good:

final value = 1;

int function() {
if (value == 2) {
return 2; // Correct, different return value
}

return 1;
}