Skip to content

SONARJAVA-6864 Fix open SonarQube issues on master - #6050

Merged
romainbrenguier merged 10 commits into
masterfrom
romain/fix-qg
Aug 27, 2026
Merged

SONARJAVA-6864 Fix open SonarQube issues on master#6050
romainbrenguier merged 10 commits into
masterfrom
romain/fix-qg

Conversation

@romainbrenguier

@romainbrenguier romainbrenguier commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fix all 60 some open SonarQube issues flagged on the master branch across 8 rules
  • S9345 (4 CRITICAL): Make classes finalInternalSyntaxTrivia, HardCodedSecretCheck, SmapFile, Jasper.ServletContext
  • S6880 (6 MAJOR): Replace instanceof if/else chains with Java 21+ pattern-matching switch expressions
  • S9357 (8 MAJOR): Convert anonymous functional interface implementations to lambdas (where compatible with Mockito)
  • S6916, S6485, S6878 (3 MAJOR): Pattern matching instanceof, HashMap.newHashMap(), record patterns
  • S9358 (5 MINOR): Extract ternary expressions into intermediate variables
  • S909 (34 MINOR): Remove continue statements by inverting conditions

Test plan

  • All affected modules compile (java-checks, java-checks-aws, java-checks-testkit, java-frontend, java-jsp)
  • All tests pass in affected modules (3 pre-existing failures in java-checks unrelated to these changes: StaticMethodHidingCheckTest, XmlRpcExtensionsCheckTest, SpringComponentSpecializationCheckTest)
  • CI passes

🤖 Generated with Claude Code

romainbrenguier and others added 9 commits August 27, 2026 13:04
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>
@hashicorp-vault-sonar-prod hashicorp-vault-sonar-prod Bot changed the title Fix all 60 open SonarQube issues on master SONARJAVA-6864 Fix all 60 open SonarQube issues on master Aug 27, 2026
@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

SONARJAVA-6864

Comment on lines 130 to 133
Object obj = new I() {
@Override
public void foo() {
// empty implementation
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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 👍 / 👎

Comment on lines 410 to +424
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;
};

@gitar-bot gitar-bot Bot Aug 27, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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 👍 / 👎

@gitar-bot

gitar-bot Bot commented Aug 27, 2026

Copy link
Copy Markdown
Code Review 👍 Approved with suggestions 0 resolved / 3 findings

Resolves 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 conversion

📄 java-checks-testkit/src/test/java/org/sonar/java/checks/verifier/internal/JavaCheckVerifierTest.java:47

Converting the two anonymous JavaFileScanner implementations to lambdas removed the only references to JavaFileScannerContext in JavaCheckVerifierTest.java; the import at line 47 is now unused (verified: grep finds no other occurrence in the file). This introduces a new S1128 issue in a PR whose purpose is to empty the issue list. Remove the import. (The same import in JavaAstScannerTest.java is still used by the remaining inner scanner classes and must stay.)

Drop the now-unused import
// delete line 47:
// import org.sonar.plugins.java.api.JavaFileScannerContext;
💡 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 // 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.

💡 Quality: Redundant duplicate arms in convertAnnotationValue switch

📄 java-frontend/src/main/java/org/sonar/java/model/JSymbolMetadata.java:410-424

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;
};
🤖 Prompt for agents
Code Review: Resolves 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.

1. 💡 Quality: Unused import left after anonymous class to lambda conversion
   Files: java-checks-testkit/src/test/java/org/sonar/java/checks/verifier/internal/JavaCheckVerifierTest.java:47

   Converting the two anonymous `JavaFileScanner` implementations to lambdas removed the only references to `JavaFileScannerContext` in `JavaCheckVerifierTest.java`; the import at line 47 is now unused (verified: grep finds no other occurrence in the file). This introduces a new S1128 issue in a PR whose purpose is to empty the issue list. Remove the import. (The same import in `JavaAstScannerTest.java` is still used by the remaining inner scanner classes and must stay.)

   Fix (Drop the now-unused import):
   // delete line 47:
   // import org.sonar.plugins.java.api.JavaFileScannerContext;

2. 💡 Quality: Revert leftovers delete comments that exempt empty methods
   Files: 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 `// 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.

3. 💡 Quality: Redundant duplicate arms in convertAnnotationValue switch
   Files: java-frontend/src/main/java/org/sonar/java/model/JSymbolMetadata.java:410-424

   `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.

   Fix (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;
   };

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing less information.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

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>
@sonarqube-next

Copy link
Copy Markdown
Contributor

@romainbrenguier
romainbrenguier marked this pull request as ready for review August 27, 2026 14:40

@nathsou nathsou left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, by my count, this fixes 16 issues not 60 though?

@romainbrenguier romainbrenguier changed the title SONARJAVA-6864 Fix all 60 open SonarQube issues on master SONARJAVA-6864 Fix open SonarQube issues on master Aug 27, 2026
@romainbrenguier
romainbrenguier merged commit 15adb94 into master Aug 27, 2026
18 checks passed
@romainbrenguier
romainbrenguier deleted the romain/fix-qg branch August 27, 2026 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants