@@ -161,6 +161,64 @@ public int this [ [CLSCompliant(true)]int index]
161161 await this . VerifyCSharpFixAsync ( testCode , ExpectedCode ) . ConfigureAwait ( false ) ;
162162 }
163163
164+ /// <summary>
165+ /// Verify that index initializers are properly handled.
166+ /// Regression test for https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1617
167+ /// </summary>
168+ /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
169+ [ Fact ]
170+ public async Task VerifyIndexInitializerAsync ( )
171+ {
172+ var testCode = @"using System.Collections.Generic;
173+
174+ public class TestClass
175+ {
176+ public void TestMethod(IDictionary<ulong, string> items)
177+ {
178+ var test = new Dictionary<ulong, string>(items) { [100] = ""100"" };
179+ }
180+ }
181+ " ;
182+
183+ await this . VerifyCSharpDiagnosticAsync ( testCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
184+ }
185+
186+ /// <summary>
187+ /// Verify that index initializer scope determination is working as intended.
188+ /// </summary>
189+ /// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
190+ [ Fact ]
191+ public async Task VerifyThatIndexInitializerScopeIsDeterminedProperlyAsync ( )
192+ {
193+ var testCode = @"using System.Collections.Generic;
194+
195+ public class TestClass
196+ {
197+ public void TestMethod(IDictionary<ulong, string> items)
198+ {
199+ int[] indexes = { 0 };
200+ var dictionary = new Dictionary<int, int> { [indexes [0]] = 0 };
201+ }
202+ }
203+ " ;
204+
205+ var fixedTestCode = @"using System.Collections.Generic;
206+
207+ public class TestClass
208+ {
209+ public void TestMethod(IDictionary<ulong, string> items)
210+ {
211+ int[] indexes = { 0 };
212+ var dictionary = new Dictionary<int, int> { [indexes[0]] = 0 };
213+ }
214+ }
215+ " ;
216+ var expected = this . CSharpDiagnostic ( ) . WithLocation ( 8 , 62 ) . WithArguments ( "not be preceded" ) ;
217+ await this . VerifyCSharpDiagnosticAsync ( testCode , expected , CancellationToken . None ) . ConfigureAwait ( false ) ;
218+ await this . VerifyCSharpDiagnosticAsync ( fixedTestCode , EmptyDiagnosticResults , CancellationToken . None ) . ConfigureAwait ( false ) ;
219+ await this . VerifyCSharpFixAsync ( testCode , fixedTestCode ) . ConfigureAwait ( false ) ;
220+ }
221+
164222 /// <inheritdoc/>
165223 protected override IEnumerable < DiagnosticAnalyzer > GetCSharpDiagnosticAnalyzers ( )
166224 {
0 commit comments