Skip to main content

SKY-Q305 · Quality, structure and performance

Duplicate branch logic

Medium

The catalog gives no severity for this rule; every finding is MEDIUM.

An if/elif chain where two branches test the same condition, or two branches run the same block of code. The second copy either can never run or does the same thing under a different name.

Languages
Python, TypeScript, JavaScript, Rust
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
review
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

It is usually a copy-paste slip. In the example below heavy parcels get express labels and priority because the elif body was copied from the branch above and never changed, so the business pays express rates for every heavy order.

What Skylos flags

shipping/labels.py · PythonReported on line 10
"""Shipping labels.""" from shipping.printer import make_label  def shipping_label(order):    if order.express:        label = make_label(order, "express")        label.priority = 1    elif order.weight > 20:        label = make_label(order, "express")        label.priority = 1    else:        label = make_label(order, "standard")    return label

What Skylos leaves alone

shipping/labels.py · Python
"""Shipping rates."""  def shipping_rate(order):    if order.country == "US":        return 5    elif order.country == "CA":        return 5    else:        return 15

Why Skylos leaves it alone

Two branches return the same value, but each body is a single statement. A table of values written as branches often repeats a value on purpose, so Python bodies need at least two real statements before Skylos compares them.

More languages

TypeScript

Flagged

src/grades.ts · TypeScriptReported on line 6
export function grade(score: number): string {  if (score >= 90) {    return "A";  } else if (score >= 80) {    return "B";  } else if (score >= 90) {    return "A+";  }  return "C";}

Not flagged

src/grades.ts · TypeScript
export function grade(score: number): string {  if (score >= 97) {    return "A+";  } else if (score >= 90) {    return "A";  } else if (score >= 80) {    return "B";  }  return "C";}

Why Skylos leaves it alone

Every condition in the chain is different. In the flagged version the third test repeats the first, so "A+" can never be returned.

Rust

Flagged

src/lib.rs · RustReported on line 5
pub fn shipping_fee(express: bool, heavy: bool, base: u32) -> u32 {    if express {        let fee = base * 2;        fee + 10    } else if heavy {        let fee = base * 2;        fee + 10    } else {        base    }}
Cargo.toml · TOML
[package]name = "shop"version = "0.1.0"edition = "2021"

Not flagged

src/lib.rs · Rust
pub fn shipping_fee(express: bool, heavy: bool, base: u32) -> u32 {    if express {        let fee = base * 2;        fee + 10    } else if heavy {        let fee = base + base / 2;        fee + 5    } else {        base    }}

Cargo.toml: same as in the flagged example.

Why Skylos leaves it alone

The two blocks look alike but compute different fees, so there is no copy to merge.

How Skylos decides

  1. Runs with --quality. In Python it looks at each if/elif/else chain inside a function and compares the conditions and the branch bodies by their syntax tree, so formatting and comments do not matter.
  2. A Python condition that repeats an earlier one in the chain is reported. A body is compared only when it has at least two statements other than pass, ... and strings; a body equal to an earlier one, including the else body, is reported.
  3. In TypeScript and JavaScript it compares the text of the conditions in an if/else if chain and reports a repeated condition; bodies are not compared.
  4. In Rust it compares the blocks of an if/else if/else chain only when every condition is a variable known to be a bool (such as a bool parameter), a true/false literal or its ! negation, so a chain of comparisons is not checked. Comments are ignored; blocks with macros or attributes, or with fewer than two statements, are skipped.
  5. Reports MEDIUM on the repeated branch: its elif, condition or else line (in Python, the first statement of a repeated else body).

How to fix it

Merge the branches when they really should do the same thing, or change the copied body to what that case needs.

shipping/labels.py · Python · the fix, against the flagged file
"""Shipping labels.""" from shipping.printer import make_label  def shipping_label(order):    if order.express:        label = make_label(order, "express")        label.priority = 1    elif order.weight > 20:Removed:         label = make_label(order, "express")Removed:         label.priority = 1Added:         label = make_label(order, "freight")    else:        label = make_label(order, "standard")    return label

When it can be wrong

  • Two branches that are deliberately kept separate because they are expected to diverge soon are reported.
  • In TypeScript, conditions are compared as text, so a > b and b < a are not seen as the same, and x == 1 and x == 1 differ.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Put the marker on the reported line.

    Python
    elif order.weight > 20:  # skylos: ignore[SKY-Q305] split planned in #88
  • [tool.skylos] ignore = ["SKY-Q305"] turns the rule off for the project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.