Switch expression S2589 False postive or Documentation gap

Make sure to read this post before raising a thread here:

Then tell us:

  • What language is this for? C#
  • Which rule? S2589
  • Why do you believe it’s a false-positive/false-negative? False positive
  • Are you using Nuget: Include=“SonarAnalyzer.CSharp” Version=“9.10.0.77988” Not connected to any other tooling.
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)

In the code below, I don’t particularly agree that (_, _) is a major code smell. But can see it might be up for debate. I would suggest at least adding to the documentation that this can happen. I pretty much found the fix for this by accident and was pretty frustrated with this warning before.

    public string MapStrings(string input1, string input2)
    {
        return (input1, input2) switch
        {
            ("Foo", "hello") => "Foo out",
            ("Bar", "hello") => "Bar out",
            (_, _) => throw new InvalidOperationException("Unsupported string"), // <<--- S2589 on '(_, _)'
        };
    }

    public string MapStrings2(string input1, string input2)
    {
        return (input1, input2) switch
        {
            ("Foo", "hello") => "Foo out",
            ("Bar", _) => "Bar out",
            _ => throw new InvalidOperationException("Unsupported string"), // <<---- OK
        };
    }

Hello @mjconrad,

Thank you for reporting this.
I confirm it’s an FP.

Although the rule is set to not raise for the discard pattern when it’s in switch, it is not handling properly the tuple.

Please follow this issue on our git that is related to the issue with the tuples.

Thanks!