private void AnalyzeIfOrElseStatement(SyntaxNodeAnalysisContext context) { if (context.IsGeneratedOrNonUserCode()) { return; } IfStatementSyntax ifStatement = context.Node as IfStatementSyntax; // Is this an if statement followed directly by a call with no braces? if ((ifStatement != null) && (ifStatement.Statement != null) && (ifStatement.Statement.IsKind(SyntaxKind.Block) == false)) { Location loc = ifStatement.GetLocation(); Diagnostic diagnostic = Diagnostic.Create(Rule, loc, "if"); context.ReportDiagnostic(diagnostic); } // Is this an else clause followed by a call with no braces? ElseClauseSyntax elseSyntax = context.Node as ElseClauseSyntax; if ((elseSyntax != null) && (elseSyntax.Statement != null) && (elseSyntax.Statement.IsKind(SyntaxKind.IfStatement) == false) && (elseSyntax.Statement.IsKind(SyntaxKind.Block) == false)) { Location loc = elseSyntax.GetLocation(); Diagnostic diagnostic = Diagnostic.Create(Rule, loc, "else"); context.ReportDiagnostic(diagnostic); } }
private static void ReportIfFalse(IfStatementSyntax ifStatement, SyntaxNodeAnalysisContext context) { var location = ifStatement.Else == null ? ifStatement.GetLocation() : Location.Create( ifStatement.SyntaxTree, new TextSpan(ifStatement.IfKeyword.SpanStart, ifStatement.Else.ElseKeyword.Span.End - ifStatement.IfKeyword.SpanStart)); context.ReportDiagnostic(Diagnostic.Create(rule, location, ifStatementLiteral)); }
public static void Analyze(SyntaxNodeAnalysisContext context, IfStatementSyntax ifStatement) { if (CanRefactor(ifStatement, context.SemanticModel, context.CancellationToken) && !ifStatement.SpanContainsDirectives()) { context.ReportDiagnostic( DiagnosticDescriptors.ReplaceIfStatementWithAssignment, ifStatement.GetLocation()); } }
public override void Initialize(AnalysisContext context) { context.RegisterSyntaxNodeAction( c => { IfStatementSyntax ifNode = (IfStatementSyntax)c.Node; if (IsElseIfWithoutElse(ifNode)) { c.ReportDiagnostic(Diagnostic.Create(Rule, ifNode.GetLocation())); } }, SyntaxKind.IfStatement); }
public override void Initialize(AnalysisContext context) { context.RegisterSyntaxNodeAction( c => { IfStatementSyntax ifNode = (IfStatementSyntax)c.Node; if (HasBooleanLiteralExpressionAsCondition(ifNode)) { c.ReportDiagnostic(Diagnostic.Create(Rule, ifNode.GetLocation())); } }, SyntaxKind.IfStatement); }
private static void ReportDiagnostic(SyntaxNodeAnalysisContext context, IfStatementSyntax ifStatement, IfStatementSyntax nestedIf) { context.ReportDiagnostic( DiagnosticDescriptors.MergeIfStatementWithNestedIfStatement, ifStatement.GetLocation()); context.FadeOutToken(FadeOutDescriptor, nestedIf.IfKeyword); context.FadeOutToken(FadeOutDescriptor, nestedIf.OpenParenToken); context.FadeOutToken(FadeOutDescriptor, nestedIf.CloseParenToken); if (ifStatement.Statement.IsKind(SyntaxKind.Block) && nestedIf.Statement.IsKind(SyntaxKind.Block)) { context.FadeOutBraces(FadeOutDescriptor, (BlockSyntax)nestedIf.Statement); } }
public override void VisitIfStatement(IfStatementSyntax node) { if (entryNode is AnonymousFunctionExpressionSyntax && embeddedNode is AnonymousFunctionExpressionSyntax) { return; } if (_weComeFromMethod && _weInAnonymousMethod) { return; } InsertLLOCMap(node.GetLocation()); if (node.Else != null) { InsertLLOCMap(node.Else.GetLocation()); } InsertLLOCMap(node.OpenParenToken.GetLocation()); InsertLLOCMap(node.CloseParenToken.GetLocation()); base.VisitIfStatement(node); }
public static void Analyze(SyntaxNodeAnalysisContext context, IfStatementSyntax ifStatement) { if (IfElseChainAnalysis.IsIsolatedIf(ifStatement) && CheckCondition(ifStatement.Condition)) { IfStatementSyntax ifStatement2 = GetContainedIfStatement(ifStatement); if (ifStatement2 != null && ifStatement2.Else == null && CheckCondition(ifStatement2.Condition)) { context.ReportDiagnostic( DiagnosticDescriptors.MergeIfStatementWithContainedIfStatement, ifStatement.GetLocation()); FadeOut(context, ifStatement, ifStatement2); } } }
public static void Analyze(SyntaxNodeAnalysisContext context, IfStatementSyntax ifStatement) { if (IfElseAnalysis.IsIsolatedIf(ifStatement) && ConditionAllowsMerging(ifStatement.Condition)) { IfStatementSyntax nestedIf = GetNestedIfStatement(ifStatement); if (nestedIf != null && nestedIf.Else == null && ConditionAllowsMerging(nestedIf.Condition) && TriviaAllowsMerging(ifStatement, nestedIf)) { context.ReportDiagnostic( DiagnosticDescriptors.MergeIfStatementWithNestedIfStatement, ifStatement.GetLocation()); FadeOut(context, ifStatement, nestedIf); } } }
private static void AnalyzeBraces(SyntaxNodeAnalysisContext context, IfStatementSyntax ifStatement) { if (!ifStatement.IsParentKind(SyntaxKind.ElseClause) && ifStatement.Else != null) { BracesAnalysisResult result = CSharpAnalysis.AnalyzeBraces(ifStatement); if ((result & BracesAnalysisResult.AddBraces) != 0) { context.ReportDiagnostic(DiagnosticDescriptors.AddBracesToIfElse, ifStatement.GetLocation()); } if ((result & BracesAnalysisResult.RemoveBraces) != 0) { context.ReportDiagnostic(DiagnosticDescriptors.RemoveBracesFromIfElse, ifStatement.GetLocation()); foreach (SyntaxNode node in ifStatement.DescendantNodes()) { if (node.IsKind(SyntaxKind.Block)) { context.FadeOutBraces(DiagnosticDescriptors.RemoveBracesFromIfElseFadeOut, (BlockSyntax)node); } } } } }
private static void ReportIfFalse(IfStatementSyntax ifStatement, SyntaxNodeAnalysisContext c) { var location = ifStatement.Else == null ? ifStatement.GetLocation() : Location.Create( ifStatement.SyntaxTree, new TextSpan(ifStatement.IfKeyword.SpanStart, ifStatement.Else.ElseKeyword.Span.End - ifStatement.IfKeyword.SpanStart)); c.ReportDiagnostic(Diagnostic.Create(Rule, location, ifStatementLiteral)); }