E4218

E4218#

Compiler diagnostic name: lexscan_pattern_not_anchored.

A lexscan regex pattern is not anchored where anchoring is required.

Patterns used with the longest strategy must start at the beginning of the input. Anchor each such regex with ^.

Erroneous example#

///|
pub fn classify(input : String) -> String {
  lexscan input with longest {
    re"[a-z]+" => "word"
    _ => "other"
  }
}

Suggestion#

Add ^ to the start of the regex. Add $ as well only when the case must consume the whole input.

///|
pub fn classify(input : String) -> String {
  lexscan input with longest {
    (re"^[a-z]+", after=_) => "word"
    _ => "other"
  }
}