diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1868.md b/docs/fundamentals/code-analysis/quality-rules/ca1868.md new file mode 100644 index 0000000000000..9ece56699b701 --- /dev/null +++ b/docs/fundamentals/code-analysis/quality-rules/ca1868.md @@ -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 or call is guarded by a call to . Or, an or call is guarded by a call to . + +## Rule description + +Both and perform a lookup, which makes it redundant to call beforehand. It's more efficient to call or directly, which returns a Boolean value indicating whether the item was added or removed. + +This logic also applies to and , 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 (or ) that's followed by a call to or (or or ) with a single call to the latter method. + +## Example + +The following code snippet shows a violation of CA1868: + +```csharp +void Run(ISet 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 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). diff --git a/docs/fundamentals/code-analysis/quality-rules/index.md b/docs/fundamentals/code-analysis/quality-rules/index.md index 46d5fdc02325d..a868268214739 100644 --- a/docs/fundamentals/code-analysis/quality-rules/index.md +++ b/docs/fundamentals/code-analysis/quality-rules/index.md @@ -158,6 +158,7 @@ The following table lists code quality analysis rules. > | [CA1860: Avoid using 'Enumerable.Any()' extension method](ca1860.md) | It's more efficient and clearer to use `Length`, `Count`, or `IsEmpty` (if possible) than to call to determine whether a collection type has any elements. | > | [CA1861: Avoid constant arrays as arguments](ca1861.md) | Constant arrays passed as arguments are not reused which implies a performance overhead. Consider extracting them to 'static readonly' fields to improve performance. | > | [CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method](ca1864.md) | Both and perform a lookup, which is redundant. It's is more efficient to call , which returns a `bool` indicating if the value was added or not. `TryAdd` doesn't overwrite the key's value if the key is already present. | +> | [CA1868: Unnecessary call to 'Contains' for sets](ca1868.md) | Both and perform a lookup, which makes it redundant to call beforehand. It's more efficient to call or directly, which returns a Boolean value indicating whether the item was added or removed. | > | [CA2000: Dispose objects before losing scope](ca2000.md) | Because an exceptional event might occur that will prevent the finalizer of an object from running, the object should be explicitly disposed before all references to it are out of scope. | > | [CA2002: Do not lock on objects with weak identity](ca2002.md) |An object is said to have a weak identity when it can be directly accessed across application domain boundaries. A thread that tries to acquire a lock on an object that has a weak identity can be blocked by a second thread in a different application domain that has a lock on the same object. | > | [CA2007: Do not directly await a Task](ca2007.md) | An asynchronous method [awaits](../../../csharp/language-reference/operators/await.md) a directly. When an asynchronous method awaits a directly, continuation occurs in the same thread that created the task. This behavior can be costly in terms of performance and can result in a deadlock on the UI thread. Consider calling to signal your intention for continuation. | diff --git a/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md b/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md index 385d3927b7df2..bb1b11cd745ee 100644 --- a/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md +++ b/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md @@ -71,3 +71,4 @@ Performance rules support high-performance libraries and applications. | [CA1860: Avoid using 'Enumerable.Any()' extension method](ca1860.md) | It's more efficient and clearer to use `Length`, `Count`, or `IsEmpty` (if possible) than to call to determine whether a collection type has any elements. | | [CA1861: Avoid constant arrays as arguments](ca1861.md) | Constant arrays passed as arguments are not reused which implies a performance overhead. Consider extracting them to 'static readonly' fields to improve performance. | | [CA1864: Prefer the 'IDictionary.TryAdd(TKey, TValue)' method](ca1864.md) | Both and perform a lookup, which is redundant. It's is more efficient to call , which returns a `bool` indicating if the value was added or not. `TryAdd` doesn't overwrite the key's value if the key is already present. | +| [CA1868: Unnecessary call to 'Contains' for sets](ca1868.md) | Both and perform a lookup, which makes it redundant to call beforehand. It's more efficient to call or directly, which returns a Boolean value indicating whether the item was added or removed. | diff --git a/docs/navigate/tools-diagnostics/toc.yml b/docs/navigate/tools-diagnostics/toc.yml index 436c580b5f97d..1238300a1087f 100644 --- a/docs/navigate/tools-diagnostics/toc.yml +++ b/docs/navigate/tools-diagnostics/toc.yml @@ -805,6 +805,8 @@ items: href: ../../fundamentals/code-analysis/quality-rules/ca1861.md - name: CA1864 href: ../../fundamentals/code-analysis/quality-rules/ca1864.md + - name: CA1868 + href: ../../fundamentals/code-analysis/quality-rules/ca1868.md - name: SingleFile rules items: - name: Overview