Skip to main content

SKY-UC002 · Dead code

Unreachable statement

Medium

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

A statement in a { ... } block that comes after a return, throw, break or continue in the same block, so it can never run. SKY-UC001 is the Python rule; this one covers the curly-brace languages.

Languages
TypeScript, JavaScript, Java, C#, Rust
Turned on by
skylos . --quality (or -a)
Quality gate
counted toward the gate limits (full and diff scans). Reported in the quality list, so it counts toward max_quality (10 by default), not max_dead_code.
Finding type
defect
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

The statement was meant to do something and silently does nothing. Here the rejected coupon is never logged because the call comes after the throw, so support has no record of why a customer's coupon failed.

What Skylos flags

src/coupons.ts · TypeScriptReported on line 7
import { CouponExpired } from "./errors";import { logRejectedCoupon } from "./log"; export function applyCoupon(order: Order, coupon: Coupon): void {  if (coupon.expired) {    throw new CouponExpired(coupon.code);    logRejectedCoupon(order, coupon);  }  order.discount = coupon.amount;}

What Skylos leaves alone

src/coupons.ts · TypeScript
export function applyCoupon(order: Order, coupon: Coupon): void {  switch (coupon.kind) {    case "percent":      order.discount = order.total * coupon.rate;      break;    case "fixed":      order.discount = coupon.amount;      break;  }}

Why Skylos leaves it alone

The statement after the first break starts the next case, which a jump can reach. Skylos only looks inside { ... } blocks, and case clauses are not blocks.

More languages

Java

Flagged

src/main/java/com/shop/Coupons.java · JavaReported on line 7
package com.shop; public class Coupons {    public void applyCoupon(Order order, Coupon coupon) {        if (coupon.isExpired()) {            throw new CouponExpiredException(coupon.getCode());            logRejected(order, coupon);        }        order.setDiscount(coupon.getAmount());    }}

Not flagged

src/main/java/com/shop/Coupons.java · Java
package com.shop; public class Coupons {    public void applyCoupon(Order order, Coupon coupon) {        if (coupon.isExpired()) {            logRejected(order, coupon);            throw new CouponExpiredException(coupon.getCode());        }        order.setDiscount(coupon.getAmount());    }}

Why Skylos leaves it alone

The log call comes before the throw, so it runs. (Java's own compiler rejects most code after throw; Skylos reports it before the build does.)

Rust

Flagged

src/lib.rs · RustReported on line 4
pub fn discounted(total: u32, expired: bool) -> u32 {    if expired {        return total;        record_rejection(total);    }    total - 5} fn record_rejection(_total: u32) {}
Cargo.toml · TOML
[package]name = "shop"version = "0.1.0"edition = "2021"

Not flagged

src/lib.rs · Rust
pub fn discounted(total: u32, expired: bool) -> u32 {    if expired {        record_rejection(total);        return total;    }    total - 5} fn record_rejection(_total: u32) {}

Cargo.toml: same as in the flagged example.

Why Skylos leaves it alone

The call runs before the early return.

How Skylos decides

  1. Runs with --quality on TypeScript, JavaScript, Java, C# and Rust files.
  2. In each { ... } block, finds a return, throw, break or continue statement (in Rust return, break or continue) and reports the first statement after it that is not a comment, then moves on to the next block.
  3. Only the block's own statements count: an if that returns in both branches does not make the code after it unreachable, and switch cases are not blocks.
  4. In Rust, macro calls, items such as nested fn and attributes are not statements here, and a file that does not parse is skipped.
  5. Reports MEDIUM on the line of the unreachable statement.

How to fix it

Move the statement before the jump, or delete it if it is no longer needed.

src/coupons.ts · TypeScript · the fix, against the flagged file
import { CouponExpired } from "./errors";import { logRejectedCoupon } from "./log"; export function applyCoupon(order: Order, coupon: Coupon): void {  if (coupon.expired) {Removed:     throw new CouponExpired(coupon.code);    logRejectedCoupon(order, coupon);Added:     throw new CouponExpired(coupon.code);  }  order.discount = coupon.amount;}

When it can be wrong

  • In TypeScript and JavaScript, a helper function declared after the return is reported, although function declarations are hoisted and can be called from above.
  • It misses code after a call that never returns, such as process.exit(), and code after an if/else whose branches both return.

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 unreachable line.

    TypeScript
    logRejectedCoupon(order, coupon); // skylos: ignore[SKY-UC002] kept for #77
  • [tool.skylos] ignore = ["SKY-UC002"] turns the rule off for the project.

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