You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/Microsoft.NetCore.Analyzers/Core/ImmutableCollections/DoNotCallToImmutableCollectionOnAnImmutableCollectionValue.cs
+11
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,7 @@
3
3
usingSystem.Collections.Generic;
4
4
usingSystem.Collections.Immutable;
5
5
usingSystem.Diagnostics;
6
+
usingSystem.Linq;
6
7
usingAnalyzer.Utilities;
7
8
usingAnalyzer.Utilities.Extensions;
8
9
usingMicrosoft.CodeAnalysis;
@@ -67,6 +68,16 @@ public override void Initialize(AnalysisContext context)
Copy file name to clipboardExpand all lines: src/Microsoft.NetCore.Analyzers/UnitTests/ImmutableCollections/DoNotCallToImmutableCollectionOnAnImmutableCollectionValueTests.cs
public static {collectionName}<TSource> To{collectionName}<TSource>(this IEnumerable<TSource> items, IEqualityComparer<TSource> comparer)
55
+
{{
56
+
return default({collectionName}<TSource>);
57
+
}}
53
58
}}
54
59
55
60
class C
56
61
{{
57
-
public void M(IEnumerable<int> p1, List<int> p2, {collectionName}<int> p3)
62
+
public void M(IEnumerable<int> p1, List<int> p2, {collectionName}<int> p3, IEqualityComparer<int> comparer)
58
63
{{
59
64
// Allowed
60
65
p1.To{collectionName}();
61
66
p2.To{collectionName}();
67
+
p3.To{collectionName}(comparer); // Potentially modifies the collection
62
68
63
69
// No dataflow
64
70
IEnumerable<int> l1 = p3;
@@ -76,13 +82,19 @@ Module Extensions
76
82
Public Function To{collectionName}(Of TSource)(items As IEnumerable(Of TSource)) As {collectionName}(Of TSource)
77
83
Return Nothing
78
84
End Function
85
+
86
+
<System.Runtime.CompilerServices.Extension> _
87
+
Public Function To{collectionName}(Of TSource)(items As IEnumerable(Of TSource), comparer as IEqualityComparer(Of TSource)) As {collectionName}(Of TSource)
88
+
Return Nothing
89
+
End Function
79
90
End Module
80
91
81
92
Class C
82
-
Public Sub M(p1 As IEnumerable(Of Integer), p2 As List(Of Integer), p3 As {collectionName}(Of Integer))
93
+
Public Sub M(p1 As IEnumerable(Of Integer), p2 As List(Of Integer), p3 As {collectionName}(Of Integer), comparer As IEqualityComparer(Of Integer))
83
94
' Allowed
84
95
p1.To{collectionName}()
85
96
p2.To{collectionName}()
97
+
p3.To{collectionName}(comparer) ' Potentially modifies the collection
p3.To{collectionName}(keyComparer); // Potentially modifies the collection
119
137
120
138
// No dataflow
121
139
IEnumerable<KeyValuePair<int, int>> l1 = p3;
@@ -133,13 +151,19 @@ Module Extensions
133
151
Public Function To{collectionName}(Of TKey, TValue)(items As IEnumerable(Of KeyValuePair(Of TKey, TValue))) As {collectionName}(Of TKey, TValue)
134
152
Return Nothing
135
153
End Function
154
+
155
+
<System.Runtime.CompilerServices.Extension> _
156
+
Public Function To{collectionName}(Of TKey, TValue)(items As IEnumerable(Of KeyValuePair(Of TKey, TValue)), keyComparer As IEqualityComparer(Of TKey)) As {collectionName}(Of TKey, TValue)
157
+
Return Nothing
158
+
End Function
136
159
End Module
137
160
138
161
Class C
139
-
Public Sub M(p1 As IEnumerable(Of KeyValuePair(Of Integer, Integer)), p2 As List(Of KeyValuePair(Of Integer, Integer)), p3 As {collectionName}(Of Integer, Integer))
162
+
Public Sub M(p1 As IEnumerable(Of KeyValuePair(Of Integer, Integer)), p2 As List(Of KeyValuePair(Of Integer, Integer)), p3 As {collectionName}(Of Integer, Integer), keyComparer As IEqualityComparer(Of Integer))
140
163
' Allowed
141
164
p1.To{collectionName}()
142
165
p2.To{collectionName}()
166
+
p3.To{collectionName}(keyComparer) ' Potentially modifies the collection
143
167
144
168
' No dataflow
145
169
Dim l1 As IEnumerable(Of KeyValuePair(Of Integer, Integer)) = p3
0 commit comments