Skip to content

Commit 6cfefef

Browse files
cirrasfourls
authored andcommitted
Include property expressions in ObjectPassedAsInterface
1 parent e3743d7 commit 6cfefef

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
### Changed
1515

1616
- Exclude common non-ref-counted interface implementations by default in `ObjectPassedAsInterface`.
17+
- Handle property references in `ObjectPassedAsInterface` (in addition to variables/fields).
1718

1819
### Fixed
1920

delphi-checks/src/main/java/au/com/integradev/delphi/checks/ObjectPassedAsInterfaceCheck.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
import org.sonar.plugins.communitydelphi.api.ast.PrimaryExpressionNode;
3333
import org.sonar.plugins.communitydelphi.api.check.DelphiCheck;
3434
import org.sonar.plugins.communitydelphi.api.check.DelphiCheckContext;
35+
import org.sonar.plugins.communitydelphi.api.symbol.declaration.PropertyNameDeclaration;
3536
import org.sonar.plugins.communitydelphi.api.symbol.declaration.RoutineNameDeclaration;
3637
import org.sonar.plugins.communitydelphi.api.symbol.declaration.VariableNameDeclaration;
3738
import org.sonar.plugins.communitydelphi.api.type.Type;
39+
import org.sonar.plugins.communitydelphi.api.type.Typed;
3840

3941
@Rule(key = "ObjectPassedAsInterface")
4042
public class ObjectPassedAsInterfaceCheck extends DelphiCheck {
@@ -91,11 +93,12 @@ private boolean isObjectReferenceExpression(ExpressionNode expression) {
9193
}
9294

9395
var declaration = ((NameReferenceNode) maybeName).getLastName().getNameDeclaration();
94-
if (!(declaration instanceof VariableNameDeclaration)) {
96+
if (!(declaration instanceof VariableNameDeclaration
97+
|| declaration instanceof PropertyNameDeclaration)) {
9598
return false;
9699
}
97100

98-
Type type = ((VariableNameDeclaration) declaration).getType();
101+
Type type = ((Typed) declaration).getType();
99102

100103
return type.isClass()
101104
&& excludedTypesList.stream().noneMatch(e -> type.is(e) || type.isDescendantOf(e));

delphi-checks/src/test/java/au/com/integradev/delphi/checks/ObjectPassedAsInterfaceCheckTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,31 @@ void testObjectPassedAsInterfaceShouldAddIssue() {
6767
.verifyIssues();
6868
}
6969

70+
@Test
71+
void testQualifiedObjectPassedAsInterfaceShouldAddIssue() {
72+
CheckVerifier.newVerifier()
73+
.withCheck(new ObjectPassedAsInterfaceCheck())
74+
.onFile(
75+
new DelphiTestUnitBuilder()
76+
.appendDecl("type")
77+
.appendDecl(" TFoo = class(TInterfacedObject, IInterface)")
78+
.appendDecl(" end;")
79+
.appendDecl(" IBar = interface")
80+
.appendDecl(" property Foo: TFoo;")
81+
.appendDecl(" end;")
82+
.appendDecl(" TBar = class(TInterfacedObject, IBar)")
83+
.appendDecl(" end;")
84+
.appendDecl("procedure DoThing(Obj: IInterface);")
85+
.appendImpl("procedure Test;")
86+
.appendImpl("var")
87+
.appendImpl(" Intf: IBar;")
88+
.appendImpl("begin")
89+
.appendImpl(" Intf := TBar.Create;")
90+
.appendImpl(" DoThing(Intf.Foo); // Noncompliant")
91+
.appendImpl("end;"))
92+
.verifyIssues();
93+
}
94+
7095
@Test
7196
void testInterfacePassedAsInterfaceShouldNotAddIssue() {
7297
CheckVerifier.newVerifier()

0 commit comments

Comments
 (0)