Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- False positives on enumerable method `GetEnumerator` in `UnusedRoutine`.
- False positives on enumerator method `MoveNext` in `UnusedRoutine`.
- False positives on enumerator property `Current` in `UnusedProperty`.
- False positives on enums that are never referenced by name (but have used values) in `UnusedType`.
- Name resolution failures in legacy initialization sections referencing the implementation section.
- Incorrect file position calculation for multiline string tokens.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@
*/
package au.com.integradev.delphi.checks;

import java.util.List;
import java.util.function.Predicate;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.plugins.communitydelphi.api.ast.EnumElementNode;
import org.sonar.plugins.communitydelphi.api.ast.EnumTypeNode;
import org.sonar.plugins.communitydelphi.api.ast.InterfaceSectionNode;
import org.sonar.plugins.communitydelphi.api.ast.NameDeclarationNode;
import org.sonar.plugins.communitydelphi.api.ast.TypeDeclarationNode;
import org.sonar.plugins.communitydelphi.api.check.DelphiCheck;
import org.sonar.plugins.communitydelphi.api.check.DelphiCheckContext;
Expand Down Expand Up @@ -63,6 +68,10 @@ private boolean isViolation(TypeDeclarationNode node) {
return false;
}

if (node.isEnum() && hasElementsWithUsages((EnumTypeNode) node.getTypeNode())) {
return false;
}

if (excludeApi
&& (node.isPublic() || node.isPublished())
&& node.getFirstParentOfType(InterfaceSectionNode.class) != null) {
Expand All @@ -77,6 +86,13 @@ private boolean isViolation(TypeDeclarationNode node) {
.allMatch(occurrence -> isWithinType(occurrence, type));
}

private static boolean hasElementsWithUsages(EnumTypeNode node) {
return node.getElements().stream()
.map(EnumElementNode::getNameDeclarationNode)
.map(NameDeclarationNode::getUsages)
.anyMatch(Predicate.not(List::isEmpty));
}

private static boolean isWithinType(NameOccurrence occurrence, Type type) {
DelphiScope scope = occurrence.getLocation().getScope();
while (scope != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,34 @@ void testUnusedTypeWithAttributeShouldNotAddIssue() {
.appendDecl(" end;"))
.verifyNoIssues();
}

@Test
void testUnusedEnumWithUsedElementsShouldNotAddIssue() {
CheckVerifier.newVerifier()
.withCheck(new UnusedTypeCheck())
.onFile(
new DelphiTestUnitBuilder()
.appendImpl("function Foo: Integer;")
.appendImpl("type")
.appendImpl(" TBar = (Baz, Flarp);")
.appendImpl("begin")
.appendImpl(" Result := Ord(Baz) + 123;")
.appendImpl("end;"))
.verifyNoIssues();
}

@Test
void testUnusedEnumWithUnusedElementsShouldAddIssue() {
CheckVerifier.newVerifier()
.withCheck(new UnusedTypeCheck())
.onFile(
new DelphiTestUnitBuilder()
.appendImpl("function Foo: Integer;")
.appendImpl("type")
.appendImpl(" TBar = (Baz, Flarp); // Noncompliant")
.appendImpl("begin")
.appendImpl(" Result := 123;")
.appendImpl("end;"))
.verifyIssues();
}
}