Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S3878 AD0001: Cover the case of ImplicitArrayCreationExpressionSyntax #9456

Merged
merged 8 commits into from
Jun 24, 2024

Conversation

mary-georgiou-sonarsource
Copy link
Contributor

No description provided.

Copy link
Contributor

@Tim-Pohlmann Tim-Pohlmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The switch can be simplified, looks good otherwise.

Comment on lines 43 to 48
argument switch
{
_ when argument.Expression as ArrayCreationExpressionSyntax is { } arrayCreation => model.GetTypeInfo(arrayCreation.Type.ElementType).Type,
_ when argument.Expression as ImplicitArrayCreationExpressionSyntax is { } implicitArrayCreation => model.GetTypeInfo(implicitArrayCreation).Type,
_ => null
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
argument switch
{
_ when argument.Expression as ArrayCreationExpressionSyntax is { } arrayCreation => model.GetTypeInfo(arrayCreation.Type.ElementType).Type,
_ when argument.Expression as ImplicitArrayCreationExpressionSyntax is { } implicitArrayCreation => model.GetTypeInfo(implicitArrayCreation).Type,
_ => null
};
argument.Expression switch
{
ArrayCreationExpressionSyntax arrayCreation => model.GetTypeInfo(arrayCreation.Type.ElementType).Type,
ImplicitArrayCreationExpressionSyntax implicitArrayCreation => model.GetTypeInfo(implicitArrayCreation).Type,
_ => null
};

argument switch
{
_ when argument.Expression as ArrayCreationExpressionSyntax is { } arrayCreation => model.GetTypeInfo(arrayCreation.Type.ElementType).Type,
_ when argument.Expression as ImplicitArrayCreationExpressionSyntax is { } implicitArrayCreation => model.GetTypeInfo(implicitArrayCreation).Type,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong. It does return the array type (e.g., int[]), but the method is called ArrayElementType (which would be int). It should be (model.GetTypeInfo(implicitArrayCreation).Type as IArrayTypeSymbol)?.ElementType

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe more UTs are required then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the UTs cover differences in the array rank? I don't think so, and maybe the implementation as well:

void Method(params object[] args) { }

Method(new object[] { null }); // Noncompliant
// These cases might be FPs
Method(new object[,] { { null } }); // Compliant
Method(new[,] { { new object() } }); // Compliant

// This should work
Method(new object[][] { new[] { new object() } }); // Compliant

params array must be single-dimensional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cover only jagged arrays.

string.Format(CultureInfo.InvariantCulture, "{0}.{1}", new object[] { "", new object() });
MethodImplicitArray(new[] { "Hello", "Hi" }); // Noncompliant , Implicit array creation
Method(new [] { 1, 2, 3, }); // Compliant, Elements in args: [System.Int32[]]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Method(new [] { 1, 2, 3, }); // Compliant, Elements in args: [System.Int32[]]
Method(new[] { 1, 2, 3, }); // Compliant, Elements in args: [System.Int32[]]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add one more test with a multidimensional array

Suggested change
Method(new [] { 1, 2, 3, }); // Compliant, Elements in args: [System.Int32[]]
Method(new[] { 1, 2, 3, }); // Compliant, Elements in args: [System.Int32[]]
Method(new[,] { { 1, 2 } }); // Compliant, Elements in args: [System.Int32[,]]

Copy link

sonarcloud bot commented Jun 24, 2024

Copy link

sonarcloud bot commented Jun 24, 2024

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved, because the cases left are edge cases, but there seems to be something wrong with the implementation. I think there is some special handling with object[] params and I'm not sure, we handle multi-dimensional arrays properly.
You may want to have another look later.

MethodImplicitArray(new[] { "Hello", "Hi" }); // Noncompliant , Implicit array creation
Method(new[] { 1, 2, 3, }); // Compliant, Elements in args: [System.Int32[]]
Method(new[,] { { 1, 2 } }); // Compliant, Elements in args: [System.Int32[,]]
Method(new object[] { null }); // Compliant

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is Method(new String[] { "1", "2" }); Noncompliant, while Method(new object[] { null }); is compliant? This looks like an FN to me.

@mary-georgiou-sonarsource mary-georgiou-sonarsource merged commit 9c5b2d1 into master Jun 24, 2024
28 checks passed
@mary-georgiou-sonarsource mary-georgiou-sonarsource deleted the mary/ad0001 branch June 24, 2024 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants