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

CS8655 (exhaustive enum) should be triggered for nullable enum values #76817

Open
juwens opened this issue Jan 20, 2025 · 3 comments
Open

CS8655 (exhaustive enum) should be triggered for nullable enum values #76817

juwens opened this issue Jan 20, 2025 · 3 comments
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@juwens
Copy link

juwens commented Jan 20, 2025

Version Used: C#12

Steps to Reproduce:

  1. compile the sample code or test it in sharplab
#pragma warning disable CS8524

enum MyEnum { A }

class C
{
    void M()
    {
        bool? b = null;
        
        _ = b switch // <= correctly warns about missing null arm with CS8655
        {
            true => 1,
            false => 2,
        };
        
        
        MyEnum? x = null;
        
        _ = x switch // <= does NOT warn about missing null arm with CS8655
        {
            MyEnum.A => "A",
        };
    }
}

Diagnostic Id: CS8655: The switch expression does not handle some null inputs (it is not exhaustive).

If this is a report about a bug in an analyzer, please include the diagnostic ID and message if possible (e.g. "IDE0030: Use coalesce expression").

Expected Behavior: nullable enum-variable switch-expression with missing null should yield CS8655

Actual Behavior: nullable enum-variable switch-expression with missing null does NOT yield CS8655

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jan 20, 2025
@juwens juwens changed the title CS8655 (exhaustive enum) is not triggered for nullable num value CS8655 (exhaustive enum) should be triggered for nullable enum values Jan 20, 2025
@jcouv

This comment has been minimized.

@jcouv jcouv added the Resolution-By Design The behavior reported in the issue matches the current design label Jan 21, 2025
@jjonescz
Copy link
Member

jjonescz commented Jan 22, 2025

@jcouv I don't think that's the issue here. The problem is that CS8655 is reported for bool? but not MyEnum?. CS8524 is reported for MyEnum? for the reason you describe. So perhaps the compiler does not report CS8655 anymore to avoid "repeating itself". But if customers suppress CS8524 (because they don't want to be warned about missing enum cases because they know they don't cast ints to enums), they won't get the CS8655 warning there...

@juwens
Copy link
Author

juwens commented Jan 22, 2025

@jcouv I don't think that's the issue here. The problem is that CS8655 is reported for bool? but not MyEnum?. CS8524 is reported for MyEnum? for the reason you describe. So perhaps the compiler does not report CS8655 anymore to avoid "repeating itself". But if customers suppress CS8524 (because they don't want to be warned about missing enum cases because they know they don't cast ints to enums), they won't get the CS8655 warning there...

correct. CS8524 isn't involved with the null-check at all. With CS8524 enabled, one should see 8655 and 8524, cause CS8524 only indicates "malicious" (MyEnum)42 values, but not a missing arm for null.
That the default _ => ... arm "fixes" both warnings is just a coincidence.

@jcouv jcouv removed the Resolution-By Design The behavior reported in the issue matches the current design label Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

3 participants