[false-negative]S1219 can not detect non-case labels which are wrapped in braces

Affects Sonarqube Version:
Sonarqube version: 8.1.0.31237
sonar-scanner version: 4.2.0.1873-windows

Rule:
S1219: “switch” statements should not contain non-case labels

Description:
Unable to detect non-case labels which are wrapped in braces.
This rule is implemented in java-checks-6.2.0.21135-sources.jar!\org\sonar\java\checks\SwitchWithLabelsCheck.java,
This rule uses for (StatementTree statementTree : cgt.body()) to traverse all StatementTree under the switch, and then uses if (statementTree.is(LABELED_STATEMENT)) to determine whether it is a non-case label.
When non-case labels are wrapped in braces, they may no longer belong to cgt.body().

Code Sample demonstrating the issue:

switch (state) {
	// Seeking the first character of a new word
	case NEW:
	case NEW_WITH_AT_LEAST_ONE_WORD: {
		newSwitch:	// unable to detect this label
		switch (c) {
			// Empty word
			case ',':
	}

Expected outcome:
false-negative

Running Sonarqube through:
command line

Hello @wongwuchiu,

I agree that it makes sense to detect this case, so I created a ticket. Thank you for your report.

Cheers,
Sebastian

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.