E0053

E0053#

Unused trait bound.

多态函数或方法具有未使用的特征绑定。这可能是一个潜在的错误,因为你忘记应用该特征中的方法。

错误示例#

///|
trait Any {}

///|
pub fn[T : Any] check(_ : T) -> Unit {
  println("Hello")
}

///|
pub fn[T : Compare] shortlex(a : Array[T], b : Array[T]) -> Int {
  Int::compare(a.length(), b.length())
}

建议#

有几种方式可以修复这个警告:

  • 如果不需要此特征绑定,则可以将其删除

    ///|
    pub fn[T] check(_ : T) -> Unit {
      println("Hello")
    }
    
  • You can cast the variable to a trait object

    ///|
    pub fn[T : Any] check_any(t : T) -> Unit {
      let _ = t as &Any
      println("Hello")
    }
    
  • 您可以使用 Trait 中定义的方法

    ///|
    pub fn[T : Compare] shortlex(a : Array[T], b : Array[T]) -> Int {
      match Int::compare(a.length(), b.length()) {
        _..<0 => -1
        1..<_ => 1
        0 =>
          for i = 0; i < a.length(); i = i + 1 {
            match T::compare(a[i], b[i]) {
              _..<0 => return -1
              1..<_ => return 1
              0 => ()
            }
          } else {
            return 0
          }
      }
    }