no-equal-then-else

Same as no-equal-then-else in Dart Code Metrics

Path
lint/suspicious/no-equal-then-else
Default
On (recommended)
Origin
Dart Code Metrics
Rule
286 of 512
recommended

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

example.dartdart
// 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]);
}
Remove this ternary condition because both branches are identical.
4void testTernaryWithSameValue() {
5 final x = condition ? value : value;
Remove this ternary condition because both branches are identical.
5 final x = condition ? value : value;
6 final y = condition ? "text" : "text";
Remove this ternary condition because both branches are identical.
6 final y = condition ? "text" : "text";
7 final z = condition ? 42 : 42;
Remove this `if`/`else` condition because both branches are identical.
11 if (condition) {
12 return x;
Remove this `if`/`else` condition because both branches are identical.
19 if (a) {
20 print("value");
Remove this `if`/`else` condition because both branches are identical.
27 if (active) {
28 return "ready";
Remove this `if`/`else` condition because both branches are identical.
36 if (shouldExecute) {
37 callback();
Remove this ternary condition because both branches are identical.
44int calculate(bool flag) {
45 return flag ? 100 : 100;
Remove this ternary condition because both branches are identical.
48void testComplexExpression() {
49 final result = condition ? (a + b) : (a + b);
Remove this `if`/`else` condition because both branches are identical.
53 if (test) {
54 return true;
Remove this ternary condition because both branches are identical.
61 final x = "same";
62 final result = condition ? x : x;
Remove this `if`/`else` condition because both branches are identical.
67 final temp = calculate();
68 process(temp);
Remove this ternary condition because both branches are identical.
78void containersRegression(int rcount) {
79 final (ra, _) = ((rcount > 0 ? 1 : 1), 0);
Remove this ternary condition because both branches are identical.
80 lbl: {
81 final rb = (rcount > 0 ? 1 : 1);
Remove this ternary condition because both branches are identical.
84 final rc = switch (rcount) {
85 0 => (rcount > 0 ? 1 : 1),
Remove this ternary condition because both branches are identical.
87 };
88 final rd = switch ((rcount > 0 ? 1 : 1)) {
Remove this ternary condition because both branches are identical.
90 };
91 final re = [if (rcount > 0) (rcount > 0 ? 1 : 1)];
Remove this ternary condition because both branches are identical.
91 final re = [if (rcount > 0) (rcount > 0 ? 1 : 1)];
92 final rf = [...[(rcount > 0 ? 1 : 1)]];
Remove this ternary condition because both branches are identical.
92 final rf = [...[(rcount > 0 ? 1 : 1)]];
93 final rg = (p: (rcount > 0 ? 1 : 1), q: 0);

Valid

example.dartdart
// 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.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "no-equal-then-else": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/suspicious/no-equal-then-else: <reason>
  • no-magic-string

    Extract inline string literals into named variables or constants.

  • no-self-comparisonsno_self_comparisonsrecommended

    Flags proven self-comparisons of side-effect-free scalar values.

  • no-wildcard-variable-usesno_wildcard_variable_usesrecommended

    Flags references to a wildcard variable or parameter (a name made solely of underscores, such as _ or __).

  • overridden-fieldsoverridden_fieldsrecommended

    Flags instance fields that redeclare an inherited field.

  • prefer-unique-test-namestest

    Reports duplicate test descriptions within a group and constant names in loops.

  • recursive-gettersrecursive_gettersrecommended

    Flags a getter that unconditionally returns itself.

  • unnecessary-null-aware-assignmentsunnecessary_null_aware_assignmentsrecommended

    Flags x ??= null, which can never have an effect.

  • unnecessary-null-in-if-null-operatorsunnecessary_null_in_if_null_operatorsrecommended

    Flags x ?? null and null ?? x, where the null operand is redundant.