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

S2589 FP and FN: Conditional access and pattern matching #3910

Closed
jjolidon opened this issue Jan 6, 2021 · 3 comments · Fixed by #7750
Closed

S2589 FP and FN: Conditional access and pattern matching #3910

jjolidon opened this issue Jan 6, 2021 · 3 comments · Fixed by #7750
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

@jjolidon
Copy link

jjolidon commented Jan 6, 2021

Description

S2589 triggers a false positive for a null check after a first test with pattern matching.

Repro steps

private void Test(Exception ex)
{
    if (ex?.Data["FOO"] is string myString)
    {
        // intentionally empty for the reproducer
    }
    else
    {
        if (ex != null) // This test triggers S2589
        {
            // intentionally empty for the reproducer
        }
    }
}

Expected behavior

The above code should not trigger S2589

Actual behavior

The above code triggers S2589

Known workarounds

No known workaround

Related information

  • SonarAnalyzer.CSharp version: 8.13.0.25740
  • Visual Studio version: 16.8.3
  • MSBuild / dotnet version: 16.8.2.56705 / 5.0.101
  • SonarScanner for .NET version (if used)
  • Operating System: Win 10
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title S2589 FP after test with pattern matching S2589 FP after test with conditional access and pattern matching Jan 8, 2021
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title S2589 FP after test with conditional access and pattern matching Fix S2589 FP: Conditional access and pattern matching Jan 8, 2021
@pavel-mikula-sonarsource
Copy link
Contributor

Hi @jjolidon ,

Thank you for reporting this case. I can confirm it as False Positive.

@pavel-mikula-sonarsource pavel-mikula-sonarsource added Area: C# C# rules related issues. Area: CFG/SE CFG and SE related issues. Type: False Positive Rule IS triggered when it shouldn't be. labels Jan 8, 2021
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title Fix S2589 FP: Conditional access and pattern matching Fix S2589 FP and FN: Conditional access and pattern matching Jan 8, 2021
@andrei-epure-sonarsource
Copy link
Contributor

other conditional access FPs: #3416 , #2528, #2704

@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: CFG/SE FPs Rule IS triggered when it shouldn't be for CFG and SE rules. and removed Type: False Positive Rule IS triggered when it shouldn't be. labels Jun 25, 2021
@andrei-epure-sonarsource andrei-epure-sonarsource changed the title Fix S2589 FP and FN: Conditional access and pattern matching S2589 FP and FN: Conditional access and pattern matching Oct 14, 2022
@mary-georgiou-sonarsource mary-georgiou-sonarsource added this to the 9.7 milestone Jul 24, 2023
@martin-strecker-sonarsource martin-strecker-sonarsource modified the milestones: 9.7, 9.8 Aug 4, 2023
@mary-georgiou-sonarsource
Copy link
Contributor

Fixed by #7750

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.

5 participants