SKY-UC001 · Dead code
Unreachable Python code
Python statements that can never run. Typical cases are code after a return, raise, break or continue in the same block, the body of an if whose condition is always false, and a loop over an empty literal.
- Languages
- Python
- 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), notmax_dead_code. - Finding type
- defect
Why it matters
The line was written to do something, and it silently does nothing. In the example below the rejected coupon is never logged because the call sits after the raise, so support cannot see why customers' coupons failed.
What Skylos flags
"""Coupon handling.""" from shop.errors import CouponExpiredfrom shop.log import log_rejected_coupon def apply_coupon(order, coupon): if coupon.expired: raise CouponExpired(coupon.code) log_rejected_coupon(order, coupon) order.discount = coupon.amountWhat Skylos leaves alone
"""Refunds from the bank, not available yet.""" def iter_refunds(order): """An empty generator until the bank API ships.""" return yieldWhy Skylos leaves it alone
The lone yield never runs, but its presence makes iter_refunds a generator, so callers can loop over it and get nothing. Skylos keeps a function's only yield (or yield None) for this idiom.
How Skylos decides
- Runs with
--qualityon Python files. Looks at the statement blocks of modules, functions, classes,if, loops,withandtrybodies. - Reports the first statement that follows a
return,raise,breakorcontinuein the same block, or anif/elsewhose branches all end that way, or awhile Trueloop with nobreak. - Reports the first statement of an
ifbody whose condition is always false, of anelseafter a condition that is always true, of awhilewhose condition is always false, and of aforover an empty literal ([],(),{},""). A condition counts only when it is built from constants,not,andandor;sys.version_infochecks within the project'srequires-pythonrange are left alone. - Skips a function's only
yieldoryield Nonewhen it is the unreachable statement. Reports MEDIUM on the unreachable line.
How to fix it
Move the statement to where it can run, here before the raise, or delete it if it is no longer needed.
"""Coupon handling.""" from shop.errors import CouponExpiredfrom shop.log import log_rejected_coupon def apply_coupon(order, coupon): if coupon.expired:Removed: raise CouponExpired(coupon.code) log_rejected_coupon(order, coupon)Added: raise CouponExpired(coupon.code) order.discount = coupon.amountWhen it can be wrong
- Code kept under
if False:on purpose, for example to switch a block off during debugging, is reported. - It misses code after a call that never returns, such as
sys.exit()or a function that always raises.
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.
Python log_rejected_coupon(order, coupon) # skylos: ignore[SKY-UC001] kept for #77[tool.skylos] ignore = ["SKY-UC001"]turns the rule off for the project.