E0077

E0077#

Warning name: lexmatch_longest_match

Using lexmatch with longest-match semantics.

This warning is disabled by default. It can be enabled to audit remaining lexmatch ... with longest usages. Regex match expressions do not provide longest-match semantics, so code that relies on lexer-style longest matching may need to keep lexmatch ... with longest.

Erroneous example#

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

///|
test {
  ignore(is_keyword)
}

Suggestion#

If longest-match behavior is not required, rewrite the code without lexmatch. For example, use a regex match expression for a full-string regex check.

If longest-match behavior is required, keep lexmatch ... with longest and leave this audit warning disabled or suppress it locally.

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

///|
test {
  ignore(is_keyword)
}