E1007

E1007#

字段从未被读取。这包括结构体中的字段和枚举构造器中的字段。

错误示例#

对于枚举构造器字段

enum E {
  A(Int) // Warning: The 1st positional argument of constructor 'A' is unused.
  B(value~ : Int) // Warning: Field 'value' of constructor 'B' is unused.
}

fn main {
  ignore(B(value=1))
  match A(1) {
    A(_) => println("A")
    B(_) => println("B")
  }
}

对于结构体字段

struct S {
  value : Int // Warning: Field 'value' is never read
}

fn main {
  ignore(S::{ value : 1 })
}

建议#

如果枚举构造器中的字段未使用,你可以在模式中展开它们以使用它们:

fn main {
  // ...
  match A(1) {
    A(x) => println("A(\{x})")
    B(value~) => println("B(\{value})")
  }
}

如果结构体中的字段未使用,你可以在结构体模式中列出它们,或使用点语法访问它们:

fn main {
  let s = S::{ value : 1 }
  match s {
    { value } => println("S(\{value})")
  }
  println("S(\{s.value})")
}

或者,如果字段确实没有用,你可以从构造器中移除字段:

enum E {
  A
  B
}
struct S {
}