Skip to content

Add UnnecessarySemicolonOperation (java:S2959)#156

Merged
RadikalJin merged 1 commit into
mainfrom
feature/sonar-s2959-unnecessary-semicolon
Jun 11, 2026
Merged

Add UnnecessarySemicolonOperation (java:S2959)#156
RadikalJin merged 1 commit into
mainfrom
feature/sonar-s2959-unnecessary-semicolon

Conversation

@RadikalJin

Copy link
Copy Markdown
Member

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.

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>
@RadikalJin RadikalJin merged commit e1dc45c into main Jun 11, 2026
1 check failed
@RadikalJin RadikalJin deleted the feature/sonar-s2959-unnecessary-semicolon branch June 11, 2026 10:57
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.

1 participant