-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37056 from MihaZupan/ca1870
Add documentation for CA1870
- Loading branch information
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
docs/fundamentals/code-analysis/quality-rules/ca1870.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
--- | ||
title: "CA1870: Use a cached 'SearchValues' instance" | ||
description: "Learn about code analyzer rule CA1870 - Use a cached 'SearchValues' instance" | ||
ms.date: 09/12/2023 | ||
ms.topic: reference | ||
f1_keywords: | ||
- CA1870 | ||
- UseSearchValuesAnalyzer | ||
helpviewer_keywords: | ||
- CA1870 | ||
dev_langs: | ||
- CSharp | ||
author: MihaZupan | ||
ms.author: mizupan | ||
--- | ||
|
||
# CA1870: Use a cached 'SearchValues' instance | ||
|
||
| Property | Value | | ||
|-------------------------------------|----------------------------------------| | ||
| **Rule ID** | CA1870 | | ||
| **Title** | Use a cached 'SearchValues' instance | | ||
| **Category** | [Performance](performance-warnings.md) | | ||
| **Fix is breaking or non-breaking** | Non-breaking | | ||
| **Enabled by default in .NET 8** | Yes | | ||
|
||
## Cause | ||
|
||
An `IndexOfAny` or `ContainsAny` method is called with many constant values in a way that can benefit from using <xref:System.Buffers.SearchValues> instead. | ||
|
||
The rule doesn't flag calls that use up to five values, as those already use an optimal implementation. | ||
|
||
## Rule description | ||
|
||
Using a cached <xref:System.Buffers.SearchValues%601> instance is more efficient than passing values to `IndexOfAny` or `ContainsAny` directly. | ||
|
||
## How to fix violations | ||
|
||
Create and cache a <xref:System.Buffers.SearchValues%601> instance in a `static readonly` field, then pass that instance to the `IndexOfAny` or `ContainsAny` call instead. | ||
|
||
A code fixer that performs this transformation is available. | ||
|
||
## Example | ||
|
||
The following code snippet shows two violations of CA1870: | ||
|
||
```csharp | ||
static readonly char[] MyValues = new[] { 'a', 'b', 'c', 'x', 'y', 'z' }; | ||
|
||
static int IndexOfMyValues(ReadOnlySpan<char> text) | ||
{ | ||
return text.IndexOfAny(MyValues); | ||
} | ||
|
||
static bool ContainsOnlyMyValues(ReadOnlySpan<char> text) | ||
{ | ||
return !text.ContainsAnyExcept("abcxyz"); | ||
} | ||
``` | ||
|
||
The following code snippet fixes the violations: | ||
|
||
```csharp | ||
private static readonly SearchValues<char> s_myValues = SearchValues.Create("abcxyz"); | ||
|
||
static int IndexOfMyValues(ReadOnlySpan<char> text) | ||
{ | ||
return text.IndexOfAny(s_myValues); | ||
} | ||
|
||
static bool ContainsOnlyMyValues(ReadOnlySpan<char> text) | ||
{ | ||
return !text.ContainsAnyExcept(s_myValues); | ||
} | ||
``` | ||
|
||
If there are multiple calls to `IndexOfAny` with the same set of values, `s_myValues` should be reused. | ||
|
||
## When to suppress warnings | ||
|
||
It's safe to suppress this warning if performance isn't a concern. | ||
|
||
## Suppress a warning | ||
|
||
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. | ||
|
||
```csharp | ||
#pragma warning disable CA1870 | ||
// The code that's violating the rule is on this line. | ||
#pragma warning restore CA1870 | ||
``` | ||
|
||
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). | ||
|
||
```ini | ||
[*.{cs,vb}] | ||
dotnet_diagnostic.CA1870.severity = none | ||
``` | ||
|
||
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters