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

Fix SA1503 to report on lock, using and fixed statements without braces #1186

Merged
merged 2 commits into from
Aug 11, 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 @@ -28,6 +28,9 @@ public static IEnumerable<object[]> TestStatements
yield return new[] { "while (i == 0)" };
yield return new[] { "for (var j = 0; j < i; j++)" };
yield return new[] { "foreach (var j in new[] { 1, 2, 3 })" };
yield return new[] { "lock (this)" };
yield return new[] { "using (this)" };
yield return new[] { "fixed (byte* ptr = new byte[10])" };
}
}

Expand Down Expand Up @@ -412,29 +415,33 @@ private async Task TestCodeFixForStatementAsync(string statementText)
private string GenerateTestStatement(string statementText)
{
var testCodeFormat = @"using System.Diagnostics;
public class Foo
public class Foo : System.IDisposable
{
public void Bar(int i)
public unsafe void Bar(int i)
{
#STATEMENT#
Debug.Assert(true);
}

public void Dispose() {}
}";
return testCodeFormat.Replace("#STATEMENT#", statementText);
}

private string GenerateFixedTestStatement(string statementText)
{
var fixedTestCodeFormat = @"using System.Diagnostics;
public class Foo
public class Foo : System.IDisposable
{
public void Bar(int i)
public unsafe void Bar(int i)
{
#STATEMENT#
{
Debug.Assert(true);
}
}

public void Dispose() {}
}";
return fixedTestCodeFormat.Replace("#STATEMENT#", statementText);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,17 @@ public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
/// <inheritdoc/>
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeActionHonorExclusions(this.HandleIfStatement, SyntaxKind.IfStatement);
context.RegisterSyntaxNodeActionHonorExclusions(this.HandleDoStatement, SyntaxKind.DoStatement);
context.RegisterSyntaxNodeActionHonorExclusions(this.HandleWhileStatement, SyntaxKind.WhileStatement);
context.RegisterSyntaxNodeActionHonorExclusions(this.HandleForStatement, SyntaxKind.ForStatement);
context.RegisterSyntaxNodeActionHonorExclusions(this.HandleForEachStatement, SyntaxKind.ForEachStatement);
context.RegisterSyntaxNodeActionHonorExclusions(HandleIfStatement, SyntaxKind.IfStatement);
context.RegisterSyntaxNodeActionHonorExclusions(ctx => CheckChildStatement(ctx, ((DoStatementSyntax)ctx.Node).Statement), SyntaxKind.DoStatement);
context.RegisterSyntaxNodeActionHonorExclusions(ctx => CheckChildStatement(ctx, ((WhileStatementSyntax)ctx.Node).Statement), SyntaxKind.WhileStatement);
context.RegisterSyntaxNodeActionHonorExclusions(ctx => CheckChildStatement(ctx, ((ForStatementSyntax)ctx.Node).Statement), SyntaxKind.ForStatement);
context.RegisterSyntaxNodeActionHonorExclusions(ctx => CheckChildStatement(ctx, ((ForEachStatementSyntax)ctx.Node).Statement), SyntaxKind.ForEachStatement);
context.RegisterSyntaxNodeActionHonorExclusions(ctx => CheckChildStatement(ctx, ((FixedStatementSyntax)ctx.Node).Statement), SyntaxKind.FixedStatement);
context.RegisterSyntaxNodeActionHonorExclusions(ctx => CheckChildStatement(ctx, ((UsingStatementSyntax)ctx.Node).Statement), SyntaxKind.UsingStatement);
context.RegisterSyntaxNodeActionHonorExclusions(ctx => CheckChildStatement(ctx, ((LockStatementSyntax)ctx.Node).Statement), SyntaxKind.LockStatement);
}

private void HandleIfStatement(SyntaxNodeAnalysisContext context)
private static void HandleIfStatement(SyntaxNodeAnalysisContext context)
{
var ifStatement = (IfStatementSyntax)context.Node;
if (ifStatement.Parent.IsKind(SyntaxKind.ElseClause))
Expand Down Expand Up @@ -117,47 +120,11 @@ private void HandleIfStatement(SyntaxNodeAnalysisContext context)

foreach (StatementSyntax clause in clauses)
{
this.CheckChildStatement(context, clause);
CheckChildStatement(context, clause);
}
}

private void HandleDoStatement(SyntaxNodeAnalysisContext context)
{
var doStatement = context.Node as DoStatementSyntax;
if (doStatement != null)
{
this.CheckChildStatement(context, doStatement.Statement);
}
}

private void HandleWhileStatement(SyntaxNodeAnalysisContext context)
{
var whileStatement = context.Node as WhileStatementSyntax;
if (whileStatement != null)
{
this.CheckChildStatement(context, whileStatement.Statement);
}
}

private void HandleForStatement(SyntaxNodeAnalysisContext context)
{
var forStatement = context.Node as ForStatementSyntax;
if (forStatement != null)
{
this.CheckChildStatement(context, forStatement.Statement);
}
}

private void HandleForEachStatement(SyntaxNodeAnalysisContext context)
{
var forEachStatement = context.Node as ForEachStatementSyntax;
if (forEachStatement != null)
{
this.CheckChildStatement(context, forEachStatement.Statement);
}
}

private void CheckChildStatement(SyntaxNodeAnalysisContext context, StatementSyntax childStatement)
private static void CheckChildStatement(SyntaxNodeAnalysisContext context, StatementSyntax childStatement)
{
if (childStatement is BlockSyntax)
{
Expand Down