diff --git a/CHANGELOG.md b/CHANGELOG.md index 78f638e11..120fc5336 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- `EmptyBlock` now ignores all empty blocks containing an explanatory comment. + ## [1.17.2] - 2025-07-03 ### Fixed diff --git a/delphi-checks/src/main/java/au/com/integradev/delphi/checks/EmptyBlockCheck.java b/delphi-checks/src/main/java/au/com/integradev/delphi/checks/EmptyBlockCheck.java index 2cb368d47..910285968 100644 --- a/delphi-checks/src/main/java/au/com/integradev/delphi/checks/EmptyBlockCheck.java +++ b/delphi-checks/src/main/java/au/com/integradev/delphi/checks/EmptyBlockCheck.java @@ -20,8 +20,6 @@ import org.sonar.check.Rule; import org.sonar.plugins.communitydelphi.api.ast.AnonymousMethodNode; -import org.sonar.plugins.communitydelphi.api.ast.CaseItemStatementNode; -import org.sonar.plugins.communitydelphi.api.ast.CaseStatementNode; import org.sonar.plugins.communitydelphi.api.ast.CompoundStatementNode; import org.sonar.plugins.communitydelphi.api.ast.DelphiNode; import org.sonar.plugins.communitydelphi.api.ast.ElseBlockNode; @@ -50,37 +48,31 @@ public DelphiCheckContext visit(CompoundStatementNode block, DelphiCheckContext private static boolean shouldAddViolation(CompoundStatementNode block) { DelphiNode parent = block.getParent(); - if (parent instanceof RoutineBodyNode || parent instanceof AnonymousMethodNode) { - // Handled by EmptyRoutineRule + if (!block.getComments().isEmpty()) { + // An empty block is OK if it has an explanatory comment. return false; } - if (parent instanceof ExceptItemNode) { - // Handled by SwallowedExceptionsRule + if (parent instanceof RoutineBodyNode || parent instanceof AnonymousMethodNode) { + // Handled by EmptyRoutine return false; } - if (parent instanceof CaseItemStatementNode) { - // Handling all cases in a case statement is a reasonable thing to do. - // With that being said, a comment is required. - return block.getComments().isEmpty(); + if (parent instanceof ExceptItemNode) { + // Handled by SwallowedException + return false; } if (parent instanceof StatementListNode) { StatementListNode statementList = (StatementListNode) parent; - DelphiNode grandparent = parent.getParent(); + DelphiNode enclosing = statementList.getParent(); if (statementList.getStatements().size() == 1) { - if (grandparent instanceof ElseBlockNode - && grandparent.getParent() instanceof CaseStatementNode) { - // Handling all cases in a case statement is a reasonable thing to do. - // With that being said, a comment is required. - return block.getComments().isEmpty(); + if (enclosing instanceof ElseBlockNode) { + enclosing = enclosing.getParent(); } - - // Handled by SwallowedExceptionsRule - return !(grandparent instanceof ElseBlockNode) - || !(grandparent.getParent() instanceof ExceptBlockNode); + // Handled by SwallowedException + return !(enclosing instanceof ExceptBlockNode); } } diff --git a/delphi-checks/src/main/resources/org/sonar/l10n/delphi/rules/community-delphi/EmptyBlock.html b/delphi-checks/src/main/resources/org/sonar/l10n/delphi/rules/community-delphi/EmptyBlock.html index c71182fa1..f9438300e 100644 --- a/delphi-checks/src/main/resources/org/sonar/l10n/delphi/rules/community-delphi/EmptyBlock.html +++ b/delphi-checks/src/main/resources/org/sonar/l10n/delphi/rules/community-delphi/EmptyBlock.html @@ -5,7 +5,7 @@
EmptyRoutine rule)SwallowedException rule)