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: || and property patterns #7096

Closed
jnyrup opened this issue Apr 18, 2023 · 2 comments · Fixed by #7750
Closed

Fix S2589 FP: || and property patterns #7096

jnyrup opened this issue Apr 18, 2023 · 2 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

@jnyrup
Copy link

jnyrup commented Apr 18, 2023

Description

While rewriting some older code to using pattern matching it triggered S2589 when using || together with a property pattern.

obj is A { Value: true } || obj is B { Value: true };

When rewriting the disjunction with a logical pattern using or S2589 disappeared despite both lowered and compiled code are identical.

obj is A { Value: true } or B { Value: true };

Repro steps

public interface I { }

public class A : I { }

public class B : I { }

class C
{
    public bool Foo(I obj) => obj is A { } || obj is B { }; // Triggers S2589 on "obj is B { }"

    public bool Bar(I obj) => obj is A || obj is B; // No warnings

    public bool Baz(I obj) => obj is A { } or B { }; // No warnings
}

Expected behavior

S2589 is not triggered for Foo

Actual behavior

S2589 is triggered for Foo.

Known workarounds

Rewrite Foo to Baz.

Related information

  • C#/VB.NET Plugins version: SonarLint for Visual Studio 2022 6.15.0.67199
  • Visual Studio version: 2022 17.6.0 Preview 3.0
  • MSBuild / dotnet version: .NET 6
  • SonarScanner for .NET version (if used)
  • Operating System: Windows 10
@pavel-mikula-sonarsource
Copy link
Contributor

Hi @jnyrup,

Thank you for reporting this case. I confirm it as FP. We plan to reimplement the rule soon and that should address the problem.

@pavel-mikula-sonarsource pavel-mikula-sonarsource added 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. labels Apr 19, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title S2589 FP: || and property patterns Fix S2589 FP: || and property patterns Apr 19, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource added the Area: C# C# rules related issues. label Apr 19, 2023
antonioaversa pushed a commit that referenced this issue Apr 24, 2023
@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.

4 participants