E0077

E0077#

Warning name: lexmatch_longest_match

使用 longest-match 语义的 lexmatch

此警告默认关闭。可以启用它来审查剩余的 lexmatch ... with longest 用法。正则匹配表达式不提供 longest-match 语义,因此依赖词法分析器风格最长匹配的代码可能需要保留 lexmatch ... with longest

错误示例#

///|
fn is_keyword(input : String) -> Bool {
  lexmatch input with longest {
    "if" => true
    _ => false
  }
}

///|
test {
  ignore(is_keyword)
}

建议#

如果不需要 longest-match 行为,请改写代码以避免使用 lexmatch。例如,对于全字符串正则检查,可以使用正则匹配表达式。

如果需要 longest-match 行为,请保留 lexmatch ... with longest,并让这个审查用警告保持关闭,或在局部抑制它。

///|
fn is_keyword(input : String) -> Bool {
  input =~ re"^if$"
}

///|
test {
  ignore(is_keyword)
}