SKY-Q305 · Quality, structure and performance
Duplicate branch logic
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
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.""" 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 labelWhat Skylos leaves alone
"""Shipping rates.""" def shipping_rate(order): if order.country == "US": return 5 elif order.country == "CA": return 5 else: return 15Why 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
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
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
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 }}[package]name = "shop"version = "0.1.0"edition = "2021"Not flagged
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 }}Why Skylos leaves it alone
The two blocks look alike but compute different fees, so there is no copy to merge.
How Skylos decides
- Runs with
--quality. In Python it looks at eachif/elif/elsechain inside a function and compares the conditions and the branch bodies by their syntax tree, so formatting and comments do not matter. - 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 theelsebody, is reported. - In TypeScript and JavaScript it compares the text of the conditions in an
if/else ifchain and reports a repeated condition; bodies are not compared. - In Rust it compares the blocks of an
if/else if/elsechain only when every condition is a variable known to be abool(such as aboolparameter), atrue/falseliteral 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. - Reports MEDIUM on the repeated branch: its
elif, condition orelseline (in Python, the first statement of a repeatedelsebody).
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.""" 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 labelWhen 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 > bandb < aare not seen as the same, andx == 1andx == 1differ.
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.