Skip to content

Commit 242b97c

Browse files
Fix S3878 AD0001: Cover the case of CollectionInitializerSyntax for VB.NET (#9464)
1 parent 9c5b2d1 commit 242b97c

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

analyzers/src/SonarAnalyzer.VisualBasic/Rules/ArrayPassedAsParams.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ protected override ArgumentSyntax LastArgumentIfArrayCreation(SyntaxNode express
3939
: null;
4040

4141
protected override ITypeSymbol ArrayElementType(ArgumentSyntax argument, SemanticModel model) =>
42-
model.GetTypeInfo(((ArrayCreationExpressionSyntax)argument.GetExpression()).Type).Type;
42+
argument.GetExpression() switch
43+
{
44+
ArrayCreationExpressionSyntax arrayCreation => model.GetTypeInfo(arrayCreation.Type).Type,
45+
CollectionInitializerSyntax collectionInitializer => (model.GetTypeInfo(collectionInitializer).Type as IArrayTypeSymbol)?.ElementType,
46+
_ => null
47+
};
4348

4449
private static ArgumentListSyntax ArgumentList(SyntaxNode expression) =>
4550
expression switch

analyzers/tests/SonarAnalyzer.Test/TestCases/ArrayPassedAsParams.vb

+2
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,14 @@ Public Class Repro6894
8282
' The argument given for a parameter array can be a single expression that is implicitly convertible (§10.2) to the parameter array type.
8383
' In this case, the parameter array acts precisely like a value parameter.
8484
' see: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes#14625-parameter-arrays
85+
Method({"1", "2"}) ' FN
8586
Method(New Object() {New Integer() {1, 2}}) ' FN, elements in args: [System.Int32[]]
8687
Method(New Integer() {1, 2, 3}) ' Compliant, Elements in args: [System.Int32[]]
8788
Method(New String() {"1", "2"}, New String() {"1", "2"}) ' Compliant, elements in args: [System.String[], System.String[]]
8889
Method(New String() {"1", "2"}, New Integer() {1, 2}) ' Compliant, elements in args: [System.String[], System.Int32[]]
8990
MethodArray(New String() {"1", "2"}, New String() {"1", "2"}) ' Compliant, elements in args: [System.String[], System.String[]]
9091
MethodArray(New Integer() {1, 2}, New Integer() {1, 2}) ' Compliant, elements in args: [System.Int32[], System.Int32[]]
92+
MethodArray({1, 2}, {1, 2}) ' Compliant, elements in args: [System.Int32[], System.Int32[]]
9193

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

0 commit comments

Comments
 (0)