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 S2589 FP: Var pattern should not raise #8326

Closed
Tim-Pohlmann opened this issue Nov 7, 2023 · 0 comments · Fixed by #8421
Closed

Fix S2589 FP: Var pattern should not raise #8326

Tim-Pohlmann opened this issue Nov 7, 2023 · 0 comments · Fixed by #8421
Assignees
Labels
Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules.
Milestone

Comments

@Tim-Pohlmann
Copy link
Contributor

Tim-Pohlmann commented Nov 7, 2023

void Test(int i, object o)
{
    _ = i switch
    {
        1 => 1,
        var other => 2  // Noncompliant FP: var should not raise
    };
            
    _ = o switch
    {
        1 => 1,
        var other => 2  // Compliant: by accident, o has no SymbolicValue and always branches du to #8324
    };
}

The var pattern always matches, which is why it is raising S2589. While this is technically correct (the best kind of correct), it does not make sense to raise here, since the point of the pattern is to be a catch-all.
When implementing an exception, make sure that the rule about unreachable code (S2583) still raises, if necessary:

void Test(int i)
{
    if (i is var x1)            // Noncompliant: unreachable code
        Console.WriteLine();
    else
        Console.WriteLine();    // Secondary
}
@Tim-Pohlmann Tim-Pohlmann added Area: CFG/SE CFG and SE 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 Nov 7, 2023
Tim-Pohlmann added a commit that referenced this issue Nov 7, 2023
Tim-Pohlmann added a commit that referenced this issue Nov 7, 2023
Tim-Pohlmann added a commit that referenced this issue Nov 8, 2023
Co-authored-by: Zsolt Kolbay <121798625+zsolt-kolbay-sonarsource@users.noreply.github.com>
sebastien-marichal pushed a commit that referenced this issue Nov 10, 2023
Co-authored-by: Zsolt Kolbay <121798625+zsolt-kolbay-sonarsource@users.noreply.github.com>
@Tim-Pohlmann Tim-Pohlmann modified the milestones: 9.15, 9.16 Dec 6, 2023
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. 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.

3 participants