E2000

E2000#

函数(类型、特征等)的使用被标记为警告。通常,警告消息会带有警告类型和警告的详细描述。如果你正在使用来自库的函数,这些警告是由库作者设置的,以提供有关函数使用的更多信息。

以下是一些常见的警告:

  • deprecated: 表示函数(类型、特征等)已被弃用,不应该使用。你应迁移到新的 API。

  • unsafe: 表示该 API 可能会导致 panic、具有内部不变式,或在某些情况下具有未定义的行为。对于这类警告的具体语义可能会因软件包而异,请咨询相关文档或软件包的作者以获得更多详细信息。

错误示例#

lib/top.mbt#
///| 
#deprecated("Use `greet` instead")
pub fn greeting() -> String {
  "Hello!"
}

///|
pub fn greet(name~ : String = "") -> String {
  if name != "" {
    "Hello!"
  } else {
    "Hello, \{name}!"
  }
}
top.mbt#
///|
fn main {
  println(@lib.greeting())
  //      ^~~~~~~~ Warning (Alert deprecated): Use `greet` instead(2000)
}

建议#

修复警告的一种方法是按照消息中的建议更改代码(如 deprecated):

top.mbt#
///|
fn main {
  println(@lib.greet())
}

如果你清楚知道自己在做什么,并且想要抑制警告,你可以更改 moon.pkg.json 文件,以禁用这种类型的警告。例如:

moon.pkg.json#
{
  "is-main": true,
  "import": [
    "moonbit-community/E2000-fixed/lib"
  ],
  "alert-list": "-deprecated"
}

备注

无法为一行/文件禁用警告。