Skip to content

Commit

Permalink
Fix S3878 AD0001: Cover the case of CollectionInitializerSyntax for V…
Browse files Browse the repository at this point in the history
…B.NET (#9464)
  • Loading branch information
mary-georgiou-sonarsource authored Jun 26, 2024
1 parent 9c5b2d1 commit 242b97c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ protected override ArgumentSyntax LastArgumentIfArrayCreation(SyntaxNode express
: null;

protected override ITypeSymbol ArrayElementType(ArgumentSyntax argument, SemanticModel model) =>
model.GetTypeInfo(((ArrayCreationExpressionSyntax)argument.GetExpression()).Type).Type;
argument.GetExpression() switch
{
ArrayCreationExpressionSyntax arrayCreation => model.GetTypeInfo(arrayCreation.Type).Type,
CollectionInitializerSyntax collectionInitializer => (model.GetTypeInfo(collectionInitializer).Type as IArrayTypeSymbol)?.ElementType,
_ => null
};

private static ArgumentListSyntax ArgumentList(SyntaxNode expression) =>
expression switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ Public Class Repro6894
' The argument given for a parameter array can be a single expression that is implicitly convertible (§10.2) to the parameter array type.
' In this case, the parameter array acts precisely like a value parameter.
' see: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes#14625-parameter-arrays
Method({"1", "2"}) ' FN
Method(New Object() {New Integer() {1, 2}}) ' FN, elements in args: [System.Int32[]]
Method(New Integer() {1, 2, 3}) ' Compliant, Elements in args: [System.Int32[]]
Method(New String() {"1", "2"}, New String() {"1", "2"}) ' Compliant, elements in args: [System.String[], System.String[]]
Method(New String() {"1", "2"}, New Integer() {1, 2}) ' Compliant, elements in args: [System.String[], System.Int32[]]
MethodArray(New String() {"1", "2"}, New String() {"1", "2"}) ' Compliant, elements in args: [System.String[], System.String[]]
MethodArray(New Integer() {1, 2}, New Integer() {1, 2}) ' Compliant, elements in args: [System.Int32[], System.Int32[]]
MethodArray({1, 2}, {1, 2}) ' Compliant, elements in args: [System.Int32[], System.Int32[]]

MethodJaggedArray(New Integer() {1, 2}) ' Compliant: jagged array [System.Object[]]
End Sub
Expand Down

0 comments on commit 242b97c

Please sign in to comment.