False Negative for rule RSPEC-5855

Hi Team,

Both code snippets below violate this bug rule RSPEC-5855 by having this pattern “[ab]|a”. However, the bug wasn’t flagged in either of the two code snippets below.

Code snippet 1:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

class RegexBug {

    public static void main(String[] args) {

        String input = "abc";
        String pattern = "[ab]|a";
        Pattern r = Pattern.compile(pattern);

        Matcher m = r.matcher(input);
        while (m.find()) {
            System.out.println(m.group());
        }

        int value = 0;
        switch (value) {
            case 1:
                System.out.println("This code is unreachable");
                break;
            default:

                System.out.println("This code is also unreachable");

                break;
        }
    }
}

Code snippet 2:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
class RegexBug {
    public static void main(String[] args) {
        String input = "abc";
        String pattern = "[ab]|a";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(input);
        while (m.find()) {
            System.out.println(m.group());

       }
    }
}

screenshot from the code snippet 1

screenshot from the code snippet 2 which doesn’t flag the bug

Note: To rescan, use sonar-scanner to rescan the code snippets above to identify the issue.

Scanning information:
Sonarqube version: 10.7.0.96327
SonarScanner version: 5.0.1.3006
SonarQube Community Edition
Related language: Java

Hey there.

Thanks for the report. As I’ve noted in your other reports, please reproduce this using the latest version of SonarQube Community Build – 25.5.