SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
GNU Lesser General Public License v3.0
775 stars 226 forks source link

Fix S2589 FP: Rule ignores case guards #9663

Open manpil opened 1 week ago

manpil commented 1 week ago

Description

S2589 is raised on a switch expression on a tuple where the second parameter is discarded but for the first one there is a guard clause.

Repro steps

 private static string Example(string input, bool y)
    {
        return (input, y) switch
        {
            ("Foo", true) => "FooTrue",
            ("Foo", false) => "FooFalse",
            (var x, true) when x.StartsWith("ab") => "AbTrue",
            (var x, _) when x.StartsWith("CD") => "CD",
            _ => "empty",
        };
    }

Expected behavior

S2589 is not raised

Actual behavior

S2589 is raised on the line containing: (var x, _) when x.StartsWith("CD") => "CD",

Known workarounds

None/ Disable with pragma

Related information

Kindof relates to: https://github.com/SonarSource/sonar-dotnet/issues/8008

sebastien-marichal commented 5 days ago

Hello @manpil,

Thank you for reporting this issue. I confirm this is a false positive.

I am adding this issue to our backlog to tackle it in the future.

Have a good day!