Skip to content

no_equal_branches

Reports a choice that decides nothing, because both ways through it are the same. Make each branch do what it was meant to, or drop the choice.

Tier
blocking
Scope
Dart
Fix
none
Case Example
Identical branches of an if with an else, of an if in a collection, or of a conditional expression if (ready) { start(); } else { start(); }, wide ? 80 : 80
A repeated condition in an if/else-if chain, whose branch is never reached if (score > 90) … else if (score > 70) … else if (score > 90) …
A branch that repeats what follows it: the branch leaves, and the code after the if does the same if (ready) return value; return value;
An inner condition that repeats an enclosing one, so it always holds if (verbose) { print('a'); if (verbose) print('b'); }

Two branches are identical when they hold the same tokens in the same order, so layout and comments do not matter. Branches that differ in any token, even braces or quotes, are not reported. An if without an else, or a chain without a last else, is never reported for identical branches.

When both branches are identical, the condition has no effect: either a branch was copied and never changed, and a behaviour is missing, or the difference was removed and the test left behind. A repeated condition is the same defect from the other side: its branch can never run. An AI coding agent makes both when it fills a chain by copying the branch above, and the code compiles while it does not do what its shape says.

Violating
String status(Order order) {
if (order.isPaid) {
return 'Paid';
} else {
return 'Paid';
}
}
double shippingCost(Order order) {
if (order.isExpress) {
return order.weight * 2.5;
} else if (order.isInternational) {
return order.weight * 4.0;
} else if (order.isExpress) {
return order.weight * 6.0;
} else {
return order.weight * 1.2;
}
}
final label = items.length == 1 ? 'items' : 'items';
Correctedby writing what each case was meant to do
String status(Order order) {
if (order.isPaid) {
return 'Paid';
} else {
return 'Open';
}
}
double shippingCost(Order order) {
if (order.isExpress && order.isInternational) {
return order.weight * 6.0;
} else if (order.isExpress) {
return order.weight * 2.5;
} else if (order.isInternational) {
return order.weight * 4.0;
} else {
return order.weight * 1.2;
}
}
final label = items.length == 1 ? 'item' : 'items';

When the two cases really are the same, delete the choice and keep one copy: start(); in place of if (ready) { start(); } else { start(); }.

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.
assay:
rules:
no_equal_branches:
exclude: ["lib/generated/**"]

Write why on the line directly above the suppression:

// Both platforms take the same path until the desktop layout is drawn.
// ignore: assay/no_equal_branches
if (isDesktop) {
showCompactLayout();
} else {
showCompactLayout();
}

The suppression goes above the line the violation starts on: the if keyword for identical branches, the repeated condition for a repeated condition, and the start of a conditional expression.

// ignore_for_file: assay/no_equal_branches silences it in a whole file, and // ignore: no_equal_branches without the prefix works too. See Suppressions.

All cases

Four cases. The last two have a message of their own.

Identical branches.

  • An if with an else whose branches are identical: if (ready) { start(); } else { start(); }. The violation covers the head of the if: if (ready).
  • An if/else-if chain is judged from its end. The if reported is the first one from which every branch, the last else included, is identical. A chain whose branches are all identical is reported once, at its first if; one whose last two branches are identical, at its last if.
  • An if with an else as a collection element: [if (admin) 'staff' else 'staff'].
  • A conditional expression with two identical expressions: wide ? 80 : 80. The violation covers the whole expression.

A repeated condition. An if in an if/else-if chain with the condition of an earlier if of the same chain: if (score > 90) … else if (score > 70) … else if (score > 90) …. Its branch is never reached. The violation covers the repeated condition, and each further repetition is a violation of its own. The conditions are compared by their tokens alone, so a call that can give another answer the second time, such as iterator.moveNext(), is reported too: where the repetition is meant, suppress it with a justification.

A branch that repeats what follows it. An if with no else and no case, standing in a block, whose branch leaves (it ends with return, throw, rethrow, break or continue), and whose statements the statements after the if repeat one by one: if (ready) return value; return value;. Whatever the condition says, the same code runs. The violation covers the head of the if:

  • message: “This choice decides nothing: the branch of the ‘if’ does what the code after it does anyway.”
  • how to fix: “Drop the ‘if’ and keep the code after it, or make the branch do what was meant.”

An inner condition that repeats an enclosing one. An if inside the branch of another if that tests the same condition, so it always holds there: if (verbose) { print('a'); if (verbose) print('b'); }. It is reported when:

  • the outer condition has no case and is repeatable: literals, constants, locals, parameters, final fields and the reading members of the core library (isEmpty, length…), with the core operators;
  • no local or parameter it reads is written anywhere in the branch, or mentioned by a closure or local function of the function that declares it;
  • the inner if is not inside a closure of the branch, which may run later, and not in the outer if’s else.

The violation covers the inner condition, once however many enclosing if statements it repeats:

  • message: “This condition repeats the condition of an enclosing ‘if’, and nothing between changes what it reads, so it always holds here.”
  • how to fix: “Drop the inner test, or test what was meant.”

Two pieces of source are identical when they hold the same tokens in the same order. Whitespace and comments are not tokens, so branches that differ only in layout or comments are identical, and so are two branches that are empty or hold only comments.

Every name and operator must also stand for the same thing in both. A type test gives a variable another type in each branch, and the same call can then be another function:

extension on Object { String describe() => 'object'; }
extension on int { String describe() => 'int'; }
String label(Object value) => value is int ? value.describe() : value.describe();

The two expressions call two different methods, so this is not reported. A member of the object itself is the same in both, since the object decides which declaration runs: value is int ? value.toString() : value.toString() is reported. A variable, parameter or local function that each branch declares for itself is the same when it stands at the same place in both.

Branches that differ only in what the compiler infers are identical: value is int ? [value] : [value] is reported, though it builds a list of another type in each.

  • An if without an else, or a chain without a last else, for its branches: one way through does nothing, so the ways are not the same (but see a branch that repeats what follows it).
  • An inner if whose condition reads a field that is not final, a getter written in the source, or a call, which can change in between.
  • Branches that differ in any token: another literal or operator, braces around one of them, 'start' against "start", 80 against (80).
  • Identical branches that are not the whole end of a chain: a first and a last branch with another between, or two branches followed by a different else.
  • The same condition in two if statements that are not one chain, or in an if inside a branch of the chain.
  • An if whose case declares a variable, compared with the branches after it: if (value case int n) { print(n); } else { print(n); }. Only the first branch sees n.
  • A switch whose cases are identical.