Open apsthisdev opened 3 years ago
You need to add nullable attributes to your code in order for the analyzer to know that Error can't be null after the IsError check See this example https://github.com/dotnet/aspnetcore/blob/8b30d862de6c9146f466061d51aa3f1414ee2337/src/Mvc/Mvc.Razor/src/RazorPageFactoryResult.cs#L43
You can read about the attributes at https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/nullable-analysis
@BrennanConroy, you closed the issue already but how does MemberNotNullWhenAttribute solve the problem for class libraries targeting .NET Standard 2.0 and 2.1 ? Looks like MemberNotNullWhenAttribute is only available .NET5 + ?
Am I missing something here ?
This is by-design. When looking at the call-site if(result.IsError) { ... }
the compiler only sees the signature of IsError
, not the body of the implementation. For this call to affect nullability, IsError
needs to declare that it informs nullability. The MemberNotNullWhenAttribute
is designed for this. In this case [MemberNotNullWhen(true, "Error")]
will do it.
@jcouv, thanks I understand the MemberNotNullWhen and that part is OK. But it is not available for .NET Standard 2.0 and 2.1. At least the docs says so. So how can .NET Standard class libraries use them to inform nullability to callers ?
The nullability feature isn't supported on .NET Standard 2.0. I'd have to dig into the announcement posts to check which version of .NET it was initially supported for.
That said, some people define their own MemberNotNullWhenAttribute
type for such scenarios.
I create a simple project, and enabled the nullable
Sample
Result.cs
Usage code...
This is incorrect, the Error is never null here because the IsError accessor already checks for null and the code would not do inside the if condition.
This is the simplistic use case and the VS analyzer flags these kinds of incorrect null warning all over the place in the code. This is very irritating and non productive to even try and investigate the cause.
C# Class Libraries, Blazor Projects, ASP.NET Core Projects VS 2019, 2022