Add UnnecessarySemicolonOperation (java:S2959)#156
Merged
Conversation
Implements SonarQube rule java:S2959 "Unnecessary semicolons should be omitted". Four forms are detected and removed: - Try-with-resources trailing semicolons (SonarJava's core S2959 detection: separators.size() == resources.size() indicates the last separator is unnecessary) - Empty statements (lone ;) inside braced blocks and initialisers - Empty statements inside switch/switch-expression case lists - Class-level semicolons after method/constructor bodies, invisible to the JDT AST, found by scanning source-text gaps between bodyDeclarations() entries at every nesting level (classes, inner classes, anonymous classes, enums, interfaces, records, annotation types) Control-flow empty bodies (while(x);, for(...);, if(x);) are deliberately preserved as they are intentional and removing them would change program semantics. For enum declarations, the required semicolon separating enum constants from body declarations is correctly skipped to avoid false positives. Also ensures CompilationUnitProperty.SOURCE is set on pre-parsed CompilationUnits in the batch-parse path (AstraCore), mirroring readAsCompilationUnit, so that source-text-scanning operations work correctly in production batch runs. 9 tests covering all detected forms plus no-op control-flow preservation. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements SonarQube rule java:S2959 "Unnecessary semicolons should be omitted".
Four forms are detected and removed:
Control-flow empty bodies (while(x);, for(...);, if(x);) are deliberately preserved as they are intentional and removing them would change program semantics.
For enum declarations, the required semicolon separating enum constants from body declarations is correctly skipped to avoid false positives.
Also ensures CompilationUnitProperty.SOURCE is set on pre-parsed CompilationUnits in the batch-parse path (AstraCore), mirroring readAsCompilationUnit, so that source-text-scanning operations work correctly in production batch runs.
9 tests covering all detected forms plus no-op control-flow preservation.