Roslyn: WarningsAsErrors for CS8602 is not returning an error

Created on 29 Aug 2019  路  5Comments  路  Source: dotnet/roslyn

Version Used:
Visual Studio 2019 16.2.3
.NET Core: 3.0.0-preview8-28405-07

Steps to Reproduce:

For CS8602 I am not getting the expected errors when I add this code in the WarningsAsErrors section.

  1. Modify the csproj as:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8602</WarningsAsErrors>

</PropertyGroup>
</Project>

  1. My sample application code is:

private static void PosibleDereferenceNullableReferenceTypes()
{
string data = null;
Console.WriteLine(data.Length);
}

Expected Behavior:
An error when we build the project.
Error CS8602 Dereference of a possibly null reference.

Note: When I put the codes CS8600, CS8603 or CS8604 into the WarningsAsErrors section, I receive the expected error messages in compilation time.

Actual Behavior:
Warning CS8602 Dereference of a possibly null reference.

Updated information about this issue and its behavior:

If I write into the csproj:
<WarningsAsErrors>CS8600;CS8602</WarningsAsErrors>
I get the error messages for CS8600 (as expected) and for CS8602

Area-External Bug Resolution-External

All 5 comments

Additional notes

If I change my csproj as:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>

And my application code is:

private static void PosibleDereferenceNullableReferenceTypes()
{
#nullable enable
string data = null;
Console.WriteLine(data.Length);
#nullable restore
}

Here I get the error for CS8602 as expected

For some reason, the command line build fails, but we don't get an error in the VS error list. Looking into whether there is maybe a bug in the VS workspace.

This may be an instance of dotnet/project-system#3680, but relating to the WarningsAsErrors property instead of the WarningsNotAsErrors property.

Basically, it seems like for some values of WarningsAsErrors, the project system sends us a command line with ... /warnAsError- ... /warnAsError+:CS0105 .... For other values it sends us a command line like ... /warnAsError+:CS8602 ... /warnAsError- .... The latter case is where the bug happens-- warnAsError- wins and we don't get the right behavior in the IDE.

As far as I am aware, this is a project system bug, and can't be fixed within Roslyn. /cc @davkean

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AdamSpeight2008 picture AdamSpeight2008  路  3Comments

DavidArno picture DavidArno  路  3Comments

glennblock picture glennblock  路  3Comments

orthoxerox picture orthoxerox  路  3Comments

marler8997 picture marler8997  路  3Comments