Lyricdev
GitHub

LYR-SEM0023

a mut function that has nothing to write

error · spec 05-interfaces

mut is written on a free function, which has no receiver, or on a method of a type that has no var field and whose body does not replace this (unless an interface demands the method as mut).

Explanation

mut fn marks a method that writes its receiver; callers have to hand it a writable place. A free function has no receiver, and a method of a type without var fields that never assigns this writes nothing — the mark would only restrict its callers.

Drop mut, or declare the field the method writes with var.

Wrong

struct Counter {
    n: int,

    mut fn bump(): void { }
}

fn main(): void {
    var c = Counter { n = 0 };
    c.bump();
}
struct Counter {
    var n: int,
    mut fn bump(): void { this.n = this.n + 1; }
}

fn main(): void {
    var c = Counter { n = 0 };
    c.bump();
}