Skip to content

Commit da94bf6

Browse files
committed
Merge pull request #2068 from vweijsters/fix-2056
Fixed ArgumentOutOfRangeException in SA1120
2 parents d5bd61e + 5389fe5 commit da94bf6

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1120UnitTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,35 @@ public async Task VerifyThatEmptyCommentAtFileEndWillBeHandledProperlyAsync()
404404
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
405405
}
406406

407+
/// <summary>
408+
/// Verifies that an unclosed multi-line comment at the end of a source file will be handled correctly.
409+
/// This is a regression test for #2056
410+
/// </summary>
411+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
412+
[Fact]
413+
public async Task VerifyThatUnclosedCommentAtFileEndWillBeHandledProperlyAsync()
414+
{
415+
var testCode = @"public class TestClass
416+
{
417+
}
418+
/*";
419+
420+
var fixedTestCode = @"public class TestClass
421+
{
422+
}
423+
";
424+
425+
DiagnosticResult[] expected =
426+
{
427+
this.CSharpCompilerError("CS1035").WithMessage("End-of-file found, '*/' expected").WithLocation(4, 1),
428+
this.CSharpDiagnostic().WithLocation(4, 1),
429+
};
430+
431+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
432+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
433+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
434+
}
435+
407436
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
408437
{
409438
yield return new SA1120CommentsMustContainText();

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1120CommentsMustContainText.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private static void HandleMultiLineComment(SyntaxTreeAnalysisContext context, Sy
7171
var nodeText = multiLineComment.ToString();
7272

7373
// We remove the /* and the */ and determine if the comment has any content.
74-
var commentText = nodeText.Substring(2, nodeText.Length - 4);
74+
var commentText = nodeText.Substring(2, Math.Max(0, nodeText.Length - 4));
7575

7676
if (string.IsNullOrWhiteSpace(commentText))
7777
{

0 commit comments

Comments
 (0)