Skip to content

Commit 046752f

Browse files
authored
Merge pull request #3313 from sharwell/generic-index
Fix SA1015 handling of '>' followed by ']'
2 parents 32818be + d5fb5fb commit 046752f

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1015UnitTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,5 +381,54 @@ void Method()
381381

382382
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
383383
}
384+
385+
[Fact]
386+
[WorkItem(3312, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3312")]
387+
public async Task TestSpacingInIndexAsync()
388+
{
389+
var testCode = @"using System;
390+
using System.Collections.Generic;
391+
392+
public class TestClass
393+
{
394+
private Dictionary<object, object> values;
395+
396+
public void TestMethod2(object input)
397+
{
398+
var x = values[input as List<int>];
399+
x = values[input as List<int {|#0:>|}];
400+
x = values[input as List<int{|#1:>|} ];
401+
x = values[input as List<int {|#2:>|} ];
402+
}
403+
}
404+
";
405+
406+
var fixedCode = @"using System;
407+
using System.Collections.Generic;
408+
409+
public class TestClass
410+
{
411+
private Dictionary<object, object> values;
412+
413+
public void TestMethod2(object input)
414+
{
415+
var x = values[input as List<int>];
416+
x = values[input as List<int>];
417+
x = values[input as List<int>];
418+
x = values[input as List<int>];
419+
}
420+
}
421+
";
422+
423+
DiagnosticResult[] expected =
424+
{
425+
Diagnostic(DescriptorNotPreceded).WithLocation(0),
426+
Diagnostic(DescriptorNotFollowed).WithLocation(1),
427+
Diagnostic(DescriptorNotPreceded).WithLocation(2),
428+
Diagnostic(DescriptorNotFollowed).WithLocation(2),
429+
};
430+
431+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
432+
}
384433
}
385434
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1015ClosingGenericBracketsMustBeSpacedCorrectly.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ private static void HandleGreaterThanToken(SyntaxTreeAnalysisContext context, Sy
136136
allowTrailingSpace = true;
137137
break;
138138

139+
// values[x as T<int>]
140+
// ^^
141+
case SyntaxKind.CloseBracketToken when nextToken.Parent.IsKind(SyntaxKind.BracketedArgumentList):
142+
allowTrailingNoSpace = true;
143+
allowTrailingSpace = false;
144+
break;
145+
139146
default:
140147
allowTrailingNoSpace = false;
141148
allowTrailingSpace = true;

0 commit comments

Comments
 (0)