Skip to content

Commit ecc6096

Browse files
committed
Merge pull request #1622 from vweijsters/fix-1617
2 parents 68e55fc + 77c2d72 commit ecc6096

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1010UnitTests.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
{

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private static void HandleOpenBracketToken(SyntaxTreeAnalysisContext context, Sy
9292
bool followedBySpace = token.IsFollowedByWhitespace();
9393
bool lastInLine = token.IsLastInLine();
9494

95-
if (!firstInLine && precededBySpace && !ignorePrecedingSpaceProblem)
95+
if (!firstInLine && precededBySpace && !ignorePrecedingSpaceProblem && !IsPartOfIndexInitializer(token))
9696
{
9797
// Opening square bracket must {not be preceded} by a space.
9898
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingCodeFixProvider.RemovePreceding, "not be preceded"));
@@ -104,5 +104,11 @@ private static void HandleOpenBracketToken(SyntaxTreeAnalysisContext context, Sy
104104
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingCodeFixProvider.RemoveFollowing, "not be followed"));
105105
}
106106
}
107+
108+
private static bool IsPartOfIndexInitializer(SyntaxToken token)
109+
{
110+
return token.Parent.IsKind(SyntaxKind.BracketedArgumentList)
111+
&& token.Parent.Parent.IsKind(SyntaxKind.ImplicitElementAccess);
112+
}
107113
}
108114
}

0 commit comments

Comments
 (0)