Skip to content

IDE0072: False positive when using switch expression on integerΒ #66433

@omarjuul

Description

@omarjuul

Version Used:
.NET SDK 7.0.102

Steps to Reproduce:
dotnet build the following:

Program.cs

static string TestSwitch(int foo)
    => foo switch
    {
        0 => "zero",
        < 0 => "negative",
        > 0 => "positive",
    };

ConsoleApp0.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
  </PropertyGroup>

</Project>

.editorconfig

[*.cs]
dotnet_diagnostic.IDE0072.severity = warning

Diagnostic Id: IDE0072

Expected Behavior:

IDE0072 is not raised: all cases are handled

Actual Behavior:
IDE0072 is raised. The suggested fix is to add a default arm. When I do that, I get a message that (correctly) informs me that The pattern has already been handled by a previous arm of the 'switch' expression

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-IDEBughelp wantedThe issue is "up for grabs" - add a comment if you are interested in working on it

    Type

    No type

    Projects

    Status

    Completed

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions