SONARJAVA-6864 Fix open SonarQube issues on master - #6050
Conversation
Reported by Sonar S6916
Replace continue statements with inverted conditions in HashCodeMismatchedFieldsCheck and LocalVariablesShouldNotSpanSwitchCaseGroupsCheck. Revert the pattern match guard in CompilationOrPreparationInLoopCheck which used an unsupported "when" syntax, restoring the original "if" statement. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add final keyword to classes that have no subclasses: - InternalSyntaxTrivia - HardCodedSecretCheck - SmapFile - Jasper.ServletContext Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Convert instanceof if/else chains to Java 21+ pattern-matching switch expressions in 6 locations for improved readability. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace anonymous implementations of functional interfaces with lambda expressions in 8 locations across test files. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- S6916: Use pattern matching instanceof in CompilationOrPreparationInLoopCheck - S6485: Use HashMap.newHashMap() in AnnotationFieldReferenceFinder - S6878: Use record pattern in SpelExpressionCheck Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Extract ternary expressions into intermediate variables to improve readability in 5 locations. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace continue statements with inverted conditions across 28 files. For each occurrence, the if-continue pattern is replaced by inverting the condition and wrapping the remaining loop body inside the if block. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Revert anonymous-to-lambda conversions where Mockito spy() is used, since Mockito cannot spy on lambdas - Revert anonymous-to-lambda in DefaultJavaResourceLocatorTest since the test counts generated .class files (lambdas don't generate them) - Add null case to switch expression in JSymbolMetadata to handle null annotation values Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| Object obj = new I() { | ||
| @Override | ||
| public void foo() { | ||
| // empty implementation | ||
| } |
There was a problem hiding this comment.
💡 Quality: Revert leftovers delete comments that exempt empty methods
After b2bb978 restored the anonymous classes in these two test files, the only surviving net change is the deletion of the // empty implementation and // Do nothing comments inside the empty method bodies. EmptyMethodsCheck (S1186) explicitly skips empty bodies that containsComment(block), so these comments were load-bearing exemptions, and their removal is a pure leftover of an incomplete revert with no rule benefit. Restore both comments so the files match master exactly.
Was this helpful? React with 👍 / 👎
| private Object convertAnnotationValue(Object value) { | ||
| if (value instanceof IVariableBinding iVariableBinding) { | ||
| return sema.variableSymbol(iVariableBinding); | ||
| } else if (value instanceof ITypeBinding iTypeBinding) { | ||
| return sema.typeSymbol(iTypeBinding); | ||
| } else if (value instanceof IAnnotationBinding iAnnotationBinding) { | ||
| return sema.annotation(iAnnotationBinding); | ||
| } else if (value instanceof Object[] a) { | ||
| // Godin: probably better to not modify original array | ||
| Object[] result = new Object[a.length]; | ||
| for (int i = 0; i < a.length; i++) { | ||
| result[i] = convertAnnotationValue(a[i]); | ||
| return switch (value) { | ||
| case null -> value; | ||
| case IVariableBinding iVariableBinding -> sema.variableSymbol(iVariableBinding); | ||
| case ITypeBinding iTypeBinding -> sema.typeSymbol(iTypeBinding); | ||
| case IAnnotationBinding iAnnotationBinding -> sema.annotation(iAnnotationBinding); | ||
| case Object[] a -> { | ||
| Object[] result = new Object[a.length]; | ||
| for (int i = 0; i < a.length; i++) { | ||
| result[i] = convertAnnotationValue(a[i]); | ||
| } | ||
| yield result; | ||
| } | ||
| return result; | ||
| } else { | ||
| return value; | ||
| } | ||
| default -> value; | ||
| }; |
There was a problem hiding this comment.
💡 Quality: Redundant duplicate arms in convertAnnotationValue switch
case null -> value; and default -> value; are two labels with identical bodies; the null label is only needed to avoid the pattern-switch NPE, so both can be merged into a single case null, default -> value; arm. Behaviour is unchanged either way (the old if/else chain also returned value for null since null instanceof X is false), but the duplicated arm is exactly the kind of redundancy this cleanup PR targets.
Merge the null and default arms:
return switch (value) {
case IVariableBinding iVariableBinding -> sema.variableSymbol(iVariableBinding);
case ITypeBinding iTypeBinding -> sema.typeSymbol(iTypeBinding);
case IAnnotationBinding iAnnotationBinding -> sema.annotation(iAnnotationBinding);
case Object[] a -> {
Object[] result = new Object[a.length];
for (int i = 0; i < a.length; i++) {
result[i] = convertAnnotationValue(a[i]);
}
yield result;
}
case null, default -> value;
};
Was this helpful? React with 👍 / 👎
Code Review 👍 Approved with suggestions 0 resolved / 3 findingsResolves 60 open SonarQube issues across eight rules by applying modern Java features like switch expressions, lambdas, and pattern matching. Consider addressing the Unused import left after anonymous class to lambda conversion, Revert leftovers delete comments that exempt empty methods, and Redundant duplicate arms in convertAnnotationValue switch findings. 💡 Quality: Unused import left after anonymous class to lambda conversionConverting the two anonymous Drop the now-unused import💡 Quality: Revert leftovers delete comments that exempt empty methods📄 java-frontend/src/test/java/org/sonar/java/DefaultJavaResourceLocatorTest.java:130-133 📄 java-frontend/src/test/java/org/sonar/java/model/JParserTest.java:859-862 After b2bb978 restored the anonymous classes in these two test files, the only surviving net change is the deletion of the 💡 Quality: Redundant duplicate arms in convertAnnotationValue switch📄 java-frontend/src/main/java/org/sonar/java/model/JSymbolMetadata.java:410-424
Merge the null and default arms🤖 Prompt for agentsOptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
- Rename local 'context' variable to 'scannerContext' to fix S1117 (variable shadowing) - Remove unused JavaFileScannerContext import to fix S1128 - Restore load-bearing comments for S1186 exemptions in test files - Merge redundant 'case null' and 'default' switch arms in JSymbolMetadata Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|




Summary
all 60some open SonarQube issues flagged on the master branch across 8 rulesfinal—InternalSyntaxTrivia,HardCodedSecretCheck,SmapFile,Jasper.ServletContextinstanceofif/else chains with Java 21+ pattern-matching switch expressionsHashMap.newHashMap(), record patternscontinuestatements by inverting conditionsTest plan
java-checks,java-checks-aws,java-checks-testkit,java-frontend,java-jsp)java-checksunrelated to these changes:StaticMethodHidingCheckTest,XmlRpcExtensionsCheckTest,SpringComponentSpecializationCheckTest)🤖 Generated with Claude Code