Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to treat ALL C# 8 nullable reference warnings as errors?

Using Visual Studio 2019 v16.3.2 with a .NET Core 3.0 project set to C# 8 and nullable reference types enabled.

<PropertyGroup>     <TargetFramework>netcoreapp3.0</TargetFramework>     <LangVersion>8.0</LangVersion>     <Nullable>enable</Nullable> </PropertyGroup>  

If I set it to treat all warnings as errors:

warnings as errors radio button selected

<TreatWarningsAsErrors>true</TreatWarningsAsErrors> <WarningsAsErrors /> 

It reports other warnings as errors. For example, CS0168 The variable 'x' is declared but never used is reported as an error. But all the nullable reference warnings are still reported as warnings. For example, CS8600 Converting null literal or possible null value to non-nullable type. is still reported as a warning.

How do I treat all nullable reference warnings as errors?

Note: even setting CS8600 specifically to be treated as an error doesn't cause it to be reported as an error. If that worked, it still wouldn't help with treating them all as errors because there are so many.

Edit: setting specific warnings to be treated as errors puts <WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors> in the csproj and doesn't work.

like image 400
Jeff Walker Code Ranger Avatar asked Oct 02 '19 03:10

Jeff Walker Code Ranger


People also ask

What is the best treatment for acute lymphoblastic leukemia?

The main treatment for acute lymphoblastic leukaemia (ALL) is chemotherapy. But depending on your subtype of ALL you might have another treatment. This might include a targeted cancer drug, immunotherapy, or a stem cell or bone marrow transplant. Some people might have their treatment as part of a clinical trial.

How curable is ALL leukemia?

Response rates to ALL treatment In general, about 80% to 90% of adults will have complete remissions at some point during these treatments. This means leukemia cells can no longer be seen in their bone marrow. Unfortunately, about half of these patients relapse, so the overall cure rate is in the range of 40%.

What is the survival rate for acute lymphoblastic leukemia?

While acute lymphoblastic leukemia in children is more common than other types of cancer, it has high cure rates. Survival rates are lower in adults, but they are improving. The 5-year relative survival rate for ALL is 68.8%. The statistics further break down to 90% in children and 30-40% in adults.


2 Answers

It is now possible to treat all nullable-related warnings as errors without explicitly specifying them all. To achieve this, you have to set <WarningsAsErrors>Nullable</WarningsAsErrors> in your *.csproj file [source].

Full example:

<Project Sdk="Microsoft.NET.Sdk.Web">   <PropertyGroup>     <TargetFramework>netcoreapp3.1</TargetFramework>     <Nullable>enable</Nullable>     <WarningsAsErrors>Nullable</WarningsAsErrors>   </PropertyGroup> </Project>  
like image 157
officer Avatar answered Sep 21 '22 15:09

officer


The problem was that the .editorconfig file was overriding the Visual Studio setting to treat all warnings as errors with many lines like:

dotnet_diagnostic.CS8602.severity = warning 

This forces CS8602 to be a warning.

How this happened: In a previous attempt at turning all nullable reference warnings into errors, I set many of them as errors in the editor config. In that, I discovered both that there were a ton of different warning numbers and that my codebase wasn't ready for them to be errors throughout the entire solution. So I set them to "warning" in the editor config because I didn't want to lose the list of warnings I had found. Then later, having forgotten all about that, I decided to turn on treat warnings as errors on a project by project basis.

like image 34
Jeff Walker Code Ranger Avatar answered Sep 20 '22 15:09

Jeff Walker Code Ranger