Skip to content

Commit aac3ec8

Browse files
committed
C#: Add generated cast expression for nuint and nint parameter defaults.
1 parent 9390b48 commit aac3ec8

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,19 @@ private static bool ContainsPattern(SyntaxNode node) =>
214214

215215
if (type.SpecialType is SpecialType.None)
216216
{
217-
return ImplicitCast.CreateGenerated(cx, parent, childIndex, type, defaultValue, location);
217+
return ImplicitCast.CreateGeneratedConversion(cx, parent, childIndex, type, defaultValue, location);
218218
}
219219

220220
if (type.SpecialType is SpecialType.System_DateTime)
221221
{
222222
return DateTimeObjectCreation.CreateGenerated(cx, parent, childIndex, type, defaultValue, location);
223223
}
224224

225+
if (type.SpecialType is SpecialType.System_IntPtr || type.SpecialType is SpecialType.System_UIntPtr)
226+
{
227+
return ImplicitCast.CreateGenerated(cx, parent, childIndex, type, defaultValue, location);
228+
}
229+
225230
// const literal:
226231
return Literal.CreateGenerated(cx, parent, childIndex, type, defaultValue, location);
227232
}

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ImplicitCast.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ private void AddOperatorCall(IMethodSymbol method)
5151
)
5252
.FirstOrDefault();
5353

54-
// Creates a new generated expression with an implicit cast added, if needed.
55-
public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, ITypeSymbol type, object value,
54+
/// <summary>
55+
/// Creates a new generated expression with an implicit conversion added.
56+
/// </summary>
57+
public static Expression CreateGeneratedConversion(Context cx, IExpressionParentEntity parent, int childIndex, ITypeSymbol type, object value,
5658
Extraction.Entities.Location location)
5759
{
5860
ExpressionInfo create(ExprKind kind, string? v) =>
@@ -79,7 +81,27 @@ ExpressionInfo create(ExprKind kind, string? v) =>
7981
}
8082
}
8183

82-
// Creates a new expression, adding casts as required.
84+
/// <summary>
85+
/// Creates a new generated cast expression.
86+
/// </summary>
87+
public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, ITypeSymbol type, object value,
88+
Extraction.Entities.Location location)
89+
{
90+
var info = new ExpressionInfo(cx,
91+
AnnotatedTypeSymbol.CreateNotAnnotated(type),
92+
location,
93+
ExprKind.CAST,
94+
parent,
95+
childIndex,
96+
true,
97+
ValueAsString(value));
98+
99+
return new Expression(info);
100+
}
101+
102+
/// <summary>
103+
/// Creates a new expression, adding casts as required.
104+
/// </summary>
83105
public static Expression Create(ExpressionNodeInfo info)
84106
{
85107
var resolvedType = info.ResolvedType;

0 commit comments

Comments
 (0)