Skip to content

Commit

Permalink
Correctly handle Variants in FormatArgumentType
Browse files Browse the repository at this point in the history
  • Loading branch information
fourls authored and Cirras committed Feb 1, 2024
1 parent 1f4b5f1 commit 0d84d74
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ private static boolean isAcceptedType(Type exprType, FormatSpecifierType specifi
case STRING:
return exprType.isString()
|| exprType.isChar()
|| exprType.isVariant()
|| ((exprType instanceof PointerType)
&& ((PointerType) exprType).dereferencedType().isChar());
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h2>Why is this an issue?</h2>
<li><code>%d</code>, <code>%u</code>, and <code>%x</code> must be an integer value</li>
<li><code>%e</code>, <code>%f</code>, <code>%g</code>, <code>%n</code>, and <code>%m</code> must be a floating-point value</li>
<li><code>%p</code> must be a pointer value</li>
<li><code>%s</code> must be a string, character, or <code>PChar</code> value</li>
<li><code>%s</code> must be a string, character, <code>PChar</code>, or variant value</li>
<li>Floating-point width and precision must be integer values</li>
</ul>
<h2>How to fix it</h2>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,43 @@ void testWrongNumberArgumentsShouldNotAddIssue() {
.verifyNoIssues();
}

@Test
void testVariantForStringShouldNotAddIssue() {
CheckVerifier.newVerifier()
.withCheck(new FormatArgumentTypeCheck())
.withStandardLibraryUnit(sysUtils())
.onFile(
new DelphiTestUnitBuilder()
.appendImpl("uses System.SysUtils;")
.appendImpl("var")
.appendImpl(" MyVariant: Variant;")
.appendImpl("initialization")
.appendImpl(" Format('I got %s', [")
.appendImpl(" MyVariant")
.appendImpl(" ]);"))
.verifyNoIssues();
}

@Test
void testVariantForNonStringShouldAddIssue() {
CheckVerifier.newVerifier()
.withCheck(new FormatArgumentTypeCheck())
.withStandardLibraryUnit(sysUtils())
.onFile(
new DelphiTestUnitBuilder()
.appendImpl("uses System.SysUtils;")
.appendImpl("var")
.appendImpl(" MyVariant: Variant;")
.appendImpl("initialization")
.appendImpl(" Format('%d %n %x %p', [")
.appendImpl(" MyVariant, // Noncompliant")
.appendImpl(" MyVariant, // Noncompliant")
.appendImpl(" MyVariant, // Noncompliant")
.appendImpl(" MyVariant // Noncompliant")
.appendImpl(" ]);"))
.verifyIssues();
}

@Test
void testCorrectProcedureResultShouldNotAddIssue() {
CheckVerifier.newVerifier()
Expand Down

0 comments on commit 0d84d74

Please sign in to comment.