CA1307 appears unexpectedly when you set CodeAnalysisTreatWarningsAsErrors=false
sliekens opened this issue · comments
Analyzer
Diagnostic ID: CA1307 : Specify StringComparison for clarity
Analyzer source
SDK: Built-in CA analyzers in .NET 5 SDK or later
Version: SDK 8.0.301
Describe the bug
I want to treat C# compiler warnings as errors, but not code analysis warnings.
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
For some unknown reason, setting CodeAnalysisTreatWarningsAsErrors
to false
made the analyzer more aggressive. The CA1307 warning started to appear in my editor (VSCode with C# Dev Kit) even though CA1307 is not included in the default code analysis level.
Steps To Reproduce
Project:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<!-- Compiler warnings as errors -->
<PropertyGroup>
<WarningLevel>8</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>
</PropertyGroup>
<!-- Code analysis warnings NOT as errors -->
<PropertyGroup>
<AnalysisLevel>latest-default</AnalysisLevel>
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
</PropertyGroup>
</Project>
Program.cs:
Console.WriteLine("Hello, World!".Contains("Hello"));
Expected behavior
No analyzer warnings should be reported in VSCode.
dotnet build
should succeed without warnings or errors.
Actual behavior
CA1307 is reported as a warning in VSCode (but not by dotnet build
).
CA1307 goes away completely when you flip CodeAnalysisTreatWarningsAsErrors
from false
to true
.
Additional context
CA1307 should only be reported when AnalysisMode
is All
.
$ dotnet --info
.NET SDK:
Version: 8.0.301
Commit: 1a0e9c0300
Workload version: 8.0.300-manifests.f6879a9a
MSBuild version: 17.10.4+10fbfbf2e
Runtime Environment:
OS Name: debian
OS Version: 11
OS Platform: Linux
RID: linux-x64
Base Path: /usr/share/dotnet/sdk/8.0.301/
.NET workloads installed:
There are no installed workloads to display.
Host:
Version: 8.0.6
Architecture: x64
Commit: 3b8b000a0e
.NET SDKs installed:
6.0.423 [/usr/share/dotnet/sdk]
8.0.301 [/usr/share/dotnet/sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.31 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.6 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.31 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.20 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.6 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Other architectures found:
None
Environment variables:
DOTNET_ROOT [/usr/share/dotnet]
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download