Skip to content

Commit 7e69f3e

Browse files
committed
Merge pull request #2052 from vweijsters/fix-2041
SA1513 will now allow a closing brace to be followed by a four slash …
2 parents d0ffa7c + e5d58f5 commit 7e69f3e

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1513UnitTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,36 @@ var x in
796796
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
797797
}
798798

799+
/// <summary>
800+
/// Verifies that code commented out with four slashes is accepted.
801+
/// This is a regression test for #2041
802+
/// </summary>
803+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
804+
[Fact]
805+
public async Task TestFourSlashCommentsAsync()
806+
{
807+
var testCode = @"
808+
public class TestClass
809+
{
810+
public int Do(int i)
811+
{
812+
if (i > 2)
813+
{
814+
return 1;
815+
}
816+
//// else if (i == 2)
817+
//// {
818+
//// return 2;
819+
//// }
820+
821+
return 0;
822+
}
823+
}
824+
";
825+
826+
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
827+
}
828+
799829
/// <inheritdoc/>
800830
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
801831
{

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1513ClosingBraceMustBeFollowedByBlankLine.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,27 @@ private static bool HasLeadingBlankLine(SyntaxTriviaList triviaList)
126126
return false;
127127
}
128128

129+
private static bool StartsWithSpecialComment(SyntaxTriviaList triviaList)
130+
{
131+
foreach (var trivia in triviaList)
132+
{
133+
switch (trivia.Kind())
134+
{
135+
case SyntaxKind.WhitespaceTrivia:
136+
// ignore
137+
break;
138+
139+
case SyntaxKind.SingleLineCommentTrivia:
140+
return trivia.ToFullString().StartsWith("////", StringComparison.Ordinal);
141+
142+
default:
143+
return false;
144+
}
145+
}
146+
147+
return false;
148+
}
149+
129150
private static bool StartsWithDirectiveTrivia(SyntaxTriviaList triviaList)
130151
{
131152
foreach (var trivia in triviaList)
@@ -166,9 +187,10 @@ private void AnalyzeCloseBrace(SyntaxToken token)
166187
{
167188
var nextToken = token.GetNextToken(true, true);
168189

169-
if (nextToken.HasLeadingTrivia && HasLeadingBlankLine(nextToken.LeadingTrivia))
190+
if (nextToken.HasLeadingTrivia
191+
&& (HasLeadingBlankLine(nextToken.LeadingTrivia) || StartsWithSpecialComment(nextToken.LeadingTrivia)))
170192
{
171-
// the close brace has a trailing blank line
193+
// the close brace has a trailing blank line or is followed by a single line comment that starts with 4 slashes.
172194
return;
173195
}
174196

0 commit comments

Comments
 (0)