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 FN: Support recursive pattern redundant null check #9203

Open
jnyrup opened this issue Apr 25, 2024 · 2 comments
Open

Fix S2589 FN: Support recursive pattern redundant null check #9203

jnyrup opened this issue Apr 25, 2024 · 2 comments
Labels
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.

Comments

@jnyrup
Copy link

jnyrup commented Apr 25, 2024

Description

When using a property pattern is { } as a null check S2589 does not recognize it as redundant.
Is that a bug or a deliberate design decision?

Repro steps

public class Class1
{
    public void A(string s)
    {
        _ = s.GetHashCode();
        if (s is not null) { /* Ignore */ } // TP
    }

    public void B(string s)
    {
        _ = s.GetHashCode();
        if (s != null) { /* Ignore */ } // TP
    }

    public void C(string s)
    {
        _ = s.GetHashCode();
        if (!ReferenceEquals(s, null)) { /* Ignore */ } // TP
    }

    public void E(string s)
    {
        _ = s.GetHashCode();
        if (s is { }) { /* Ignore */ } // FN: null check is not considered redundant
    }
}

Expected behavior

E() should report S2589

Actual behavior

E() does not report S2589

Known workarounds

Use is not null instead of is {} to trigger S2589

Related information

  • C#/VB.NET Plugins version
  • Visual Studio version: 17.10.0 Preview 5.0
  • MSBuild / dotnet version: 8.0
  • SonarLint for Visual Studio 2022 7.8.0.88494
  • Operating System: Windows 10
@gregory-paidis-sonarsource gregory-paidis-sonarsource changed the title Fix S2589 FN: Property pattern is not recognized as redundant null check Fix S2589 FN: Support recursive pattern redundant null check May 1, 2024
@gregory-paidis-sonarsource gregory-paidis-sonarsource added 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. and removed Area: C# C# rules related issues. labels May 1, 2024
@gregory-paidis-sonarsource
Copy link
Contributor

Hey,

That is a good find.
As you might know, recursive pattern can be tricky, as it can appear in many, many forms.
I will document it with a reproducer in our unit tests, and it will be taken care of at some point in the future, when we tackle Symbolic Execution FP/FNs.

Thanks for raising the issue.

PS: FluentAssertions is awesome!

@gregory-paidis-sonarsource gregory-paidis-sonarsource removed their assignment May 1, 2024
@jnyrup
Copy link
Author

jnyrup commented May 1, 2024

Thanks for looking into this and for the kind words regarding Fluent Assertions

As you might know, recursive pattern can be tricky, as it can appear in many, many forms.

Yeah, I did think of the many forms recursive patterns can take.
Without having the faintest insights into your analyzers, I wondered if perhaps is { } could somehow be special cased to be equivalent to a null check and then (for now) ignore the more complex patterns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

No branches or pull requests

2 participants