E0076

E0076#

Warning name: lexmatch_first_match

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

lexmatch 表达式使用默认的 first-match 策略时会触发此警告,包括 lexmatch ... with first。在新代码中,普通正则检查应优先使用正则匹配表达式。

错误示例#

///|
fn classify(input : String) -> String {
  lexmatch input {
    "if" => "keyword"
    _ => "other"
  }
}

///|
test {
  ignore(classify)
}

建议#

只需要布尔结果时,请把 first-match lexmatch 改写为正则匹配表达式;不同 case 需要返回不同值时,请改用 lexscan。需要词法分析器风格的最长匹配行为时,请使用 lexscan ... with longest

///|
fn classify(input : String) -> String {
  if input =~ re"^if$" {
    "keyword"
  } else {
    "other"
  }
}

///|
test {
  ignore(classify)
}