A Regex is created inside a loop, so its pattern is parsed again on every round
What it finds
Constructing a Regex parses the pattern and builds a matcher, which costs far more than a match. Created inside a loop with the same pattern, that work repeats each round; a static field, or the static Regex methods that cache patterns, do it once.
From the rule's source, regex_built_in_loop.rs.
using System.Text.RegularExpressions; class Scanner{ int Count(string[] lines) { int n = 0; foreach (var line in lines) { var digits = new Regex("[0-9]+");tx0 reports: the same regular expression is built on every round of the loop n += digits.Matches(line).Count; } return n; }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/csharp:regex-built-in-loop" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/csharp:regex-built-in-loop] reason="..."tx0 explain tx0/csharp:regex-built-in-loop shows the same in the terminal. Found a false positive? Report it.