Description
S2589 is erroneously raised on var-type pattern matches in e.g. switch expressions that implement the condition using a when clause.
Repro steps
[Flags]
public enum Flags
{
None,
Foo,
Bar
}
// ...
public int Test(Flags flags)
{
return flags switch
{
var value when value.HasFlag(Flags.Foo) => 1,
var value when value.HasFlag(Flags.Bar) => 2,
_ => 0
};
}
Expected behavior
Rule S2589 is not raised.
Actual behavior
Rule S2589 is raised on both var value when pattern matches.
Known workarounds
Disable or suppress S2589.
Related information
- C#/VB.NET Plugins version: 9.8 (via SonarLint v7.3.0.77872)
- Visual Studio version: VS2022 v17.7.4
- MSBuild / dotnet version: .NET SDK 7.0.401
- SonarScanner for .NET version (if used): N/A
- Operating System: Windows 11