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 FP S3878: When non-object array is passed to object[] params as first argument #6894

Closed
afpirimoglu opened this issue Mar 9, 2023 · 2 comments · Fixed by #9395
Closed
Assignees
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@afpirimoglu
Copy link

afpirimoglu commented Mar 9, 2023

Description

When the first element of the params argument is an array and the array is created on the same statement as the method call being made, S3878 is raised incorrectly.

Repro steps

public void MethodWithObjectParams(params object[] objectArgs)
{
    // Does something
}

// For this following line S3878 is raised correctly:
this.MethodWithObjectParams(new object[] { new int[] { 1, 2, 3, } });
// But for this next line S3878 is raised incorrectly:
this.MethodWithObjectParams(new int[] { 1, 2, 3, } ); // Should not have caused S3878, but it is!

// workaround: do the array creation before the method call
var intArray = new int[] { 1, 2, 3, };
this.MethodWithObjectParams(intArray); // Does not raise S3878, as it shouldn't.

Expected behavior

if the first parameter being passed is an array that is created on the statement call line, but is not the array of the params argument, but an element of the params argument, S3878 should not have been raised.

Actual behavior

Please see code example above

Known workarounds

Please see code example above

@mary-georgiou-sonarsource
Copy link
Contributor

Hello @afpirimoglu,
Thanks a lot for reporting this, I confirm this is an FP.
I added the issue to our backlog and you can track any progress by following this issue.

@mary-georgiou-sonarsource mary-georgiou-sonarsource changed the title Fix S3878: With a params argument, when the first element of the argument is an array created on the call statement, S3878 is raised incorrectly. Fix FP S3878: When params argument is object [], and the first argument is an array created on the call statement Mar 16, 2023
@mary-georgiou-sonarsource mary-georgiou-sonarsource added Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be. labels Mar 16, 2023
@afpirimoglu
Copy link
Author

@mary-georgiou-sonarsource thanks for looking into this!
I had filed this issue too, which is also a false positive for S3878: #6893
There are situations the params arg have to be called by name in the method call, which then requires it to be passed as an array type.
Is it possible to take a look at that issue also?

@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title Fix FP S3878: When params argument is object [], and the first argument is an array created on the call statement Fix FP S3878: When non-object array is passed to object[] params as first argument Apr 19, 2023
@mary-georgiou-sonarsource mary-georgiou-sonarsource added this to the 9.27 milestone Jun 4, 2024
@martin-strecker-sonarsource martin-strecker-sonarsource removed this from the 9.27 milestone Jun 11, 2024
@mary-georgiou-sonarsource mary-georgiou-sonarsource added this to the 9.28 milestone Jun 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants