Sisu
W0014

Local or parameter that nothing reads

Warning

Built from the repository at commit a8614dc.

The warning is for a local or a parameter when no expression reads its value. A local is a let, a let mut, the name of a for or a name that a pattern binds. The program still builds. Only the locals and parameters of live code are judged; live code is as for W0012. A name that starts with _ gets no warning, and self is never judged. The warning does not show while the program has an error. For a for name or a pattern name, the safe fix replaces the name with _. A let or a parameter has no fix, because only you know if the value is still necessary.

Why

A name that nothing reads is often a bug: the code computes or receives a value and then does not use it. In the broken program below, area takes a height h but multiplies the width by itself. These are not reads:

  • a write to the name, x = e
  • a compound assignment, x += e or s += "a", which reads the old value only to write it back
  • a write to an element, xs[i] = e

Any other read counts, also a read inside the value that is written back, as in x = x + 1. A field write through a class, p.x = 1, reads p: it changes a shared object that other code can see. A function or a method with a todo gets no warning for its parameters. The body that is not yet written will probably use them. Its locals are still judged. An unread let mut gets this warning and not W0002: delete it, and both go. To keep a name on purpose, start it with _.

Broken

fn area(w: i64, h: i64) -> i64 {
    w * w
}

fn main() {
    print(area(2, 3))
}

What sisu says

warning[W0014]: parameter `h` is never read
 --> W0014.sisu:1:17
  |
1 | fn area(w: i64, h: i64) -> i64 {
  |                 ^ never read
  |
  = help: if this is on purpose, name it `_h`

sisu: 1 warning
sisu: for more on a code, run `sisu explain <code>`: W0014

Fixed

fn area(w: i64, h: i64) -> i64 {
    w * h
}

fn main() {
    print(area(2, 3))
}

Related

  • W0002: variable that is never reassigned.
  • W0012: function, method, class, enum or constant that no live code uses.