From ff1ebfcec5febbb7fafb719c153f6b9328b4a864 Mon Sep 17 00:00:00 2001 From: Dmitry Turin Date: Tue, 11 Aug 2015 10:50:23 +0300 Subject: [PATCH 1/2] Fix SA1503: report on `lock`, `using`, `fixed` statements without braces --- .../SA1503CurlyBracketsMustNotBeOmitted.cs | 55 ++++--------------- 1 file changed, 11 insertions(+), 44 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1503CurlyBracketsMustNotBeOmitted.cs b/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1503CurlyBracketsMustNotBeOmitted.cs index b0dc2738e..3e418c11f 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1503CurlyBracketsMustNotBeOmitted.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1503CurlyBracketsMustNotBeOmitted.cs @@ -80,14 +80,17 @@ public override ImmutableArray SupportedDiagnostics /// 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)) @@ -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) { From 27d90f46d735ec480ecbfc029332cc5fef776d9f Mon Sep 17 00:00:00 2001 From: Dmitry Turin Date: Tue, 11 Aug 2015 10:51:25 +0300 Subject: [PATCH 2/2] SA1503: add unit tests for `lock`, `using`, `fixed` statements --- .../LayoutRules/SA1503UnitTests.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1503UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1503UnitTests.cs index 336abb696..3dd8cda79 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1503UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1503UnitTests.cs @@ -28,6 +28,9 @@ public static IEnumerable 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])" }; } } @@ -412,13 +415,15 @@ 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); } @@ -426,15 +431,17 @@ public void Bar(int i) 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); }