$npx -y skills add wshaddix/dotnet-skills --skill dotnet-add-analyzersAdding analyzer packages to a project. Nullable, trimming, AOT compat analyzers, severity config.
| 1 | # dotnet-add-analyzers |
| 2 | |
| 3 | Add and configure .NET code analyzers to an existing project. Covers built-in Roslyn CA rules, nullable reference types enforcement, trimming/AOT compatibility analyzers, and third-party analyzer packages. |
| 4 | |
| 5 | **Prerequisites:** Run [skill:dotnet-version-detection] first — analyzer features vary by SDK version. Run [skill:dotnet-project-analysis] to understand the current project layout. |
| 6 | |
| 7 | Cross-references: [skill:dotnet-project-structure] for where build props/targets live, [skill:dotnet-scaffold-project] which includes analyzer setup in new projects, [skill:dotnet-editorconfig] for EditorConfig hierarchy/precedence, IDE* code style preferences, naming rules, and global AnalyzerConfig files. |
| 8 | |
| 9 | --- |
| 10 | |
| 11 | ## Built-in Roslyn Analyzers |
| 12 | |
| 13 | .NET SDK ships built-in analyzers controlled by `AnalysisLevel`. Configure in `Directory.Build.props`: |
| 14 | |
| 15 | ```xml |
| 16 | <PropertyGroup> |
| 17 | <AnalysisLevel>latest-all</AnalysisLevel> |
| 18 | <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild> |
| 19 | <TreatWarningsAsErrors>true</TreatWarningsAsErrors> |
| 20 | </PropertyGroup> |
| 21 | ``` |
| 22 | |
| 23 | ### AnalysisLevel Values |
| 24 | |
| 25 | | Value | Behavior | |
| 26 | |-------|----------| |
| 27 | | `latest` | Default rules only — covers correctness, not style | |
| 28 | | `latest-minimum` | Fewer rules than default | |
| 29 | | `latest-recommended` | Default + additional recommended rules | |
| 30 | | `latest-all` | All rules enabled — most comprehensive | |
| 31 | | `9-all`, `10-all` | Pin to a specific SDK version's full rule set | |
| 32 | |
| 33 | `latest-all` is recommended for new projects. For existing projects with many warnings, start with `latest-recommended` and tighten over time. |
| 34 | |
| 35 | ### Rule Categories |
| 36 | |
| 37 | | Category | Prefix | Examples | |
| 38 | |----------|--------|----------| |
| 39 | | Design | CA1xxx | CA1002 (don't expose generic lists), CA1062 (validate arguments) | |
| 40 | | Globalization | CA1300–CA1399 | CA1304 (specify CultureInfo) | |
| 41 | | Performance | CA1800–CA1899 | CA1822 (mark members static), CA1848 (use LoggerMessage) | |
| 42 | | Reliability | CA2000–CA2099 | CA2000 (dispose objects), CA2007 (ConfigureAwait) | |
| 43 | | Security | CA2100–CA2199, CA3xxx, CA5xxx | CA2100 (SQL injection), CA3075 (XML processing) | |
| 44 | | Usage | CA2200–CA2299 | CA2211 (non-constant static fields), CA2245 (don't assign to self) | |
| 45 | | Naming | CA1700–CA1799 | CA1707 (no underscores in identifiers) | |
| 46 | | Style | IDE0xxx | IDE0003 (this qualification), IDE0063 (using declaration) | |
| 47 | |
| 48 | --- |
| 49 | |
| 50 | ## EditorConfig Severity Overrides |
| 51 | |
| 52 | Fine-tune analyzer severity per-rule in `.editorconfig`: |
| 53 | |
| 54 | ```ini |
| 55 | [*.cs] |
| 56 | # Suppress specific rules |
| 57 | dotnet_diagnostic.CA1062.severity = none # Nullable handles this |
| 58 | dotnet_diagnostic.CA2007.severity = none # Not needed in ASP.NET Core apps |
| 59 | |
| 60 | # Escalate to error |
| 61 | dotnet_diagnostic.CA1822.severity = error # Mark members as static |
| 62 | dotnet_diagnostic.CA1848.severity = warning # Use LoggerMessage delegates |
| 63 | |
| 64 | # Style enforcement |
| 65 | dotnet_diagnostic.IDE0005.severity = warning # Remove unnecessary usings |
| 66 | dotnet_diagnostic.IDE0063.severity = warning # Use simple using statement |
| 67 | dotnet_diagnostic.IDE0090.severity = warning # Simplify new expression |
| 68 | ``` |
| 69 | |
| 70 | ### Common Suppressions by Project Type |
| 71 | |
| 72 | **ASP.NET Core apps** — suppress ConfigureAwait warnings: |
| 73 | ```ini |
| 74 | dotnet_diagnostic.CA2007.severity = none |
| 75 | ``` |
| 76 | |
| 77 | **Libraries** — keep CA2007 as warning (callers may not have a SynchronizationContext): |
| 78 | ```ini |
| 79 | dotnet_diagnostic.CA2007.severity = warning |
| 80 | ``` |
| 81 | |
| 82 | **Test projects** — relax certain rules: |
| 83 | ```ini |
| 84 | dotnet_diagnostic.CA1707.severity = none # Allow underscores in test names |
| 85 | dotnet_diagnostic.CA1062.severity = none # Parameters validated by test framework |
| 86 | dotnet_diagnostic.CA2007.severity = none # ConfigureAwait not relevant |
| 87 | ``` |
| 88 | |
| 89 | --- |
| 90 | |
| 91 | ## Nullable Reference Types |
| 92 | |
| 93 | Enable globally in `Directory.Build.props`: |
| 94 | |
| 95 | ```xml |
| 96 | <PropertyGroup> |
| 97 | <Nullable>enable</Nullable> |
| 98 | </PropertyGroup> |
| 99 | ``` |
| 100 | |
| 101 | Nullable analysis produces warnings (CS86xx) not CA rules. Related settings: |
| 102 | |
| 103 | ```xml |
| 104 | <PropertyGroup> |
| 105 | <!-- Treat nullable warnings as errors --> |
| 106 | <WarningsAsErrors>$(WarningsAsErrors);nullable</WarningsAsErrors> |
| 107 | </PropertyGroup> |
| 108 | ``` |
| 109 | |
| 110 | For gradual adoption in existing codebases, enable per-file: |
| 111 | |
| 112 | ```csharp |
| 113 | #nullable enable |
| 114 | ``` |
| 115 | |
| 116 | See [skill:dotnet-csharp-nullable-reference-types] for annotation strategies and patterns. |
| 117 | |
| 118 | --- |
| 119 | |
| 120 | ## Trimming and AOT Compatibility Analyzers |
| 121 | |
| 122 | ### Applications |
| 123 | |
| 124 | For apps published with trimming or Native AOT, enable the analyzers alongside the publish properties: |
| 125 | |
| 126 | ```xml |
| 127 | <PropertyGroup> |
| 128 | <!-- Enable trimmed publishing + analysis --> |
| 129 | <PublishTrimmed>true</PublishTrimmed> |
| 130 | <EnableTrimAnalyzer>true</EnableTrimAnalyzer> |
| 131 | |
| 132 | <!-- Enable AOT publishing + analysis --> |
| 133 | <PublishAot>true</PublishAot> |
| 134 | <EnableAotAnalyzer>true</EnableAotAnalyzer> |
| 135 | |
| 136 | <!-- Single-file analysis (subset of trim analysis) --> |
| 137 | <EnableSingleFileAnalyzer>true</EnableSingleFileAnalyzer> |
| 138 | </PropertyGroup> |
| 139 | ``` |
| 140 | |
| 141 | Enable |