将 Visual Studio 2019 v16.3.2 与 .NET Core 3.0 项目设置为 C# 8 并启用可为空引用类型。
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
如果我将其设置为将所有警告视为错误:
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
Run Code Online (Sandbox Code Playgroud)
它将其他警告报告为错误。例如,CS0168 The variable 'x' is declared but never used被报告为错误。但所有可为空的引用警告仍报告为警告。例如,CS8600 Converting null literal or possible null value to non-nullable type.仍然报告为警告。
如何将所有可为空的引用警告视为错误?
注意:即使将 CS8600 专门设置为错误处理,也不会导致其报告为错误。如果这样做有效,那么将它们全部视为错误仍然无济于事,因为有很多。
编辑:将特定警告设置为错误放入<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>csproj 并且不起作用。