|
| 1 | +--- |
| 2 | +title: 'CA2009: Do not call ToImmutableCollection on an ImmutableCollection value' |
| 3 | +ms.date: 04/29/2020 |
| 4 | +ms.topic: reference |
| 5 | +f1_keywords: |
| 6 | +- CA2009 |
| 7 | +- DoNotCallToImmutableCollectionOnAnImmutableCollectionValueAnalyzer |
| 8 | +helpviewer_keywords: |
| 9 | +- CA2009 |
| 10 | +author: mavasani |
| 11 | +ms.author: mavasani |
| 12 | +manager: jillfra |
| 13 | +--- |
| 14 | +# CA2009: Do not call ToImmutableCollection on an ImmutableCollection value |
| 15 | + |
| 16 | +||| |
| 17 | +|-|-| |
| 18 | +|TypeName|DoNotCallToImmutableCollectionOnAnImmutableCollectionValueAnalyzer| |
| 19 | +|CheckId|CA2009| |
| 20 | +|Category|Microsoft.Reliability| |
| 21 | +|Breaking change|Non-breaking| |
| 22 | + |
| 23 | +## Cause |
| 24 | + |
| 25 | +`ToImmutable` method was unnecessarily called on an immutable collection from <xref:System.Collections.Immutable> namespace. |
| 26 | + |
| 27 | +## Rule description |
| 28 | + |
| 29 | +<xref:System.Collections.Immutable> namespace contains types that define immutable collections. This rule analyzes the following immutable collection types: |
| 30 | + |
| 31 | +- <xref:System.Collections.Immutable.ImmutableArray%601?displayProperty=fullName> |
| 32 | +- <xref:System.Collections.Immutable.ImmutableList%601?displayProperty=fullName> |
| 33 | +- <xref:System.Collections.Immutable.ImmutableHashSet%601?displayProperty=fullName> |
| 34 | +- <xref:System.Collections.Immutable.ImmutableSortedSet%601?displayProperty=fullName> |
| 35 | +- <xref:System.Collections.Immutable.ImmutableDictionary%602?displayProperty=fullName> |
| 36 | +- <xref:System.Collections.Immutable.ImmutableSortedDictionary%602?displayProperty=fullName> |
| 37 | + |
| 38 | +These types define extension methods that create a new immutable collection from an existing <xref:System.Collections.Generic.IEnumerable%601> collection. |
| 39 | + |
| 40 | +- <xref:System.Collections.Immutable.ImmutableArray%601> defines <xref:System.Collections.Immutable.ImmutableArray.ToImmutableArray%2A>. |
| 41 | +- <xref:System.Collections.Immutable.ImmutableList%601> defines <xref:System.Collections.Immutable.ImmutableList.ToImmutableList%2A>. |
| 42 | +- <xref:System.Collections.Immutable.ImmutableHashSet%601> defines <xref:System.Collections.Immutable.ImmutableHashSet.ToImmutableHashSet%2A>. |
| 43 | +- <xref:System.Collections.Immutable.ImmutableSortedSet%601> defines <xref:System.Collections.Immutable.ImmutableSortedSet.ToImmutableSortedSet%2A>. |
| 44 | +- <xref:System.Collections.Immutable.ImmutableDictionary%602> defines <xref:System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary%2A>. |
| 45 | +- <xref:System.Collections.Immutable.ImmutableSortedDictionary%602> defines <xref:System.Collections.Immutable.ImmutableSortedDictionary.ToImmutableSortedDictionary%2A>. |
| 46 | + |
| 47 | +These extension methods are designed to convert a mutable collection to an immutable collection. However, the caller might accidentally pass in an immutable collection as input to these methods. This can represent a performance and/or a functional issue. |
| 48 | + |
| 49 | +- Performance issue: Unnecessary creation of a duplicate immutable collection. The original collection was already immutable and can be used directly. |
| 50 | +- Potential functional issue: Caller assumed to be operating on a mutable collection, when it actually had an immutable collection. |
| 51 | + |
| 52 | +> [!NOTE] |
| 53 | +> Rule CA2009 is not available in legacy analysis. It was first introduced in [FxCop analyzers](https://www.nuget.org/packages/Microsoft.CodeAnalysis.FxCopAnalyzers) version 2.9.6. |
| 54 | +
|
| 55 | +## How to fix violations |
| 56 | + |
| 57 | +To fix violations, remove the redundant `ToImmutable` call on an immutable collection. For example, the following two code snippets show a violation of the rule and how to fix them: |
| 58 | + |
| 59 | +```csharp |
| 60 | +using System; |
| 61 | +using System.Collections.Generic; |
| 62 | +using System.Collections.Immutable; |
| 63 | + |
| 64 | +public class C |
| 65 | +{ |
| 66 | + public void M(IEnumerable<int> collection, ImmutableArray<int> immutableArray) |
| 67 | + { |
| 68 | + // This is fine. |
| 69 | + M2(collection.ToImmutableArray()); |
| 70 | + |
| 71 | + // This leads to CA2009. |
| 72 | + M2(immutableArray.ToImmutableArray()); |
| 73 | + } |
| 74 | + |
| 75 | + private void M2(ImmutableArray<int> immutableArray) |
| 76 | + { |
| 77 | + Console.WriteLine(immutableArray.Length); |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | +```csharp |
| 84 | +using System; |
| 85 | +using System.Collections.Generic; |
| 86 | +using System.Collections.Immutable; |
| 87 | + |
| 88 | +public class C |
| 89 | +{ |
| 90 | + public void M(IEnumerable<int> collection, ImmutableArray<int> immutableArray) |
| 91 | + { |
| 92 | + // This is fine. |
| 93 | + M2(collection.ToImmutableArray()); |
| 94 | + |
| 95 | + // This is now fine. |
| 96 | + M2(immutableArray); |
| 97 | + } |
| 98 | + |
| 99 | + private void M2(ImmutableArray<int> immutableArray) |
| 100 | + { |
| 101 | + Console.WriteLine(immutableArray.Length); |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +> [!TIP] |
| 107 | +> A code fix is available for this rule in Visual Studio. To use it, position the cursor on the violation and press **Ctrl**+**.** (period). Choose **Remove redundant call** from the list of options that's presented. |
| 108 | +> |
| 109 | +>  |
| 110 | +
|
| 111 | +## When to suppress warnings |
| 112 | + |
| 113 | +Do not suppress violations from this rule, unless you're not concerned about the performance impact from unnecessary allocations of immutable collections. |
| 114 | + |
| 115 | +## See also |
| 116 | + |
| 117 | +- [Reliability warnings](reliability-warnings.md) |
| 118 | +- [Performance warnings](performance-warnings.md) |
0 commit comments