Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
66 changes: 37 additions & 29 deletions docs/fundamentals/code-analysis/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Code analysis in .NET
titleSuffix: ""
description: Learn about source code analysis in the .NET SDK.
ms.date: 09/30/2025
ms.date: 11/05/2025
ms.topic: overview
ms.custom: updateeachrelease
helpviewer_keywords:
Expand All @@ -24,6 +24,41 @@ If rule violations are found by an analyzer, they're reported as a suggestion, w

### Enabled rules

# [.NET 10](#tab/net-10)

The following rules are enabled, by default, as errors or warnings in .NET 10. Additional rules are enabled as suggestions.

| Diagnostic ID | Category | Severity | Version added | Description |
|-----------------------------------|------------------|----------|---------------|---------------------------------|
| [CA1416](quality-rules/ca1416.md) | Interoperability | Warning | .NET 5 | Validate platform compatibility |
| [CA1417](quality-rules/ca1417.md) | Interoperability | Warning | .NET 5 | Do not use `OutAttribute` on string parameters for P/Invokes |
| [CA1418](quality-rules/ca1418.md) | Interoperability | Warning | .NET 6 | Use valid platform string |
| [CA1420](quality-rules/ca1420.md) | Interoperability | Warning | .NET 7 | Using features that require runtime marshalling when it's disabled will result in run-time exceptions |
| [CA1422](quality-rules/ca1422.md) | Interoperability | Warning | .NET 7 | Validate platform compatibility |
| [CA1831](quality-rules/ca1831.md) | Performance | Warning | .NET 5 | Use `AsSpan` instead of range-based indexers for string when appropriate |
| [CA1856](quality-rules/ca1856.md) | Performance | Error | .NET 8 | Incorrect usage of `ConstantExpected` attribute |
| [CA1857](quality-rules/ca1857.md) | Performance | Warning | .NET 8 | A constant is expected for the parameter |
| [CA2013](quality-rules/ca2013.md) | Reliability | Warning | .NET 5 | Do not use `ReferenceEquals` with value types |
| [CA2014](quality-rules/ca2014.md) | Reliability | Warning | .NET 5 | Do not use `stackalloc` in loops |
| [CA2015](quality-rules/ca2015.md) | Reliability | Warning | .NET 5 | Do not define finalizers for types derived from <xref:System.Buffers.MemoryManager%601> |
| [CA2017](quality-rules/ca2017.md) | Reliability | Warning | .NET 6 | Parameter count mismatch |
| [CA2018](quality-rules/ca2018.md) | Reliability | Warning | .NET 6 | The `count` argument to `Buffer.BlockCopy` should specify the number of bytes to copy |
| [CA2021](quality-rules/ca2021.md) | Reliability | Warning | .NET 8 | Do not call `Enumerable.Cast<T>` or `Enumerable.OfType<T>` with incompatible types |
| [CA2022](quality-rules/ca2022.md) | Reliability | Warning | .NET 9 | Avoid inexact read with `Stream.Read` |
| [CA2023](quality-rules/ca2023.md) | Reliability | Warning | .NET 10 | Invalid braces in message template |
| [CA2200](quality-rules/ca2200.md) | Usage | Warning | .NET 5 | Rethrow to preserve stack details |
| [CA2247](quality-rules/ca2247.md) | Usage | Warning | .NET 5 | Argument passed to `TaskCompletionSource` constructor should be <xref:System.Threading.Tasks.TaskCreationOptions> enum instead of <xref:System.Threading.Tasks.TaskContinuationOptions> |
| [CA2252](quality-rules/ca2252.md) | Usage | Error | .NET 6 | Opt in to preview features |
| [CA2255](quality-rules/ca2255.md) | Usage | Warning | .NET 6 | The `ModuleInitializer` attribute should not be used in libraries |
| [CA2256](quality-rules/ca2256.md) | Usage | Warning | .NET 6 | All members declared in parent interfaces must have an implementation in a `DynamicInterfaceCastableImplementation`-attributed interface |
| [CA2257](quality-rules/ca2257.md) | Usage | Warning | .NET 6 | Members defined on an interface with the `DynamicInterfaceCastableImplementationAttribute` should be `static` |
| [CA2258](quality-rules/ca2258.md) | Usage | Warning | .NET 6 | Providing a `DynamicInterfaceCastableImplementation` interface in Visual Basic is unsupported |
| [CA2259](quality-rules/ca2259.md) | Usage | Warning | .NET 7 | `ThreadStatic` only affects static fields |
| [CA2260](quality-rules/ca2260.md) | Usage | Warning | .NET 7 | Use correct type parameter |
| [CA2261](quality-rules/ca2261.md) | Usage | Warning | .NET 8 | Do not use `ConfigureAwaitOptions.SuppressThrowing` with `Task<TResult>` |
| [CA2264](quality-rules/ca2264.md) | Usage | Warning | .NET 9 | Do not pass a non-nullable value to `ArgumentNullException.ThrowIfNull` |
| [CA2265](quality-rules/ca2265.md) | Usage | Warning | .NET 9 | Do not compare `Span<T>` to `null` or `default` |

