示例#1
0
        protected void CheckTernaryExpressionBranches(SyntaxNodeAnalysisContext context,
                                                      SyntaxTree ternaryTree, SyntaxNode thenBranch, SyntaxNode elseBranch)
        {
            var thenNoParantheses = RemoveParentheses(thenBranch);
            var elseNoParantheses = RemoveParentheses(elseBranch);

            var thenIsBooleanLiteral = IsBooleanLiteral(thenNoParantheses);
            var elseIsBooleanLiteral = IsBooleanLiteral(elseNoParantheses);

            var bothSideBool  = thenIsBooleanLiteral && elseIsBooleanLiteral;
            var bothSideTrue  = IsTrueLiteralKind(thenNoParantheses) && IsTrueLiteralKind(elseNoParantheses);
            var bothSideFalse = IsFalseLiteralKind(thenNoParantheses) && IsFalseLiteralKind(elseNoParantheses);

            if (bothSideBool && !bothSideFalse && !bothSideTrue)
            {
                context.ReportDiagnosticWhenActive(Diagnostic.Create(SupportedDiagnostics[0], thenBranch.CreateLocation(elseBranch)));
            }
            if (thenIsBooleanLiteral ^ elseIsBooleanLiteral)
            {
                // one side is boolean literal, the other is NOT boolean literal
                var booleanLiteralSide = thenIsBooleanLiteral ? thenBranch : elseBranch;

                context.ReportDiagnosticWhenActive(Diagnostic.Create(SupportedDiagnostics[0], booleanLiteralSide.GetLocation()));
            }
        }