E1120: result marked must_use is ignored

View source

A value that carries an outcome has to be looked at.

A value that carries an outcome has to be looked at. Two things make a value must-use (ADR-0090):

  • a verb marked meta must_use("reason"), such as one that returns a status code. The message repeats the reason that the declaration gives;
  • every Result and every Option. A Result carries an error and an Option may hold no value, and the message says so.

A call that is a statement of its own throws its result away, and the compiler rejects it. A result is used when it is bound to a binding that is used, returned, passed as an argument, used by an operator or propagated with ?. The tail of an arm of a case or an if whose value is used is that value, so it is not thrown away.

A binding that holds a Result or an Option and is never mentioned again in its scope throws the value away in the same way, at the end of the scope, and is rejected too. Any mention counts as a use (a read, an argument in any role, a case, a return, ?, drop), anywhere in the scope: the check does not follow paths.

Example

actus
meta must_use("the caller must inspect the status")
verb write_frame() -> Int {
    return 0;
}

verb main() {
    write_frame();
}

A Result needs no marker:

actus
verb read_value() -> Result[Int, Int] {
    return Result[Int, Int].Ok(1);
}

verb main() {
    read_value();
}

A Result held by a binding that nothing uses:

actus
verb read_value() -> Result[Int, Int] {
    return Result[Int, Int].Ok(1);
}

verb main() {
    erg outcome = read_value();
}

Fix

Bind the result and use it.

actus
meta must_use("the caller must inspect the status")
verb write_frame() -> Int {
    return 0;
}

verb main() {
    erg status = write_frame();
    if status != 0 {
        return;
    }
}

Propagate the error with ? when the verb can return it:

actus
verb read_value() -> Result[Int, Int] {
    return Result[Int, Int].Ok(1);
}

verb main() -> Result[Int, Int] {
    read_value()?;
    return Result[Int, Int].Ok(0);
}

Or end the value on purpose with drop, when ignoring it is the intent, as in a cleanup path:

actus
verb read_value() -> Result[Int, Int] {
    return Result[Int, Int].Ok(1);
}

verb main() {
    drop(read_value());
}

A binding that nothing uses is replaced by the statement: drop(read_value()); or read_value()?;.

actus check --format json carries these as fixes marked review, and actus fix lists them without applying them, because each one decides what the program does with the outcome.


Run actus explain E1120 in your terminal to read this entry offline. Browse Modules & rules or all error codes.