Home / Docs / Diagnostics

U3025: if error clause on an expression that cannot fail

Release v0.4.0

An if error clause is on an expression that cannot fail.

Why Una rejects it

In edition 0.3, an if error clause belongs only where the expression can fail (spec/06 §3, ADR 0100). A clause on an expression that cannot fail never runs:

It also misleads the reader, and a dead if error fail V in a private function gives that function an error kind of its own, which every caller then has to handle.

The rule waits while the package has an error that can hide a failure: an error outside U3007, U3008 and U3009, or one inside the function that is called.

Editions 0.1 and 0.2 accept the clause.

How to fix it

Remove the clause. una fix applies this repair.

The repair at a glance

src/demo.unadiff
  module demo

  kind LookupError = Missing | Unavailable

  pub next(n: Int) -> Int
    fails LookupError
  =
    let x = n + 1
-     if error fail Unavailable
    x

Failing example

una.tomltoml
[package]
name = "demo"
version = "0.1.0"
edition = "0.3"
src/demo.unauna
module demo

kind LookupError = Missing | Unavailable

pub next(n: Int) -> Int
  fails LookupError
=
  let x = n + 1
    if error fail Unavailable
  x

Fixed example

una.tomltoml
[package]
name = "demo"
version = "0.1.0"
edition = "0.3"
src/demo.unauna
module demo

kind LookupError = Missing | Unavailable

pub next(n: Int) -> Int
  fails LookupError
=
  let x = n + 1
  x