# [.NET 9](#tab/net-9)

The following rules are enabled, by default, as errors or warnings in .NET 9. Additional rules are enabled as suggestions.
Expand Down Expand Up @@ -89,38 +124,11 @@ The following rules are enabled, by default, as errors or warnings in .NET 8. Ad
| [CA2260](quality-rules/ca2260.md) | Usage | Warning | .NET 7 | Use correct type parameter |
| [CA2261](quality-rules/ca2261.md) | Usage | Warning | .NET 8 | Do not use `ConfigureAwaitOptions.SuppressThrowing` with `Task<TResult>` |

# [.NET 7](#tab/net-7)

The following rules are enabled, by default, as errors or warnings in .NET 7. Additional rules are enabled as suggestions.

| Diagnostic ID | Category | Severity | Description |
| - | - | - | - |
| [CA1416](quality-rules/ca1416.md) | Interoperability | Warning | Validate platform compatibility |
| [CA1417](quality-rules/ca1417.md) | Interoperability | Warning | Do not use `OutAttribute` on string parameters for P/Invokes |
| [CA1418](quality-rules/ca1418.md) | Interoperability | Warning | Use valid platform string |
| [CA1420](quality-rules/ca1420.md) | Interoperability | Warning | Using features that require runtime marshalling when it's disabled will result in run-time exceptions |
| [CA1422](quality-rules/ca1422.md) | Interoperability | Warning | Validate platform compatibility |
| [CA1831](quality-rules/ca1831.md) | Performance | Warning | Use `AsSpan` instead of range-based indexers for string when appropriate |
| [CA2013](quality-rules/ca2013.md) | Reliability | Warning | Do not use `ReferenceEquals` with value types |
| [CA2014](quality-rules/ca2014.md) | Reliability | Warning | Do not use `stackalloc` in loops |
| [CA2015](quality-rules/ca2015.md) | Reliability | Warning | Do not define finalizers for types derived from <xref:System.Buffers.MemoryManager%601> |
| [CA2017](quality-rules/ca2017.md) | Reliability | Warning | Parameter count mismatch |
| [CA2018](quality-rules/ca2018.md) | Reliability | Warning | The `count` argument to `Buffer.BlockCopy` should specify the number of bytes to copy |
| [CA2200](quality-rules/ca2200.md) | Usage | Warning | Rethrow to preserve stack details |
| [CA2247](quality-rules/ca2247.md) | Usage | Warning | Argument passed to `TaskCompletionSource` constructor should be <xref:System.Threading.Tasks.TaskCreationOptions> enum instead of <xref:System.Threading.Tasks.TaskContinuationOptions> |
| [CA2252](quality-rules/ca2252.md) | Usage | Error | Opt in to preview features |
| [CA2255](quality-rules/ca2255.md) | Usage | Warning | The `ModuleInitializer` attribute should not be used in libraries |
| [CA2256](quality-rules/ca2256.md) | Usage | Warning | All members declared in parent interfaces must have an implementation in a `DynamicInterfaceCastableImplementation`-attributed interface |
| [CA2257](quality-rules/ca2257.md) | Usage | Warning | Members defined on an interface with the `DynamicInterfaceCastableImplementationAttribute` should be `static` |
| [CA2258](quality-rules/ca2258.md) | Usage | Warning | Providing a `DynamicInterfaceCastableImplementation` interface in Visual Basic is unsupported |
| [CA2259](quality-rules/ca2259.md) | Usage | Warning | `ThreadStatic` only affects static fields |
| [CA2260](quality-rules/ca2260.md) | Usage | Warning | Use correct type parameter |

