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: Should not report in switch expression after a null check #6170

Closed
pavel-mikula-sonarsource opened this issue Oct 6, 2022 · 0 comments · Fixed by #6172
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

First null check should build NotNull constraint in "else" branch. Other checks should take that into account

public string NullCheck(object arg) =>
    arg switch
    {
        null => throw new ArgumentNullException(nameof(arg)),
        { } when IsInstance(arg) => "42",  // This somehow learns possible Null when it's already known to be NotNull
        _ => throw new InvalidOperationException("Unexpected type: " + arg.GetType().Name)  // Noncompliant FP
    };

private static bool IsInstance(object arg) => false;
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