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
};
}