-
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.
Add documentation for CA1868 (#36379)
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
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,98 @@ | ||
--- | ||
title: "CA1868: Unnecessary call to 'Contains' for sets" | ||
description: "Learn about code analyzer rule CA1868 - Unnecessary call to 'Contains' for sets" | ||
ms.date: 07/25/2023 | ||
ms.topic: reference | ||
f1_keywords: | ||
- CA1868 | ||
- DoNotGuardSetAddOrRemoveByContainsAnalyzer | ||
helpviewer_keywords: | ||
- CA1868 | ||
author: mpidash | ||
dev_langs: | ||
- CSharp | ||
- VB | ||
--- | ||
|
||
# CA1868: Unnecessary call to 'Contains' for sets | ||
|
||
| | Value | | ||
| ----------------------------------- |----------------------------------------| | ||
| **Rule ID** | CA1868 | | ||
| **Category** | [Performance](performance-warnings.md) | | ||
| **Fix is breaking or non-breaking** | Non-breaking | | ||
| **Enabled by default in .NET 7** | No | | ||
|
||
## Cause | ||
|
||
An <xref:System.Collections.Generic.ISet%601.Add%2A?displayProperty=nameWithType> or <xref:System.Collections.Generic.ICollection%601.Remove%2A?displayProperty=nameWithType> call is guarded by a call to <xref:System.Collections.Generic.ICollection%601.Contains%2A>. Or, an <xref:System.Collections.Immutable.IImmutableSet%601.Add%2A?displayProperty=nameWithType> or <xref:System.Collections.Immutable.IImmutableSet%601.Remove%2A?displayProperty=nameWithType> call is guarded by a call to <xref:System.Collections.Immutable.IImmutableSet%601.Contains%2A?displayProperty=nameWithType>. | ||
|
||
## Rule description | ||
|
||
Both <xref:System.Collections.Generic.ISet%601.Add(%600)?displayProperty=nameWithType> and <xref:System.Collections.Generic.ICollection%601.Remove(%600)?displayProperty=nameWithType> perform a lookup, which makes it redundant to call <xref:System.Collections.Generic.ICollection%601.Contains(%600)?displayProperty=nameWithType> beforehand. It's more efficient to call <xref:System.Collections.Generic.ISet%601.Add(%600)> or <xref:System.Collections.Generic.ICollection%601.Remove(%600)> directly, which returns a Boolean value indicating whether the item was added or removed. | ||
|
||
This logic also applies to <xref:System.Collections.Immutable.IImmutableSet%601.Add(%600)?displayProperty=nameWithType> and <xref:System.Collections.Immutable.IImmutableSet%601.Remove(%600)?displayProperty=nameWithType>, except that they either return a new set if the item is added or removed, or the original set if it wasn't. | ||
|
||
## How to fix violations | ||
|
||
Replace the call to <xref:System.Collections.Generic.ICollection%601.Contains(%600)?displayProperty=nameWithType> (or <xref:System.Collections.Immutable.IImmutableSet%601.Contains(%600)?displayProperty=nameWithType>) that's followed by a call to <xref:System.Collections.Generic.ISet%601.Add(%600)?displayProperty=nameWithType> or <xref:System.Collections.Generic.ICollection%601.Remove(%600)?displayProperty=nameWithType> (or <xref:System.Collections.Immutable.IImmutableSet%601.Add(%600)?displayProperty=nameWithType> or <xref:System.Collections.Immutable.IImmutableSet%601.Remove(%600)?displayProperty=nameWithType>) with a single call to the latter method. | ||
|
||
## Example | ||
|
||
The following code snippet shows a violation of CA1868: | ||
|
||
```csharp | ||
void Run(ISet<string> set) | ||
{ | ||
if (!set.Contains("Hello World")) | ||
{ | ||
set.Add("Hello World"); | ||
} | ||
} | ||
``` | ||
|
||
```vb | ||
Sub Run(set As ISet(Of String)) | ||
If Not set.Contains("Hello World") Then | ||
set.Add("Hello World") | ||
End If | ||
End Sub | ||
``` | ||
|
||
The following code snippet fixes the violation: | ||
|
||
```csharp | ||
void Run(ISet<string> set) | ||
{ | ||
set.Add("Hello World"); | ||
} | ||
``` | ||
|
||
```vb | ||
Sub Run(set As ISet(Of String)) | ||
set.Add("Hello World") | ||
End Sub | ||
``` | ||
|
||
## 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 CA1868 | ||
// The code that's violating the rule is on this line. | ||
#pragma warning restore CA1868 | ||
``` | ||
|
||
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.CA1868.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