Skip to content

Commit 262571c

Browse files
committed
Fix and test handling of missing tokens in SA1500 (BracesForMultiLineStatementsMustNotShareLine)
1 parent dcdd80a commit 262571c

2 files changed

Lines changed: 65 additions & 3 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1500/SA1500UnitTests.Properties.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.Test.LayoutRules
66
using System.Threading;
77
using System.Threading.Tasks;
88
using StyleCop.Analyzers.LayoutRules;
9+
using StyleCop.Analyzers.Lightup;
910
using TestHelper;
1011
using Xunit;
1112

@@ -461,5 +462,66 @@ public int[] TestProperty
461462
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
462463
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
463464
}
465+
466+
/// <summary>
467+
/// Verifies that a property declaration missing the opening brace will be handled correctly.
468+
/// </summary>
469+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
470+
[Fact]
471+
public async Task TestAccessorMissingOpeningBraceAsync()
472+
{
473+
var testCode = @"
474+
class ClassName
475+
{
476+
int Property
477+
{
478+
get
479+
}
480+
}
481+
}";
482+
483+
DiagnosticResult accessorError;
484+
if (LightupHelpers.SupportsCSharp7)
485+
{
486+
accessorError = this.CSharpCompilerError("CS8180").WithMessage("{ or ; or => expected");
487+
}
488+
else
489+
{
490+
accessorError = this.CSharpCompilerError("CS1043").WithMessage("{ or ; expected");
491+
}
492+
493+
DiagnosticResult[] expected =
494+
{
495+
accessorError.WithLocation(6, 12),
496+
this.CSharpCompilerError("CS1022").WithMessage("Type or namespace definition, or end-of-file expected").WithLocation(9, 1),
497+
};
498+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
499+
}
500+
501+
/// <summary>
502+
/// Verifies that a property declaration missing the closing brace at the end of the source file will be handled
503+
/// correctly.
504+
/// </summary>
505+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
506+
[Fact]
507+
public async Task TestAccessorMissingClosingBraceAtEndOfFileAsync()
508+
{
509+
var testCode = @"
510+
class ClassName
511+
{
512+
int Property
513+
{
514+
get
515+
{";
516+
517+
DiagnosticResult[] expected =
518+
{
519+
this.CSharpCompilerError("CS0161").WithMessage("'ClassName.Property.get': not all code paths return a value").WithLocation(6, 9),
520+
this.CSharpCompilerError("CS1513").WithMessage("} expected").WithLocation(7, 10),
521+
this.CSharpCompilerError("CS1513").WithMessage("} expected").WithLocation(7, 10),
522+
this.CSharpCompilerError("CS1513").WithMessage("} expected").WithLocation(7, 10),
523+
};
524+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
525+
}
464526
}
465527
}

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1500BracesForMultiLineStatementsMustNotShareLine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private static void CheckBraceToken(SyntaxNodeAnalysisContext context, SyntaxTok
186186

187187
int line = token.GetLineSpan().StartLinePosition.Line;
188188

189-
SyntaxToken previousToken = token.GetPreviousToken();
189+
SyntaxToken previousToken = token.GetPreviousToken(includeZeroWidth: true);
190190
if (!previousToken.IsMissing)
191191
{
192192
if (previousToken.GetLineSpan().StartLinePosition.Line == line)
@@ -198,7 +198,7 @@ private static void CheckBraceToken(SyntaxNodeAnalysisContext context, SyntaxTok
198198
}
199199
}
200200

201-
SyntaxToken nextToken = token.GetNextToken();
201+
SyntaxToken nextToken = token.GetNextToken(includeZeroWidth: true);
202202
if (!nextToken.IsMissing)
203203
{
204204
switch (nextToken.Kind())
@@ -210,7 +210,7 @@ private static void CheckBraceToken(SyntaxNodeAnalysisContext context, SyntaxTok
210210
// these are allowed to appear on the same line
211211
return;
212212

213-
case SyntaxKind.None:
213+
case SyntaxKind.EndOfFileToken:
214214
// last token of this file
215215
return;
216216

0 commit comments

Comments
 (0)