if is an expression, but without an else it has no value when the condition is false. When
its value is used, every path must produce one.
Wrong#
fn sign(c: bool): int = if c { 1 }Fixed#
fn sign(c: bool): int = if c { 1 } else { 0 }Common causes#
- A missing
elsebranch in a function's final expression. - An
ifas the last line of a block whose value is returned. - To run code only sometimes, put the
ifbefore the block's real result.