E0077

E0077#

Warning name: lexmatch_longest_match

使用 longest-match 语义的 lexmatch 已弃用。

lexmatch ... with longest 会触发此警告。正则匹配表达式不提供最长匹配语义;词法分析器风格的最长匹配请使用 lexscan ... with longest

错误示例#

///|
fn classify(input : String) -> String {
  lexmatch input with longest {
    ("if|[a-z]*" as token) => token.to_owned()
    _ => "other"
  }
}

///|
test {
  inspect(classify("iff"), content="iff")
}

建议#

将每个旧式字符串模式转换为正则字面量,并用 ^ 锚定开头;如果 case 必须消耗整个输入,还要用 $ 锚定结尾。将末尾的剩余绑定替换为 after=。最长匹配的 lexscan 不支持开头的剩余绑定或 case 守卫,因此这些情况需要手动重构。

///|
fn classify(input : String) -> String {
  lexscan input with longest {
    re"^(if|[a-z]*)$" as token => token.to_owned()
    _ => "other"
  }
}

///|
test {
  inspect(classify("iff"), content="iff")
}