E0058

E0058#

Warning name: unused_non_capturing

Unnecessary non-capturing group in regex.

This warning is emitted when a regex contains a non-capturing group that is not needed. Consider simplifying the regex by removing the redundant group.

Erroneous example#

///|
test {
  let text = "xxabcyy"
  if text =~ (re"a(?:b)c", before~, after~) {
    inspect(before, content="xx")
    inspect(after, content="yy")
  } else {
    fail("")
  }
}

Suggestion#

Remove the redundant non-capturing group. If the matched part is needed, bind that regex subexpression with as. Regex named capture groups are available through Regex::execute and MatchResult::named_group, but they are not binders in =~ expressions:

///|
test {
  let text = "xxabcyy"
  if text =~ (re"a" + (re"b" as middle) + re"c", before~, after~) {
    inspect(before, content="xx")
    inspect(middle, content="b")
    inspect(after, content="yy")
  } else {
    fail("")
  }
}