Skip to content

Commit 0d84d74

Browse files
fourlscirras
authored andcommitted
Correctly handle Variants in FormatArgumentType
1 parent 1f4b5f1 commit 0d84d74

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ private static boolean isAcceptedType(Type exprType, FormatSpecifierType specifi
8989
case STRING:
9090
return exprType.isString()
9191
|| exprType.isChar()
92+
|| exprType.isVariant()
9293
|| ((exprType instanceof PointerType)
9394
&& ((PointerType) exprType).dereferencedType().isChar());
9495
default:

delphi-checks/src/main/resources/org/sonar/l10n/delphi/rules/community-delphi/FormatArgumentType.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ <h2>Why is this an issue?</h2>
77
<li><code>%d</code>, <code>%u</code>, and <code>%x</code> must be an integer value</li>
88
<li><code>%e</code>, <code>%f</code>, <code>%g</code>, <code>%n</code>, and <code>%m</code> must be a floating-point value</li>
99
<li><code>%p</code> must be a pointer value</li>
10-
<li><code>%s</code> must be a string, character, or <code>PChar</code> value</li>
10+
<li><code>%s</code> must be a string, character, <code>PChar</code>, or variant value</li>
1111
<li>Floating-point width and precision must be integer values</li>
1212
</ul>
1313
<h2>How to fix it</h2>

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,43 @@ void testWrongNumberArgumentsShouldNotAddIssue() {
242242
.verifyNoIssues();
243243
}
244244

245+
@Test
246+
void testVariantForStringShouldNotAddIssue() {
247+
CheckVerifier.newVerifier()
248+
.withCheck(new FormatArgumentTypeCheck())
249+
.withStandardLibraryUnit(sysUtils())
250+
.onFile(
251+
new DelphiTestUnitBuilder()
252+
.appendImpl("uses System.SysUtils;")
253+
.appendImpl("var")
254+
.appendImpl(" MyVariant: Variant;")
255+
.appendImpl("initialization")
256+
.appendImpl(" Format('I got %s', [")
257+
.appendImpl(" MyVariant")
258+
.appendImpl(" ]);"))
259+
.verifyNoIssues();
260+
}
261+
262+
@Test
263+
void testVariantForNonStringShouldAddIssue() {
264+
CheckVerifier.newVerifier()
265+
.withCheck(new FormatArgumentTypeCheck())
266+
.withStandardLibraryUnit(sysUtils())
267+
.onFile(
268+
new DelphiTestUnitBuilder()
269+
.appendImpl("uses System.SysUtils;")
270+
.appendImpl("var")
271+
.appendImpl(" MyVariant: Variant;")
272+
.appendImpl("initialization")
273+
.appendImpl(" Format('%d %n %x %p', [")
274+
.appendImpl(" MyVariant, // Noncompliant")
275+
.appendImpl(" MyVariant, // Noncompliant")
276+
.appendImpl(" MyVariant, // Noncompliant")
277+
.appendImpl(" MyVariant // Noncompliant")
278+
.appendImpl(" ]);"))
279+
.verifyIssues();
280+
}
281+
245282
@Test
246283
void testCorrectProcedureResultShouldNotAddIssue() {
247284
CheckVerifier.newVerifier()

0 commit comments

Comments
 (0)