Uploaded image for project: 'SonarJava'
  1. SonarJava
  2. SONARJAVA-3688

FP on S5860(UnusedGroupNamesCheck) for name referenced by dollar curly braces

    Details

    • Type: False-Positive
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.13
    • Component/s: Rules
    • Labels:
      None

      Description

      Group names are not only referenced by the Matcher#group(String name) method but can also be referenced by ${name} in a string literal see:
      java/11/docs/Matcher.html#appendReplacement(java.lang.StringBuffer,java.lang.String)

      The replacement string may contain references to subsequences captured during the previous match: Each occurrence of ${name} or $g will be replaced by the result of evaluating the corresponding group(name) or group(g) respectively.

      "appendReplacement" and all methods that call it are impacted like "replaceAll".

      For example:

        public static void main(String[] args) {
          Pattern pattern = Pattern.compile("a(?<foo>.)"); // false-positive
          System.out.println(pattern.matcher("abac").replaceAll("x${foo}"));
          // print: xbxc
        }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                alban.auzeill Alban Auzeill
                Reporter:
                alban.auzeill Alban Auzeill
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: