Skip to content

Commit 1385c56

Browse files
authored
Add CA2265 docs (#43231)
1 parent 8fa23a9 commit 1385c56

File tree

7 files changed

+90
-2
lines changed

7 files changed

+90
-2
lines changed

docs/core/whats-new/dotnet-9/sdk.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,4 @@ Environment variables that the container publish tooling uses to control some of
215215
| [CA2262: Set 'MaxResponseHeadersLength' properly](../../../fundamentals/code-analysis/quality-rules/ca2262.md) | Usage | The <xref:System.Net.Http.HttpClientHandler.MaxResponseHeadersLength?displayProperty=nameWithType> property is measured in kilobytes, not bytes. |
216216
| [CA2263: Prefer generic overload when type is known](../../../fundamentals/code-analysis/quality-rules/ca2263.md) | Usage | Generic overloads are preferable to overloads that accept an argument of type <xref:System.Type?displayProperty=fullName> when the type is known at compile time. |
217217
| [CA2264: Do not pass a non-nullable value to 'ArgumentNullException.ThrowIfNull'](../../../fundamentals/code-analysis/quality-rules/ca2264.md) | Usage | Certain constructs like non-nullable structs (except for <xref:System.Nullable%601>), 'nameof()' expressions, and 'new' expressions are known to never be null, so `ArgumentNullException.ThrowIfNull` will never throw. |
218-
| CA2265 | Usage | Comparing a span to `null` or `default` might not do what you intended. `default` and the `null` literal are implicitly converted to <xref:System.Span`1.Empty?displayProperty=nameWithType>. Remove the redundant comparison or make the code more explicit by using `IsEmpty`. |
218+
| [CA2265](../../../fundamentals/code-analysis/quality-rules/ca2265.md) | Usage | Comparing a span to `null` or `default` might not do what you intended. `default` and the `null` literal are implicitly converted to <xref:System.Span`1.Empty?displayProperty=nameWithType>. Remove the redundant comparison or make the code more explicit by using `IsEmpty`. |
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: "CA2265: Do not compare Span<T> to 'null' or 'default'"
3+
description: "Learn about code analysis rule CA2265 - Do not compare Span<T> to 'null' or 'default'"
4+
ms.date: 10/28/2024
5+
f1_keywords:
6+
- CA2265
7+
helpviewer_keywords:
8+
- CA2265
9+
dev_langs:
10+
- CSharp
11+
---
12+
# CA2265: Do not compare `Span<T>` to `null` or `default`
13+
14+
| Property | Value |
15+
|-------------------------------------|-------------------------------------------------|
16+
| **Rule ID** | CA2264 |
17+
| **Title** | Do not compare `Span<T>` to `null` or `default` |
18+
| **Category** | [Usage](usage-warnings.md) |
19+
| **Fix is breaking or non-breaking** | Non-breaking |
20+
| **Enabled by default in .NET 9** | As warning |
21+
22+
## Cause
23+
24+
A <xref:System.Span`1> instance is compared to `null` or `default`.
25+
26+
## Rule description
27+
28+
Comparing a span to `null` or `default` might not do what you intended. `default` and the `null` literal are implicitly converted to `Span<T>.Empty`.
29+
30+
## How to fix violations
31+
32+
Remove the redundant comparison or make the code more explicit by calling <xref:System.Span`1.IsEmpty> instead.
33+
34+
## Example
35+
36+
The following code snippet shows two violations of CA2265 and the fix for the violations.
37+
38+
:::code language="csharp" source="snippets/csharp/all-rules/ca2265.cs" id="1":::
39+
40+
## When to suppress warnings
41+
42+
It's safe to suppress this warning if you meant to compare the span to the empty span.
43+
44+
## Suppress a warning
45+
46+
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
47+
48+
```csharp
49+
#pragma warning disable CA2265
50+
// The code that's violating the rule is on this line.
51+
#pragma warning restore CA2265
52+
```
53+
54+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md).
55+
56+
```ini
57+
[*.{cs,vb}]
58+
dotnet_diagnostic.CA2265.severity = none
59+
```
60+
61+
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).
62+
63+
## See also
64+
65+
- [dotnet/runtime issue 84265](https://github.com/dotnet/runtime/issues/84265)

docs/fundamentals/code-analysis/quality-rules/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ The following table lists code quality analysis rules.
238238
> | [CA2262: Set `MaxResponseHeadersLength` properly](ca2262.md) | Make sure the `MaxResponseHeadersLength` value is provided correctly. This value is measured in kilobytes. |
239239
> | [CA2263: Prefer generic overload when type is known](ca2263.md) | Using a generic overload is preferable to passing a <xref:System.Type?displayProperty=fullName> argument when the type is known, because they promote cleaner and more type-safe code with improved compile-time checks. |
240240
> | [CA2264: Do not pass a non-nullable value to 'ArgumentNullException.ThrowIfNull'](ca2264.md) | 'ArgumentNullException.ThrowIfNull' throws when the passed argument is 'null'. Certain constructs like non-nullable structs, and 'nameof()' and 'new' expressions are known to never be null, so 'ArgumentNullException.ThrowIfNull' will never throw. |
241+
> | [CA2265: Do not compare `Span<T>` to `null` or `default`](ca2265.md) | Comparing a span to `null` or `default` might not do what you intended. `default` and the `null` literal are implicitly converted to `Span<T>.Empty`. |
241242
> | [CA2300: Do not use insecure deserializer BinaryFormatter](ca2300.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. |
242243
> | [CA2301: Do not call BinaryFormatter.Deserialize without first setting BinaryFormatter.Binder](ca2301.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. |
243244
> | [CA2302: Ensure BinaryFormatter.Binder is set before calling BinaryFormatter.Deserialize](ca2302.md) | Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. |
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace ca2265;
4+
class CA2265
5+
{
6+
public static void RunIt()
7+
{
8+
// <Snippet1>
9+
Span<int> span = new([1, 2, 3]);
10+
// CA2265 violation.
11+
if (span == null) { }
12+
// CA2265 violation.
13+
if (span == default) { }
14+
15+
// Fixes the violation.
16+
if (span.IsEmpty) { }
17+
// </Snippet1>
18+
}
19+
}

docs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/all-rules.vbproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<RootNamespace>all_rules</RootNamespace>
6-
<TargetFramework>net8.0</TargetFramework>
6+
<TargetFramework>net9.0</TargetFramework>
77
<NoWarn>$(NoWarn);SYSLIB0050</NoWarn>
88
</PropertyGroup>
99

docs/fundamentals/code-analysis/quality-rules/usage-warnings.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,5 @@ Usage rules support proper usage of .NET.
6363
| [CA2261: Do not use `ConfigureAwaitOptions.SuppressThrowing` with `Task<TResult>`](ca2261.md) | The `ConfigureAwaitOptions.SuppressThrowing` option isn't supported by the generic `Task<TResult>`, since that might lead to returning an invalid `TResult`. |
6464
| [CA2262: Set `MaxResponseHeadersLength` properly](ca2262.md) | Make sure the `MaxResponseHeadersLength` value is provided correctly. This value is measured in kilobytes. |
6565
| [CA2264: Do not pass a non-nullable value to 'ArgumentNullException.ThrowIfNull'](ca2264.md) | 'ArgumentNullException.ThrowIfNull' throws when the passed argument is 'null'. Certain constructs like non-nullable structs, and 'nameof()' and 'new' expressions are known to never be null, so 'ArgumentNullException.ThrowIfNull' will never throw. |
66+
| [CA2265: Do not compare `Span<T>` to `null` or `default`](ca2265.md) | Comparing a span to `null` or `default` might not do what you intended. `default` and the `null` literal are implicitly converted to `Span<T>.Empty`. |
6667
| [CA2263: Prefer generic overload when type is known](ca2263.md) | Using a generic overload is preferable to passing a <xref:System.Type?displayProperty=fullName> argument when the type is known, because they promote cleaner and more type-safe code with improved compile-time checks. |

docs/navigate/tools-diagnostics/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,8 @@ items:
14331433
href: ../../fundamentals/code-analysis/quality-rules/ca2263.md
14341434
- name: CA2264
14351435
href: ../../fundamentals/code-analysis/quality-rules/ca2264.md
1436+
- name: CA2265
1437+
href: ../../fundamentals/code-analysis/quality-rules/ca2265.md
14361438
- name: Code style rules
14371439
items:
14381440
- name: Overview

0 commit comments

Comments
 (0)