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 S2259 FP: SE engine doesn't take into account element existence collection methods #8266

Closed
sebastien-marichal opened this issue Oct 27, 2023 · 3 comments · Fixed by #9505
Assignees
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Sprint: Hardening Fix FPs/FNs/improvements Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Milestone

Comments

@sebastien-marichal
Copy link
Contributor

sebastien-marichal commented Oct 27, 2023

From this community post.

This might be fixed as part of #7457:

Learn from well-known (extension-) methods like list.Any()

void Method(Exception[] array)
{
    if (array.Any())
    {
        Exception exception = array.FirstOrDefault();
        Console.WriteLine(exception.Message); // Noncompliant - FP if Any is true, FirstOrDefault will never return null
    }
}
@sebastien-marichal sebastien-marichal added Type: False Negative Rule is NOT triggered when it should be. Area: CFG/SE CFG and SE related issues. Area: C# C# rules related issues. labels Oct 27, 2023
@sebastien-marichal sebastien-marichal added Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. and removed Type: False Negative Rule is NOT triggered when it should be. labels Oct 30, 2023
@Tim-Pohlmann
Copy link
Contributor

This requires the mentioned step from #7457 + extra logic for FirstOrDefault. Should be straight-forward after #7866

@mary-georgiou-sonarsource
Copy link
Contributor

@Tim-Pohlmann @sebastien-marichal For the record, this example is a true positive, as FirstOrDefault can return null even if the collection is not empty for reference types.

@Tim-Pohlmann
Copy link
Contributor

@mary-georgiou-sonarsource That's a Good catch. The engine needs to learn unknown and not NotNull here. This means it is still an FP because S2259 does not raise on unknown.

@mary-georgiou-sonarsource mary-georgiou-sonarsource changed the title Fix S2259 FP: SE engine doesn't take into account collection extension method Any Fix S2259 FP: SE engine doesn't take into account element existence collection methods Jul 5, 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. Area: CFG/SE CFG and SE related issues. Sprint: Hardening Fix FPs/FNs/improvements Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Projects
None yet
5 participants