File tree Expand file tree Collapse file tree 3 files changed +31
-2
lines changed
main/java/au/com/integradev/delphi/checks
test/java/au/com/integradev/delphi/checks Expand file tree Collapse file tree 3 files changed +31
-2
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 3232import org .sonar .plugins .communitydelphi .api .ast .PrimaryExpressionNode ;
3333import org .sonar .plugins .communitydelphi .api .check .DelphiCheck ;
3434import org .sonar .plugins .communitydelphi .api .check .DelphiCheckContext ;
35+ import org .sonar .plugins .communitydelphi .api .symbol .declaration .PropertyNameDeclaration ;
3536import org .sonar .plugins .communitydelphi .api .symbol .declaration .RoutineNameDeclaration ;
3637import org .sonar .plugins .communitydelphi .api .symbol .declaration .VariableNameDeclaration ;
3738import org .sonar .plugins .communitydelphi .api .type .Type ;
39+ import org .sonar .plugins .communitydelphi .api .type .Typed ;
3840
3941@ Rule (key = "ObjectPassedAsInterface" )
4042public 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 ));
Original file line number Diff line number Diff 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 ()
You can’t perform that action at this time.
0 commit comments