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: Engine does not learn NotNull from LINQ methods #8907

Closed
Tim-Pohlmann opened this issue Mar 12, 2024 · 1 comment · Fixed by #9414
Closed

Fix S2259 FP: Engine does not learn NotNull from LINQ methods #8907

Tim-Pohlmann opened this issue Mar 12, 2024 · 1 comment · Fixed by #9414
Assignees
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules 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.

Comments

@Tim-Pohlmann
Copy link
Contributor

Tim-Pohlmann commented Mar 12, 2024

Most LINQ methods like .Count() do throw when the collection is null. The engine should learn NotNull when a method like this is invoked to avoid FPs (e.g., for S2259).

private void Foo(string[] values)
{
    if (values == null)
    {
        Console.WriteLine("Oops!");
    }
    if (5 < values.Count())         // FN, will throw when values is null; separate issue, see #8908
    {
        _ = values[5].ToString();   // Noncompliant FP, we do not learn NotNull from LINQ methods
    }
}

Related: #8908

@Tim-Pohlmann Tim-Pohlmann added Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues. Area: C# C# rules related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. labels Mar 12, 2024
Tim-Pohlmann added a commit that referenced this issue Mar 12, 2024
@antonioaversa antonioaversa self-assigned this Jun 6, 2024
@Tim-Pohlmann Tim-Pohlmann added this to the 9.27 milestone Jun 7, 2024
@martin-strecker-sonarsource martin-strecker-sonarsource removed this from the 9.27 milestone Jun 11, 2024
@antonioaversa
Copy link
Contributor

Handing over this work to @Tim-Pohlmann, who will bring it to completion.

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. Area: VB.NET VB.NET rules 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
Development

Successfully merging a pull request may close this issue.

4 participants