Skip to content

Commit 332eaec

Browse files
committed
SA1107: Dont report if the previous statement is missing its last token
1 parent 98b446f commit 332eaec

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1107UnitTests.cs

Lines changed: 30 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;
@@ -122,6 +123,35 @@ public static void Foo(string a, string b)
122123
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
123124
}
124125

126+
[Fact]
127+
public async Task TestThatAnalyzerIgnoresStatementsWithMissingTokenAsync()
128+
{
129+
string testCode = @"
130+
using System;
131+
class ClassName
132+
{
133+
public static void Foo(string a, string b)
134+
{
135+
int i
136+
if (true)
137+
{
138+
Console.WriteLine(""Bar"");
139+
}
140+
}
141+
}
142+
";
143+
DiagnosticResult expected = new DiagnosticResult
144+
{
145+
Id = "CS1002",
146+
Message = "; expected",
147+
Severity = DiagnosticSeverity.Error,
148+
};
149+
150+
expected = expected.WithLocation(7, 14);
151+
152+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
153+
}
154+
125155
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
126156
{
127157
yield return new SA1107CodeMustNotContainMultipleStatementsOnOneLine();

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1107CodeMustNotContainMultipleStatementsOnOneLine.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,31 @@ private static void HandleBlock(SyntaxNodeAnalysisContext context)
6262

6363
if (block != null && block.Statements.Any())
6464
{
65-
FileLinePositionSpan previousStatementLocation = block.Statements[0].GetLineSpan();
65+
var previousStatement = block.Statements[0];
66+
FileLinePositionSpan previousStatementLocation = previousStatement.GetLineSpan();
6667
FileLinePositionSpan currentStatementLocation;
6768

6869
for (int i = 1; i < block.Statements.Count; i++)
6970
{
70-
currentStatementLocation = block.Statements[i].GetLineSpan();
71+
var currentStatement = block.Statements[i];
72+
currentStatementLocation = currentStatement.GetLineSpan();
7173

7274
if (previousStatementLocation.EndLinePosition.Line
73-
== currentStatementLocation.StartLinePosition.Line)
75+
== currentStatementLocation.StartLinePosition.Line
76+
&& !IsLastTokenMissing(previousStatement))
7477
{
7578
context.ReportDiagnostic(Diagnostic.Create(Descriptor, block.Statements[i].GetLocation()));
7679
}
7780

7881
previousStatementLocation = currentStatementLocation;
82+
previousStatement = currentStatement;
7983
}
8084
}
8185
}
86+
87+
private static bool IsLastTokenMissing(StatementSyntax previousStatement)
88+
{
89+
return previousStatement.GetLastToken(includeZeroWidth: true, includeSkipped: true).IsMissing;
90+
}
8291
}
8392
}

0 commit comments

Comments
 (0)