no-equal-then-else
Same as no-equal-then-else in Dart Code Metrics
- Group
- Suspicious
- Path
lint/suspicious/no-equal-then-else- Default
- On (recommended)
- Origin
- Dart Code Metrics
- Rule
- 286 of 512
Flags an if/else or ternary whose two branches are identical.
When the then and else branches have the same body, the condition decides
nothing — the same code runs either way — so either the condition is pointless
or one branch was never edited to differ. Comparison is on branch source text
with whitespace outside strings and comments normalized away, so formatting
differences do not hide a match while literal and comment contents stay intact. Remove the condition and keep the single body, or correct
the branch that was supposed to differ. Applies to both if/else statements
and ?: ternary expressions.
Invalid
// Test cases for no-equal-then-else rule
// All violations are marked inline below.
void testTernaryWithSameValue() {
final x = condition ? value : value;
final y = condition ? "text" : "text";
final z = condition ? 42 : 42;
}
void testIfElseWithSameReturn() {
if (condition) {
return x;
} else {
return x;
}
}
void testIfElseWithSamePrint() {
if (a) {
print("value");
} else {
print("value");
}
}
String getStatus(bool active) {
if (active) {
return "ready";
} else {
return "ready";
}
}
class Widget {
void onEvent(bool shouldExecute) {
if (shouldExecute) {
callback();
} else {
callback();
}
}
}
int calculate(bool flag) {
return flag ? 100 : 100;
}
void testComplexExpression() {
final result = condition ? (a + b) : (a + b);
}
bool checkValue(bool test) {
if (test) {
return true;
} else {
return true;
}
}
void testWithVariables() {
final x = "same";
final result = condition ? x : x;
}
void testMultilineIfElse() {
if (condition) {
final temp = calculate();
process(temp);
} else {
final temp = calculate();
process(temp);
}
}
// Regression: the violation must still be found inside Dart 3 containers
// (pattern declaration, labeled statement, switch expression and subject,
// collection if/spread, record field).
void containersRegression(int rcount) {
final (ra, _) = ((rcount > 0 ? 1 : 1), 0);
lbl: {
final rb = (rcount > 0 ? 1 : 1);
print(rb);
}
final rc = switch (rcount) {
0 => (rcount > 0 ? 1 : 1),
_ => null,
};
final rd = switch ((rcount > 0 ? 1 : 1)) {
_ => 0,
};
final re = [if (rcount > 0) (rcount > 0 ? 1 : 1)];
final rf = [...[(rcount > 0 ? 1 : 1)]];
final rg = (p: (rcount > 0 ? 1 : 1), q: 0);
print([ra, rc, rd, re, rf, rg]);
}void testTernaryWithSameValue() { final x = condition ? value : value; final x = condition ? value : value; final y = condition ? "text" : "text"; final y = condition ? "text" : "text"; final z = condition ? 42 : 42; if (condition) { return x; if (a) { print("value"); if (active) { return "ready"; if (shouldExecute) { callback();int calculate(bool flag) { return flag ? 100 : 100;void testComplexExpression() { final result = condition ? (a + b) : (a + b); if (test) { return true; final x = "same"; final result = condition ? x : x; final temp = calculate(); process(temp);void containersRegression(int rcount) { final (ra, _) = ((rcount > 0 ? 1 : 1), 0); lbl: { final rb = (rcount > 0 ? 1 : 1); final rc = switch (rcount) { 0 => (rcount > 0 ? 1 : 1), }; final rd = switch ((rcount > 0 ? 1 : 1)) { }; final re = [if (rcount > 0) (rcount > 0 ? 1 : 1)]; final re = [if (rcount > 0) (rcount > 0 ? 1 : 1)]; final rf = [...[(rcount > 0 ? 1 : 1)]]; final rf = [...[(rcount > 0 ? 1 : 1)]]; final rg = (p: (rcount > 0 ? 1 : 1), q: 0);Valid
// Good cases for no-equal-then-else rule
// No violations expected
void testTernaryWithDifferentValues() {
final x = condition ? value1 : value2;
final y = condition ? "yes" : "no";
final z = condition ? 42 : 0;
}
void testIfElseWithDifferentReturns() {
if (condition) {
return success;
} else {
return failure;
}
}
void testIfElseWithDifferentPrints() {
if (a) {
print("active");
} else {
print("inactive");
}
}
String getStatus(bool active) {
if (active) {
return "ready";
} else {
return "waiting";
}
}
class Widget {
void onEvent(bool shouldExecute) {
if (shouldExecute) {
onSuccess();
} else {
onFailure();
}
}
}
int calculate(bool flag) {
return flag ? 100 : 50;
}
void testComplexExpression() {
final result = condition ? (a + b) : (c + d);
}
bool checkValue(bool test) {
if (test) {
return true;
} else {
return false;
}
}
void testWithDifferentVariables() {
final x = "first";
final y = "second";
final result = condition ? x : y;
}
void testUnconditionalReturn() {
return value;
}
void testConditionalWithSideEffects() {
if (condition) {
log("path A");
processA();
} else {
log("path B");
processB();
}
}
String handleResult(Result result) {
if (result.isSuccess) {
return result.value;
} else if (result.error != null) {
return result.error!.message;
} else {
return "Unknown error";
}
}
void testNestedIfElse(bool a, bool b) {
if (a) {
if (b) {
print("a and b");
} else {
print("a not b");
}
} else {
print("not a");
}
}
int priority(bool isUrgent) {
return isUrgent ? 1 : 10;
}
void stringsAndComments(bool condition) {
final text = condition ? '/* kept */' : '';
if (condition) {
run(); /* first meaning */
} else {
run(); /* second meaning */
}
}Configure no-equal-then-else
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"suspicious": {
"no-equal-then-else": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/suspicious/no-equal-then-else: <reason>More suspicious rules
- no-magic-string
Extract inline string literals into named variables or constants.
- no-self-comparisons
no_self_comparisonsrecommendedFlags proven self-comparisons of side-effect-free scalar values.
- no-wildcard-variable-uses
no_wildcard_variable_usesrecommendedFlags references to a wildcard variable or parameter (a name made solely of underscores, such as
_or__). - overridden-fields
overridden_fieldsrecommendedFlags instance fields that redeclare an inherited field.
- prefer-unique-test-namestest
Reports duplicate test descriptions within a group and constant names in loops.
- recursive-getters
recursive_gettersrecommendedFlags a getter that unconditionally returns itself.
- unnecessary-null-aware-assignments
unnecessary_null_aware_assignmentsrecommendedFlags
x ??= null, which can never have an effect. - unnecessary-null-in-if-null-operators
unnecessary_null_in_if_null_operatorsrecommendedFlags
x ?? nullandnull ?? x, where thenulloperand is redundant.