-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Disallow default
as a constant pattern.
#23499
Comments
Just to clarify the proposal is to produce an error instead of the current warning: |
Yes. Clarified above. |
Should we make this change in 15.6 or 15.7 instead, to reduce the impact of breaking change? Default expressions where introduced in C# 7.1. |
That is a question for the compat council. |
I'm not on that thread ;-) |
For 15.6, the messages are modified as we do not have a discard pattern. For the switch, we recommend the |
Has been reviewed by LDM and compat council. Fixes dotnet#23499
* Forbid 'default' as a case constant or a pattern. Fixes #23499
I disagree. I see no reason why it is necessary to break my code after fixing CS8313 re-introduces code edits that were happily a thing of the past changing the type of a thing. And the remark You won't accidentally write |
- fix error CS8363 [disallow `default` as a constant pattern](dotnet/roslyn#23499) in tests
The suggested solution will of course not work for generic types without a class or a struct constraint |
is it too late to also disallow |
The LDM has tentatively decided to disallow a
default
keyword (literal) as a constant pattern, in order to reduce confusion with "default" cases in switch statements and expressions. A programmer can always writenull
,0
, or'\0'
instead. This is technically a breaking change.In a switch statement, the case label
case default:
is proposed to produce an error something like
error CS8313: A default literal 'default' is not valid as a pattern. Use another literal (e.g. '0' or 'null') as appropriate. To match everything, use a discard pattern '_'.
We already produce approximately this diagnostic, but as a warning.
In an is-pattern expression and other places where patterns appear
e is default
is proposed to produce an error something like
error CS8405: A default literal 'default' is not valid as a pattern. Use another literal (e.g. '0' or 'null') as appropriate. To match everything, use a discard pattern '_'.
This will need review from the compatibility council.
The text was updated successfully, but these errors were encountered: