From 06285795340f69ec207bd72e854cc9183fe688ca Mon Sep 17 00:00:00 2001 From: Mario Pistrich Date: Tue, 25 Jul 2023 02:17:05 +0200 Subject: [PATCH 1/7] Add documentation for CA1865 --- .../code-analysis/quality-rules/ca1865.md | 98 +++++++++++++++++++ .../code-analysis/quality-rules/index.md | 1 + .../quality-rules/performance-warnings.md | 1 + docs/navigate/tools-diagnostics/toc.yml | 2 + 4 files changed, 102 insertions(+) create mode 100644 docs/fundamentals/code-analysis/quality-rules/ca1865.md diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1865.md b/docs/fundamentals/code-analysis/quality-rules/ca1865.md new file mode 100644 index 0000000000000..a25d2abd8b66c --- /dev/null +++ b/docs/fundamentals/code-analysis/quality-rules/ca1865.md @@ -0,0 +1,98 @@ +--- +title: "CA1865: Unnecessary call to 'Contains' for sets" +description: "Learn about code analyzer rule CA1865 - Unnecessary call to 'Contains' for sets" +ms.date: 07/25/2023 +ms.topic: reference +f1_keywords: + - CA1865 + - DoNotGuardSetAddOrRemoveByContainsAnalyzer +helpviewer_keywords: + - CA1865 +author: mpidash +dev_langs: + - CSharp + - VB +--- + +# CA1865: Unnecessary call to 'Contains' for sets + +| | Value | +| ----------------------------------- |----------------------------------------| +| **Rule ID** | CA1865 | +| **Category** | [Performance](performance-warnings.md) | +| **Fix is breaking or non-breaking** | Non-breaking | +| **Enabled by default in .NET 7** | No | + +## Cause + +An or ( or ) call is guarded by a call to (). + +## Rule description + +Both and perform a lookup which makes calling before redundant. It is more efficient to call and directly, which will return a Boolean value indicating whether an item has been added or removed. + +This also applies to and , except that they either return a new set when an item is added or removed, or the original set otherwise. + +## How to fix violations + +Replace the call to () that is followed by a call to or ( or ) with a single call to the latter. + +## Example + +The following code snippet shows a violation of CA1865: + +```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 CA1865 +// The code that's violating the rule is on this line. +#pragma warning restore CA1865 +``` + +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.CA1865.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..6c49b0cd3f1f1 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. | +> | [CA1865: Unnecessary call to 'Contains' for sets](ca1865.md) | Both and perform a lookup which makes calling before redundant. It is more efficient to call and directly, which will return a Boolean value indicating whether an item has been 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..7af53e8a984c2 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. | +| [CA1865: Unnecessary call to 'Contains' for sets](ca1865.md) | Both and perform a lookup which makes calling before redundant. It is more efficient to call and directly, which will return a Boolean value indicating whether an item has been added or removed. | diff --git a/docs/navigate/tools-diagnostics/toc.yml b/docs/navigate/tools-diagnostics/toc.yml index 436c580b5f97d..09cb30e981a7e 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: CA1865 + href: ../../fundamentals/code-analysis/quality-rules/ca1865.md - name: SingleFile rules items: - name: Overview From 3345e26bf7fc1cc0505ce50dcb632d9d7c0dcffa Mon Sep 17 00:00:00 2001 From: Mario Pistrich Date: Tue, 25 Jul 2023 11:30:30 +0200 Subject: [PATCH 2/7] Apply suggestions from code review Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- docs/fundamentals/code-analysis/quality-rules/ca1865.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1865.md b/docs/fundamentals/code-analysis/quality-rules/ca1865.md index a25d2abd8b66c..5dccf455ed1d7 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1865.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1865.md @@ -29,13 +29,13 @@ An or and perform a lookup which makes calling before redundant. It is more efficient to call and directly, which will return a Boolean value indicating whether an item has been added or removed. +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 also applies to and , except that they either return a new set when an item is added or removed, or the original set otherwise. +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 () that is followed by a call to or ( or ) with a single call to the latter. +Replace the call to (or ) that's followed by a call to or (or or ) with a single call to the latter method. ## Example From 3635ae1aef5cbc55edfa891cf6b296fdfa4dcebe Mon Sep 17 00:00:00 2001 From: Mario Pistrich Date: Tue, 25 Jul 2023 11:32:25 +0200 Subject: [PATCH 3/7] Add space --- docs/fundamentals/code-analysis/quality-rules/ca1865.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1865.md b/docs/fundamentals/code-analysis/quality-rules/ca1865.md index 5dccf455ed1d7..22c352516cbb8 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1865.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1865.md @@ -29,7 +29,7 @@ An or 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. +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. From e055b1cae46fe07e6c02527aff246dce1e4ada9c Mon Sep 17 00:00:00 2001 From: Mario Pistrich Date: Tue, 25 Jul 2023 11:34:50 +0200 Subject: [PATCH 4/7] Apply previous changes to index files --- docs/fundamentals/code-analysis/quality-rules/index.md | 2 +- .../code-analysis/quality-rules/performance-warnings.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/index.md b/docs/fundamentals/code-analysis/quality-rules/index.md index 6c49b0cd3f1f1..1d465f61fefde 100644 --- a/docs/fundamentals/code-analysis/quality-rules/index.md +++ b/docs/fundamentals/code-analysis/quality-rules/index.md @@ -158,7 +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. | -> | [CA1865: Unnecessary call to 'Contains' for sets](ca1865.md) | Both and perform a lookup which makes calling before redundant. It is more efficient to call and directly, which will return a Boolean value indicating whether an item has been added or removed. | +> | [CA1865: Unnecessary call to 'Contains' for sets](ca1865.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 7af53e8a984c2..aeabae6e365b8 100644 --- a/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md +++ b/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md @@ -71,4 +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. | -| [CA1865: Unnecessary call to 'Contains' for sets](ca1865.md) | Both and perform a lookup which makes calling before redundant. It is more efficient to call and directly, which will return a Boolean value indicating whether an item has been added or removed. | +| [CA1865: Unnecessary call to 'Contains' for sets](ca1865.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. | From 8672f265d3b5878e606d868d873b611decc0c2b8 Mon Sep 17 00:00:00 2001 From: Mario Pistrich Date: Fri, 28 Jul 2023 02:27:26 +0200 Subject: [PATCH 5/7] Move from CA1865 to CA1868 --- .../quality-rules/{ca1865.md => ca1868.md} | 20 +++++++++---------- .../code-analysis/quality-rules/index.md | 2 +- .../quality-rules/performance-warnings.md | 2 +- docs/navigate/tools-diagnostics/toc.yml | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) rename docs/fundamentals/code-analysis/quality-rules/{ca1865.md => ca1868.md} (90%) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1865.md b/docs/fundamentals/code-analysis/quality-rules/ca1868.md similarity index 90% rename from docs/fundamentals/code-analysis/quality-rules/ca1865.md rename to docs/fundamentals/code-analysis/quality-rules/ca1868.md index 22c352516cbb8..fbaa3d3aa87c2 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1865.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1868.md @@ -1,24 +1,24 @@ --- -title: "CA1865: Unnecessary call to 'Contains' for sets" -description: "Learn about code analyzer rule CA1865 - Unnecessary call to 'Contains' for sets" +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: - - CA1865 + - CA1868 - DoNotGuardSetAddOrRemoveByContainsAnalyzer helpviewer_keywords: - - CA1865 + - CA1868 author: mpidash dev_langs: - CSharp - VB --- -# CA1865: Unnecessary call to 'Contains' for sets +# CA1868: Unnecessary call to 'Contains' for sets | | Value | | ----------------------------------- |----------------------------------------| -| **Rule ID** | CA1865 | +| **Rule ID** | CA1868 | | **Category** | [Performance](performance-warnings.md) | | **Fix is breaking or non-breaking** | Non-breaking | | **Enabled by default in .NET 7** | No | @@ -39,7 +39,7 @@ Replace the call to set) @@ -83,16 +83,16 @@ It's safe to suppress this warning if performance isn't a concern. 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 CA1865 +#pragma warning disable CA1868 // The code that's violating the rule is on this line. -#pragma warning restore CA1865 +#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.CA1865.severity = none +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 1d465f61fefde..a868268214739 100644 --- a/docs/fundamentals/code-analysis/quality-rules/index.md +++ b/docs/fundamentals/code-analysis/quality-rules/index.md @@ -158,7 +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. | -> | [CA1865: Unnecessary call to 'Contains' for sets](ca1865.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. | +> | [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 aeabae6e365b8..bb1b11cd745ee 100644 --- a/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md +++ b/docs/fundamentals/code-analysis/quality-rules/performance-warnings.md @@ -71,4 +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. | -| [CA1865: Unnecessary call to 'Contains' for sets](ca1865.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. | +| [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 09cb30e981a7e..1238300a1087f 100644 --- a/docs/navigate/tools-diagnostics/toc.yml +++ b/docs/navigate/tools-diagnostics/toc.yml @@ -805,8 +805,8 @@ items: href: ../../fundamentals/code-analysis/quality-rules/ca1861.md - name: CA1864 href: ../../fundamentals/code-analysis/quality-rules/ca1864.md - - name: CA1865 - href: ../../fundamentals/code-analysis/quality-rules/ca1865.md + - name: CA1868 + href: ../../fundamentals/code-analysis/quality-rules/ca1868.md - name: SingleFile rules items: - name: Overview From 419f0085ef501f94131e8e2fa2935f3742473c43 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Fri, 28 Jul 2023 14:40:41 -0700 Subject: [PATCH 6/7] Update docs/fundamentals/code-analysis/quality-rules/ca1868.md --- docs/fundamentals/code-analysis/quality-rules/ca1868.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1868.md b/docs/fundamentals/code-analysis/quality-rules/ca1868.md index fbaa3d3aa87c2..45ab3980d0e30 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1868.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1868.md @@ -25,7 +25,8 @@ dev_langs: ## Cause -An or ( or ) call is guarded by a call to (). +An or call is guarded by a call to . Or, +an or call is guarded by a call to . ## Rule description From d5caccd09f72434741cb9a94cb3da5642ef585f4 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Fri, 28 Jul 2023 14:52:58 -0700 Subject: [PATCH 7/7] Apply suggestions from code review --- docs/fundamentals/code-analysis/quality-rules/ca1868.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1868.md b/docs/fundamentals/code-analysis/quality-rules/ca1868.md index 45ab3980d0e30..9ece56699b701 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1868.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1868.md @@ -25,8 +25,7 @@ dev_langs: ## Cause -An or call is guarded by a call to . Or, -an or call is guarded by a call to . +An or call is guarded by a call to . Or, an or call is guarded by a call to . ## Rule description