Skip to content

Commit b4610a1

Browse files
committed
Additional tests for valid initializer expressions and fix bugs
* Fix misuse of immutable collections API
1 parent a0967d0 commit b4610a1

2 files changed

Lines changed: 108 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1137UnitTests.cs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,113 @@ void MethodName()
182182
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
183183
}
184184

185+
[Fact]
186+
public async Task TestValidInitializerExpressionAsync()
187+
{
188+
string testCode = @"
189+
using System.Collections.Generic;
190+
class ClassName
191+
{
192+
void EmptyInitializersMethod()
193+
{
194+
// array initializer
195+
int[] array = { };
196+
197+
// collection initializer
198+
List<int> list = new List<int> { };
199+
200+
// complex element initializer
201+
Dictionary<int, int> dictionary = new Dictionary<int, int> { };
202+
203+
// object initializer
204+
var obj = new StructName { };
205+
}
206+
207+
void SingleLineInitializersMethod()
208+
{
209+
// array initializer
210+
int[] array = { 0 };
211+
212+
// collection initializer
213+
List<int> list = new List<int> { 0 };
214+
215+
// complex element initializer
216+
Dictionary<int, int> dictionary = new Dictionary<int, int> { { 0, 0 } };
217+
218+
// object initializer
219+
var obj = new StructName { X = 0 };
220+
}
221+
222+
void SingleElementInitializersMethod()
223+
{
224+
// array initializer
225+
int[] array =
226+
{
227+
0,
228+
};
229+
230+
// collection initializer
231+
List<int> list =
232+
new List<int>
233+
{
234+
0,
235+
};
236+
237+
// complex element initializer
238+
Dictionary<int, int> dictionary =
239+
new Dictionary<int, int>
240+
{
241+
{ 0, 0 },
242+
};
243+
244+
// object initializer
245+
var obj =
246+
new StructName
247+
{
248+
X = 0,
249+
};
250+
}
251+
252+
void SharedLineInitializersMethod()
253+
{
254+
// array initializer
255+
int[] array =
256+
{
257+
0, 0,
258+
};
259+
260+
// collection initializer
261+
List<int> list =
262+
new List<int>
263+
{
264+
0, 0,
265+
};
266+
267+
// complex element initializer
268+
Dictionary<int, int> dictionary =
269+
new Dictionary<int, int>
270+
{
271+
{ 0, 0 }, { 0, 0 },
272+
};
273+
274+
// object initializer
275+
var obj =
276+
new StructName
277+
{
278+
X = 0, Y = 0,
279+
};
280+
}
281+
}
282+
283+
struct StructName
284+
{
285+
public int X, Y, Z;
286+
}
287+
";
288+
289+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
290+
}
291+
185292
[Fact]
186293
public async Task TestInitializerExpressionAsync()
187294
{

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1137ElementsShouldHaveTheSameIndentation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ private static void CheckElements<T>(SyntaxNodeAnalysisContext context, Immutabl
346346
return;
347347
}
348348

349-
elements.RemoveAll(
349+
elements = elements.RemoveAll(
350350
element =>
351351
{
352352
SyntaxToken firstToken = element.GetFirstToken();

0 commit comments

Comments
 (0)