java:S6395 false-positive on flag-setting groups

  • Language: Java
  • Rule: java:S6395 “Non-capturing groups without quantifier should not be used”
  • SonarLint under Eclipse 4.30 (2023-12)

The rule wrongly raises an issue when non-capturing groups that (re)set flags are used (if the same group without setting flags would also raise it). Such groups can be semantically significant and their presence might be required (say, in String.split(String), since flags can’t otherwise be passed to the pattern).

"fooBar".split("(?i:b)"); is wrongly flagged.

For comparison, "fooBar".split("(?i)b") is not flagged, and "fooBar".split("(?:b)") is flagged, which is the expected behavior in both cases. Note that they have different semantics than the above example.

Self-contained sample with these examples in Try It Online!

Hello, @ivaniesta14, and welcome to the Sonar Community!

Thank you for reporting such an exciting scenario, [SONARJAVA-4744] - Jira was created to address it.