Explicit cast should suppress warning for nullable types #2221
-
This is with the version in Preview 2. Consider the following void Foo(string bar)
{
}
void Frob(string? bbb)
{
Foo((string)bbb); // Still generates warnings: uncessesary cast, CS8600, CS8604
} There are many times when a nullable type may need to go into a place that's marked non-nullable. I would expect an explicit cast to work without warnings. It currently does not. Here's my reasoning: an explicit cast is the developer saying "do this and throw if it breaks". It's also cleaner and inline with the language expressing intent. The alternative, of using pragma's around that line to suppress, is far more invasive to the code and makes it harder to read. Here's the pragmas: void Frob(string? bbb)
{
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
#pragma warning disable CS8604 // Possible null reference argument.
Foo(bbb);
#pragma warning restore CS8604 // Possible null reference argument.
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
} I think that's much worse. |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments
-
Why not use this construct to indicate "do this and throw if it breaks"? void Foo(string bar)
{
}
void Frob(string? bbb)
{
Foo(bbb ?? throw new ArgumentNullException());
} |
Beta Was this translation helpful? Give feedback.
-
It's still more code and seems to be more complex to read. That pattern also changes the logic, putting the checking on me. A cast would merely suppress the warning and let the existing logic/checking handle things whatever it might be. Null may actually be okay. |
Beta Was this translation helpful? Give feedback.
-
I despise "case to clear warning" types with the C# style of casts. They are virtually impossible to reliably find in a code base [this is not a problem with C++ type cases (e.g. static_cast(...) ] |
Beta Was this translation helpful? Give feedback.
-
The post-fix Foo(bbb!); // no warnings |
Beta Was this translation helpful? Give feedback.
-
Moving to csharplang as this is a language design question. The language team has already made a decision here and the compiler team is just following through on the implementation. |
Beta Was this translation helpful? Give feedback.
-
While I don't care for the This seems harder to read :/ |
Beta Was this translation helpful? Give feedback.
-
Did you try putting the post-fix operator after the method call? It looks like the compiler is warning on assigning the |
Beta Was this translation helpful? Give feedback.
-
@HaloFour there are two places where the post-fix operator is required and adding them does make the warning go away. |
Beta Was this translation helpful? Give feedback.
-
I've filed an issue to track a quick-fix for the |
Beta Was this translation helpful? Give feedback.
The post-fix
!
operator will suppress nullability warnings and the compiler will treat the expression as non-null.