Skip to content

Commit

Permalink
Fix SA1015 handling of '>' followed by ']'
Browse files Browse the repository at this point in the history
Fixes #3312
  • Loading branch information
sharwell committed Mar 4, 2021
1 parent 23db6c0 commit d5fb5fb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -381,5 +381,54 @@ void Method()

await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3312, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3312")]
public async Task TestSpacingInIndexAsync()
{
var testCode = @"using System;
using System.Collections.Generic;
public class TestClass
{
private Dictionary<object, object> values;
public void TestMethod2(object input)
{
var x = values[input as List<int>];
x = values[input as List<int {|#0:>|}];
x = values[input as List<int{|#1:>|} ];
x = values[input as List<int {|#2:>|} ];
}
}
";

var fixedCode = @"using System;
using System.Collections.Generic;
public class TestClass
{
private Dictionary<object, object> values;
public void TestMethod2(object input)
{
var x = values[input as List<int>];
x = values[input as List<int>];
x = values[input as List<int>];
x = values[input as List<int>];
}
}
";

DiagnosticResult[] expected =
{
Diagnostic(DescriptorNotPreceded).WithLocation(0),
Diagnostic(DescriptorNotFollowed).WithLocation(1),
Diagnostic(DescriptorNotPreceded).WithLocation(2),
Diagnostic(DescriptorNotFollowed).WithLocation(2),
};

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ private static void HandleGreaterThanToken(SyntaxTreeAnalysisContext context, Sy
allowTrailingSpace = true;
break;

// values[x as T<int>]
// ^^
case SyntaxKind.CloseBracketToken when nextToken.Parent.IsKind(SyntaxKind.BracketedArgumentList):
allowTrailingNoSpace = true;
allowTrailingSpace = false;
break;

default:
allowTrailingNoSpace = false;
allowTrailingSpace = true;
Expand Down

0 comments on commit d5fb5fb

Please sign in to comment.