Skip to content

Commit 39b5c3f

Browse files
committed
Merge pull request #1578 from vweijsters/fix-1534
2 parents e6be9df + 5be315c commit 39b5c3f

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1508UnitTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.Test.LayoutRules
66
using System.Collections.Generic;
77
using System.Threading;
88
using System.Threading.Tasks;
9+
using Microsoft.CodeAnalysis;
910
using Microsoft.CodeAnalysis.CodeFixes;
1011
using Microsoft.CodeAnalysis.Diagnostics;
1112
using StyleCop.Analyzers.LayoutRules;
@@ -832,6 +833,57 @@ public int TestProperty
832833
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
833834
}
834835

836+
/// <summary>
837+
/// Verifies that an invalid syntax will not crash the analyzer.
838+
/// This is a regression test for #1534
839+
/// </summary>
840+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
841+
[Fact]
842+
public async Task TestInvalidSyntaxAsync()
843+
{
844+
var testCode = @"namespace TestNamespace
845+
{
846+
public class TestClass /
847+
{
848+
}
849+
}
850+
";
851+
852+
DiagnosticResult[] expected =
853+
{
854+
new DiagnosticResult
855+
{
856+
Id = "CS1514",
857+
Severity = DiagnosticSeverity.Error,
858+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 3, 28) },
859+
Message = "{ expected"
860+
},
861+
new DiagnosticResult
862+
{
863+
Id = "CS1513",
864+
Severity = DiagnosticSeverity.Error,
865+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 3, 28) },
866+
Message = "} expected"
867+
},
868+
new DiagnosticResult
869+
{
870+
Id = "CS1022",
871+
Severity = DiagnosticSeverity.Error,
872+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 3, 28) },
873+
Message = "Type or namespace definition, or end-of-file expected"
874+
},
875+
new DiagnosticResult
876+
{
877+
Id = "CS1022",
878+
Severity = DiagnosticSeverity.Error,
879+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 6, 1) },
880+
Message = "Type or namespace definition, or end-of-file expected"
881+
}
882+
};
883+
884+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
885+
}
886+
835887
/// <inheritdoc/>
836888
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
837889
{

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1508ClosingCurlyBracketsMustNotBePrecededByBlankLine.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,10 @@ private static void AnalyzeCloseBrace(SyntaxNodeAnalysisContext context, SyntaxT
129129
var separatingTrivia = TriviaHelper.MergeTriviaLists(previousToken.TrailingTrivia, closeBraceToken.LeadingTrivia);
130130

131131
// skip all leading whitespace for the close brace
132+
// the index must be checked because two tokens can be more than two lines apart and
133+
// still only be separated by whitespace trivia due to compilation errors
132134
var index = separatingTrivia.Count - 1;
133-
while (separatingTrivia[index].IsKind(SyntaxKind.WhitespaceTrivia))
135+
while (index >= 0 && separatingTrivia[index].IsKind(SyntaxKind.WhitespaceTrivia))
134136
{
135137
index--;
136138
}

0 commit comments

Comments
 (0)