-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Should null-check/assert patterns produce a warning on a constant with a nullable type? #59871
Comments
As mentioned here, these warnings are not part of the language specification, but they are recommended. Just to state an opinion, it seems reasonable for the warning to be emitted based on the static type of the expression, not on the actual value (just like non-constant expressions where we must rely on the static type). On the other hand, it might also be useful to emit a diagnostic message (a lint, probably) in the case where a constant variable declaration has an explicit type annotation, and the type is a proper supertype of the run-time type of the value. Similarly for a final variable where the lint could flag declarations where the type annotation is a proper supertype of the static type of the initializing expression. This means that we'd lint det declaration Then again, this might not be a good idea at all: If the type annotation is a widely used public type So I'd tend to prefer that the warning relies on the type annotation (not the actual value), and the fact that the type annotation is a proper supertype should not be linted, because it isn't necessarily a code smell. @bwilkerson, WDYT? |
I agree that the warnings seem correct and intended. The The matched value type is the type of |
No warning if the type of |
If the matched value type is nullable, then a pattern of I don't think it's a warning we have recommended (don't remember), but it's OK to warm about it as unnecessary code. The pattern But this should be answered by the analyzer people who decided which warnings to give. |
We currently aren't producing a warning for the case of a nullable value and a null-check pattern: const int one = 1;
void f(int? x) {
if (x case one?) {}
} We should consider adding that case to the test even though it would be a breaking change. All of the other cases are intentional. |
Analyzer produces a warning in case of use of null-check and null-asser patterns on a constant of nullable type.
Please confirm if it is really intended.
cc @eernstg
The text was updated successfully, but these errors were encountered: