Every branch of an if-else or conditional expression is the same
What it finds
if (c) { f(); } else { f(); }: when every branch of an if does the same, the condition decides nothing. Usually one branch was copied and not changed. The same goes for a conditional expression whose two outcomes are the same, as c ? x : x.
A chain of else if branches is reported only when it ends in a plain else and every branch is the same: giving each case its own branch even where two do the same is a common way to list the cases. Empty blocks are left alone, and so are switches whose cases all do the same, a common placeholder for cases still to come.
Some branches are the same on purpose. In TypeScript the same code can mean different things where a condition narrows a type, as typeof x === "string", x instanceof T, "key" in x, a comparison with a string or boolean literal, or a call of a type guard named like isArray, so conditions with any of these are left alone. So are conditions that are a build flag such as __DEV__ or DEBUG, whose branches are kept apart for builds that change them, and conditional expressions whose outcomes are literals, which are often placeholders kept for symmetry, as in translations.
From the rule's source, identical_branches.rs.
function f(ok: boolean, n: number, items: number[]) { if (ok) {tx0 reports: both branches of this if do the same n = g(n); } else { n = g(n); } if (n > 1) {tx0 reports: every branch of this if chain does the same items.push(n); } else if (n > 0) { items.push(n); } else { items.push(n); } if (ok) n++; else n++;tx0 reports: both branches if (n > 1) { items.push(n); } else if (n > 0) { items.push(n); } if (n > 1) { items.push(n); } else if (n > 0) { items.push(n); } else { items.push(1); } if (ok) { } else { } const a = ok ? items[n] : items[n];tx0 reports: both outcomes of this conditional expression are items[n] const b = ok ? this.n : this.n;tx0 reports: both outcomes const c = ok ? n : n + 1; const d = n !== 1 ? "" : ""; switch (n) { case 1: n = g(n); break; default: n = g(n); break; } return [a, b, c, d];} function narrowed(value: string | string[], kind: { type: "a" | "b" }, flag: string | true) { if (typeof value === "string") { use(value); } else { use(value); } if (Array.isArray(value)) { use(value); } else { use(value); } if (value instanceof Array) { use(value); } else { use(value); } if (kind.type === "a") { use(kind); } else { use(kind); } if (flag === true) { use(flag); } else { use(flag); } if ("length" in kind) { use(kind); } else { use(kind); } if (__DEV__) { use(kind); } else { use(kind); } return typeof value === "string" ? use(value) : use(value);} declare const __DEV__: boolean;declare function use(value: unknown): void;declare function g(n: number): number; Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/ts:identical-branches" = "off"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/ts:identical-branches] reason="..."tx0 explain tx0/ts:identical-branches shows the same in the terminal. Found a false positive? Report it.