From d5fb5fbbfa834142235c0f4b9a55e3e7f7e329bf Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Thu, 4 Mar 2021 07:25:19 -0800 Subject: [PATCH] Fix SA1015 handling of '>' followed by ']' Fixes #3312 --- .../SpacingRules/SA1015UnitTests.cs | 49 +++++++++++++++++++ ...ingGenericBracketsMustBeSpacedCorrectly.cs | 7 +++ 2 files changed, 56 insertions(+) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1015UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1015UnitTests.cs index 3c8447025..87bad0f6a 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1015UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1015UnitTests.cs @@ -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 values; + + public void TestMethod2(object input) + { + var x = values[input as List]; + x = values[input as List|}]; + x = values[input as List|} ]; + x = values[input as List|} ]; + } +} +"; + + var fixedCode = @"using System; +using System.Collections.Generic; + +public class TestClass +{ + private Dictionary values; + + public void TestMethod2(object input) + { + var x = values[input as List]; + x = values[input as List]; + x = values[input as List]; + x = values[input as List]; + } +} +"; + + 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); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1015ClosingGenericBracketsMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1015ClosingGenericBracketsMustBeSpacedCorrectly.cs index 803913522..70da702ac 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1015ClosingGenericBracketsMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1015ClosingGenericBracketsMustBeSpacedCorrectly.cs @@ -131,6 +131,13 @@ private static void HandleGreaterThanToken(SyntaxTreeAnalysisContext context, Sy allowTrailingSpace = true; break; + // values[x as T] + // ^^ + case SyntaxKind.CloseBracketToken when nextToken.Parent.IsKind(SyntaxKind.BracketedArgumentList): + allowTrailingNoSpace = true; + allowTrailingSpace = false; + break; + default: allowTrailingNoSpace = false; allowTrailingSpace = true;