Skip to content

Commit 5389fe5

Browse files
committed
Fixed ArgumentOutOfRangeException in SA1120
1 parent 7e69f3e commit 5389fe5

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.Test.ReadabilityRules
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.ReadabilityRules;
@@ -404,6 +405,41 @@ public async Task VerifyThatEmptyCommentAtFileEndWillBeHandledProperlyAsync()
404405
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
405406
}
406407

408+
/// <summary>
409+
/// Verifies that an unclosed multiline comment at the end of a source file will be handled correctly.
410+
/// This is a regression test for #2056
411+
/// </summary>
412+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
413+
[Fact]
414+
public async Task VerifyThatUnclosedCommentAtFileEndWillBeHandledProperlyAsync()
415+
{
416+
var testCode = @"public class TestClass
417+
{
418+
}
419+
/*";
420+
421+
var fixedTestCode = @"public class TestClass
422+
{
423+
}
424+
";
425+
426+
DiagnosticResult[] expected =
427+
{
428+
new DiagnosticResult
429+
{
430+
Id = "CS1035",
431+
Message = "End-of-file found, '*/' expected",
432+
Severity = DiagnosticSeverity.Error,
433+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 4, 1) }
434+
},
435+
this.CSharpDiagnostic().WithLocation(4, 1),
436+
};
437+
438+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
439+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
440+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
441+
}
442+
407443
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
408444
{
409445
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
@@ -74,7 +74,7 @@ private static void HandleMultiLineComment(SyntaxTreeAnalysisContext context, Sy
7474
var nodeText = multiLineComment.ToString();
7575

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

7979
if (string.IsNullOrWhiteSpace(commentText))
8080
{

0 commit comments

Comments
 (0)