Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SA1107: Dont report if the previous statement is missing its last token #1543

Merged
merged 1 commit into from
Sep 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.Test.ReadabilityRules
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using StyleCop.Analyzers.ReadabilityRules;
Expand Down Expand Up @@ -122,6 +123,35 @@ public static void Foo(string a, string b)
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestThatAnalyzerIgnoresStatementsWithMissingTokenAsync()
{
string testCode = @"
using System;
class ClassName
{
public static void Foo(string a, string b)
{
int i
if (true)
{
Console.WriteLine(""Bar"");
}
}
}
";
DiagnosticResult expected = new DiagnosticResult
{
Id = "CS1002",
Message = "; expected",
Severity = DiagnosticSeverity.Error,
};

expected = expected.WithLocation(7, 14);

await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}

protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
{
yield return new SA1107CodeMustNotContainMultipleStatementsOnOneLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,31 @@ private static void HandleBlock(SyntaxNodeAnalysisContext context)

if (block != null && block.Statements.Any())
{
FileLinePositionSpan previousStatementLocation = block.Statements[0].GetLineSpan();
var previousStatement = block.Statements[0];
FileLinePositionSpan previousStatementLocation = previousStatement.GetLineSpan();
FileLinePositionSpan currentStatementLocation;

for (int i = 1; i < block.Statements.Count; i++)
{
currentStatementLocation = block.Statements[i].GetLineSpan();
var currentStatement = block.Statements[i];
currentStatementLocation = currentStatement.GetLineSpan();

if (previousStatementLocation.EndLinePosition.Line
== currentStatementLocation.StartLinePosition.Line)
== currentStatementLocation.StartLinePosition.Line
&& !IsLastTokenMissing(previousStatement))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, block.Statements[i].GetLocation()));
}

previousStatementLocation = currentStatementLocation;
previousStatement = currentStatement;
}
}
}

private static bool IsLastTokenMissing(StatementSyntax previousStatement)
{
return previousStatement.GetLastToken(includeZeroWidth: true, includeSkipped: true).IsMissing;
}
}
}