Skip to content
Playground(another site)

bynk.types.regex_not_literal

Error A regex method's pattern (matchesRegex, containsRegex, findRegex, findAllRegex, replaceRegex, splitRegex) is not a string literal, so its ReDoS check cannot run at compile time.

A regex method checks its pattern when the program compiles, to reject one that can take catastrophic time on crafted input (ReDoS). A pattern built at run time would escape that check, so it must be a string literal. Write the pattern in place, as an ordinary or a raw string, r"\d+". To look for text that varies, use contains, which takes plain text.

Example

fn has(s: String, pattern: String) -> Bool {
s.containsRegex(pattern) // ✗ the pattern is not a literal
}
fn has(s: String) -> Bool {
s.containsRegex("(?i)error") // ✓
}

In the Book

See also

  • The grammar rule it governs: method_call.
  • Every code in its category, Type checking, on the diagnostic index.
  • bynk explain bynk.types.regex_not_literal shows the same from the installed toolchain, without a network.