Rules / Suspicious / no-equal-then-else

no-equal-then-else

lint/suspicious/no-equal-then-else
recommended
Same as no-equal-then-else · Dart Code Metrics

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 and block comments normalized away, so formatting differences do not hide a match. 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);
  }
}
Both branches of ternary are identical — remove the condition
4void testTernaryWithSameValue() {
5 final x = condition ? value : value;
Both branches of ternary are identical — remove the condition
5 final x = condition ? value : value;
6 final y = condition ? "text" : "text";
Both branches of ternary are identical — remove the condition
6 final y = condition ? "text" : "text";
7 final z = condition ? 42 : 42;
Both branches of if/else are identical — remove the condition
11 if (condition) {
12 return x;
Both branches of if/else are identical — remove the condition
19 if (a) {
20 print("value");
Both branches of if/else are identical — remove the condition
27 if (active) {
28 return "ready";
Both branches of if/else are identical — remove the condition
36 if (shouldExecute) {
37 callback();
Both branches of ternary are identical — remove the condition
44int calculate(bool flag) {
45 return flag ? 100 : 100;
Both branches of ternary are identical — remove the condition
48void testComplexExpression() {
49 final result = condition ? (a + b) : (a + b);
Both branches of if/else are identical — remove the condition
53 if (test) {
54 return true;
Both branches of ternary are identical — remove the condition
61 final x = "same";
62 final result = condition ? x : x;
Both branches of if/else are identical — remove the condition
67 final temp = calculate();
68 process(temp);

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;
}

How to configure

Set the severity of no-equal-then-else in your falcon.json:

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