Field that no live code reads, or variant that no live code builds
WarningA field of a class that live code builds is a warning when no live code reads it, and the program still builds. A variant of an enum that live code builds is a warning when no live code builds that variant. Live code is as for W0012. A write to a field, or a += to it, does not read it. == and contains read every field. A pattern that names a variant does not build it. A use in dead code does not count, and the warning shows up to three such uses. A class or an enum that nothing builds gets W0012, not this warning. A field whose name starts with _ gets no warning. The warning does not show while the program has an error. There is no fix, because only you know if the member is still necessary.
Why
A field that nothing reads, or a variant that nothing builds, is a member that the type carries for nothing. Each object holds the field's value, and each match on the enum needs an arm for the variant. But no part of the program uses them. They are often what a refactor leaves behind. == and contains on a class compare it field by field. They read every field of the class, and of each class that it holds. A comparison with None reads no field. Delete the member, or read it where the program needs it. To keep a field on purpose, start its name with _.
Broken
class Point {
x: i64
y: i64
}
fn main() {
let p = Point(x: 1, y: 2)
print(p.x)
}
What sisu says
warning[W0013]: field `y` is never read --> W0013.sisu:3:5 | 3 | y: i64 | ^ never read | = help: delete it, or name it `_y` if it is kept on purpose sisu: 1 warning sisu: for more on a code, run `sisu explain <code>`: W0013
Fixed
class Point {
x: i64
y: i64
}
fn main() {
let p = Point(x: 1, y: 2)
print(p.x + p.y)
}
Related
- W0012: function, method, class, enum or constant that no live code uses.
Sisu