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 S2583/S2589 FN and S2259 FP: Add support for relational pattern #8493

Closed
pavel-mikula-sonarsource opened this issue Dec 21, 2023 · 0 comments · Fixed by #8500
Closed
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

@pavel-mikula-sonarsource
Copy link
Contributor

pavel-mikula-sonarsource commented Dec 21, 2023

Add support for RelationalPattern in IsPattern implementation.

It should fix this FN in S2583/S25889

        void ArithmeticComparisonAndPattern()
        {
            int? c = null;
            if (c is > 10 and < 100)    // FN
            {
                DoSomething();
            }
        }

and FPs caused by the missing evaluation of NumberConstraint.

    public void PatternMatching_Numbers()
    {
        object o = null;
        var value = 42;
        o = value switch
        {
            < 0 => o.ToString(),            // Noncompliant FP, unreachable
            <= 0 => o.ToString(),           // Noncompliant FP, unreachable
            > 100 => o.ToString(),          // Noncompliant FP, unreachable
            >= 100 => o.ToString(),         // Noncompliant FP, unreachable
            > 0 and < 20 => o.ToString(),   // Noncompliant FP, unreachable
            < 10 or > 90 => o.ToString(),   // Noncompliant FP, unreachable
            > 40 and < 50 => o.ToString(),  // Noncompliant, this is reachable
            _ => o.ToString()               // Noncompliant FP, unreachable
        };
    }
@pavel-mikula-sonarsource pavel-mikula-sonarsource 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 Dec 21, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource added this to the 9.17 milestone Dec 21, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title Fix S2583/S2589 FN and FP: Add support for relational pattern Fix S2583/S2589 FN and S2259 FP: Add support for relational pattern Dec 21, 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.

2 participants