Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler doesn't show warning about not exhaustive switch expression #59511

Closed
sungam3r opened this issue Feb 12, 2022 · 9 comments
Closed

Compiler doesn't show warning about not exhaustive switch expression #59511

sungam3r opened this issue Feb 12, 2022 · 9 comments
Labels
Area-Compilers Feature - Nullable Reference Types Nullable Reference Types Resolution-By Design The behavior reported in the issue matches the current design untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@sungam3r
Copy link
Contributor

sungam3r commented Feb 12, 2022

Hi. The issue is described in details here coverlet-coverage/coverlet#1292 so I will not repeat. The desired behavior - compiler shows warning about not exhaustive switch expression.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Interactive untriaged Issues and PRs which have not yet been triaged by a lead labels Feb 12, 2022
@CyrusNajmabadi
Copy link
Member

I looked through the linked issue and I didn't get exactly what the issue is. Can you please restate it here with a clear code example, and an explanation of what behavior you are seeing and what behavior is expected (and why). Thanks!

@sungam3r
Copy link
Contributor Author

public class Program
{
    public static void Main()
    {
        Test1(); // SwitchExpressionException
        Test2(); // SwitchExpressionException too
    }

    private static string? ExpectOneOf1()
    {
        return null;
    }

    private static string ExpectOneOf2()
    {
        return null!;
    }

    private static bool Test1()
    {
        return ExpectOneOf1() switch // CS8655
        {
            "a" => true,
            string _ => false
        };
    }

    private static bool Test2()
    {
        return ExpectOneOf2() switch // no warning
        {
            "a" => true,
            string _ => false
        };
    }
}
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

I would like to see compiler warning in both cases.

@Youssef1313
Copy link
Member

I think the compiler relies on the annotation for knowing if null is covered or not. So the problem is that ExpectOneOf2 is returning string. So this is pretty much by-design.

@sungam3r
Copy link
Contributor Author

In both cases compiler emits default switch branch throwing SwitchExpressionException so I expect warning in both cases too. Does it make sense?

@sungam3r sungam3r changed the title Compiler doesn't show warning about not exhaustive swith expression Compiler doesn't show warning about not exhaustive switch expression Feb 12, 2022
@sungam3r
Copy link
Contributor Author

sungam3r commented Feb 12, 2022

As I understood the warning necessary in order to show the presence of a default branch with runtime exception that exists when using not exhaustive switch expression. Since this branch exists in both cases, the warning should also be in both cases.

@Youssef1313
Copy link
Member

@RikkiGibson What do you think?

Related to #52714

@CyrusNajmabadi
Copy link
Member

I think this is by design. The switch case is there for actually badly behaving code. But we trust the annotation to not force the user to handle a pointless case in the majority of cases.

@sungam3r
Copy link
Contributor Author

The switch case is there

Do you mean default case that throws SwitchExpressionException ?

Thanks. The explanation is clear, although I am not sure that the choice of such a design was the best solution.

@RikkiGibson
Copy link
Contributor

It is by design for the same reason the following is by design

#nullable enable

var c = C.GetC();
c.M(); // no warning

public class C
{
    public static C GetC()
    {
        return null!;
    }
    
    public void M()
    {
    }    
}

Simply put. There is one place where the warning is "naturally" given. But the warning is suppressed by '!'. We don't use our analysis to encourage users to "distrust" methods which claim to return not null. We use it to ask methods which return not null to really return not null to the best of their knowledge--unless they shut us up.

@jcouv jcouv added Feature - Nullable Reference Types Nullable Reference Types Resolution-By Design The behavior reported in the issue matches the current design Area-Compilers and removed Area-Interactive labels Feb 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Feature - Nullable Reference Types Nullable Reference Types Resolution-By Design The behavior reported in the issue matches the current design untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

5 participants