Skip to content

Analyzer Configuration

Adjusting Severities

Override the default severity of any rule using .editorconfig:

ini
[*.cs]
# Make boundary violations an error (default)
dotnet_diagnostic.MOD001.severity = error

# Treat handler return type as an error in strict mode
dotnet_diagnostic.MOD002.severity = error

# Disable public setter info messages
dotnet_diagnostic.MOD005.severity = none

Available severity levels:

LevelBehavior
errorBuild fails, red squiggle in IDE
warningBuild succeeds, yellow squiggle in IDE
suggestionGreen dots in IDE, no build impact
silentNot visible in IDE, still available in tooling
noneCompletely disabled

Suppressing Individual Occurrences

When you have a valid reason to suppress a specific diagnostic, use #pragma:

csharp
#pragma warning disable MOD001 // Cross-module reference needed for shared test utilities
using EShop.Modules.Catalog.Domain.Products;
#pragma warning restore MOD001

Or use the [SuppressMessage] attribute:

csharp
using System.Diagnostics.CodeAnalysis;

[SuppressMessage("Modulus", "MOD005", Justification = "DTO, not a domain entity")]
public class ProductDto
{
    public string Name { get; set; }
}

Enforcing in CI

Treat warnings as errors

Add -warnaserror to your CI build to prevent any analyzer warning from passing:

bash
dotnet build -warnaserror

Selective enforcement

Promote specific rules to errors in .editorconfig for stricter enforcement:

ini
[*.cs]
dotnet_diagnostic.MOD001.severity = error
dotnet_diagnostic.MOD002.severity = error
dotnet_diagnostic.MOD003.severity = error
dotnet_diagnostic.MOD004.severity = warning
dotnet_diagnostic.MOD005.severity = suggestion

MSBuild property

You can also configure TreatWarningsAsErrors in Directory.Build.props for CI builds:

xml
<PropertyGroup Condition="'$(CI)' == 'true'">
  <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

For most teams, the default severities work well:

RuleDefaultRecommended
MOD001ErrorError -- boundary violations should always block
MOD002WarningWarning or Error -- depends on how strictly you enforce the Result pattern
MOD003WarningWarning -- gentle nudge toward Result pattern
MOD004WarningWarning -- catches accidental infrastructure leaks
MOD005InfoInfo or Suggestion -- useful but not critical

See Also

Released under the MIT License.