---

You can change the severity of these rules to disable them or elevate them to errors. You can also [enable more rules](#enable-additional-rules).

- For a list of rules that are included with each .NET SDK version, see [Analyzer releases](https://github.com/dotnet/roslyn-analyzers/blob/main/src/NetAnalyzers/Core/AnalyzerReleases.Shipped.md).
- For a list of rules that are included with each .NET SDK version, see [Analyzer releases](https://github.com/dotnet/sdk/blob/main/src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.NetAnalyzers/AnalyzerReleases.Shipped.md).
- For a list of all the code quality rules, see [Code quality rules](quality-rules/index.md).

### Enable additional rules
Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1000.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Do not declare static members on generic types |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1001.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Types that own disposable fields should be disposable |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking - If the type is not visible outside the assembly.<br/><br/>Breaking - If the type is visible outside the assembly. |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1002.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dev_langs:
| **Title** | Do not expose generic lists |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1003.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Use generic event handler instances |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1005.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dev_langs:
| **Title** | Avoid excessive parameters on generic types |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1008.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Enums should have zero value |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking - When you're prompted to add a `None` value to a non-flag enumeration. Breaking - When you're prompted to rename or remove any enumeration values. |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1010.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ms.author: gewarren
| **Title** | Collections should implement generic interface |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1012.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dev_langs:
| **Title** | Abstract types should not have public constructors |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1014.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Mark assemblies with CLSCompliantAttribute |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1016.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Mark assemblies with AssemblyVersionAttribute |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default in .NET 9** | As suggestion |
| **Enabled by default in .NET 10** | As suggestion |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1017.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Mark assemblies with ComVisibleAttribute |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1018.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Mark attributes with AttributeUsageAttribute |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Breaking |
| **Enabled by default in .NET 9** | As suggestion |
| **Enabled by default in .NET 10** | As suggestion |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1019.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Define accessors for attribute arguments |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1021.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ms.author: gewarren
| **Title** | Avoid out parameters |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1024.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Use properties where appropriate |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1027.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ms.author: gewarren
| **Title** | Mark enums with FlagsAttribute |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1028.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Enum storage should be Int32 |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1030.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dev_langs:
| **Title** | Use events where appropriate |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1031.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Do not catch general exception types |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1032.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ms.author: gewarren
| **Title** | Implement standard exception constructors |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1033.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ms.author: gewarren
| **Title** | Interface methods should be callable by child types |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1034.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Nested types should not be visible |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1036.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Override methods on comparable types |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1040.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Avoid empty interfaces |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Breaking |
| **Enabled by default in .NET 9** | No |
| **Enabled by default in .NET 10** | No |

## Cause

Expand Down
2 changes: 1 addition & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca1041.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dev_langs:
| **Title** | Provide ObsoleteAttribute message |
| **Category** | [Design](design-warnings.md) |
| **Fix is breaking or non-breaking** | Non-breaking |
| **Enabled by default in .NET 9** | As suggestion |
| **Enabled by default in .NET 10** | As suggestion |

## Cause

Expand Down
Loading