roslyn-analyzers icon indicating copy to clipboard operation
roslyn-analyzers copied to clipboard

Implement PH2163: Avoid NoWarn project settings for diagnostic suppression

Open Copilot opened this issue 5 months ago • 8 comments

This PR implements a new analyzer PH2163 that detects when NoWarn project settings are used to suppress diagnostic warnings instead of the preferred .editorconfig approach.

Problem

Developers were using <NoWarn> elements in project files to suppress diagnostic warnings:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <NoWarn>$(NoWarn);CS8002;PH2071</NoWarn>
  </PropertyGroup>
</Project>

This approach is discouraged because it lacks the granular control, team consistency, and IDE integration benefits of .editorconfig files.

Solution

The new AvoidNoWarnAnalyzerSuppressionAnalyzer detects any NoWarn usage in project settings and suggests using .editorconfig instead:

  • Detection: Scans project files (.csproj, .vbproj) found via source directory traversal
  • Simplified Logic: Detects any <NoWarn> element usage for comprehensive coverage
  • Performance: Lightweight string-based detection without XML parsing overhead
  • Error Handling: No try-catch blocks for dogfood build compliance
  • Diagnostic Message: Provides clear guidance with .editorconfig syntax

Example Output

Before (triggers PH2163):

<NoWarn>$(NoWarn);CS8002;PH2071</NoWarn>

After (recommended approach):

# .editorconfig
[*.cs]
dotnet_diagnostic.CS8002.severity = none
dotnet_diagnostic.PH2071.severity = none

Technical Details

  • Base Class: SolutionAnalyzer (enabled by default)
  • File Discovery: Uses proven pattern from LicenseAnalyzer for finding project files
  • String Detection: Simple case-insensitive string matching for performance
  • Dogfood Compliant: No empty catch blocks or analyzer violations

Benefits of .editorconfig over NoWarn

  1. Granular control: Configure different severities (none, suggestion, warning, error)
  2. File-specific configuration: Apply rules to specific file patterns
  3. Team consistency: Shared configuration across different IDEs and tools
  4. Version control friendly: Easy to track and review analyzer configuration changes
  5. IDE integration: Better support in Visual Studio and other editors

Fixes #998.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Sep 09 '25 16:09 Copilot