private static void CheckForRedundantConditions(CSharpExplodedGraph explodedGraph, SyntaxNodeAnalysisContext context)
        {
            var conditionTrue  = new HashSet <SyntaxNode>();
            var conditionFalse = new HashSet <SyntaxNode>();

            void collectConditions(object sender, ConditionEvaluatedEventArgs args) =>
            CollectConditions(args, conditionTrue, conditionFalse, context.SemanticModel);

            void explorationEnded(object sender, EventArgs args) => Enumerable.Empty <Diagnostic>()
            .Union(conditionTrue
                   .Except(conditionFalse)
                   .Where(c => !IsConditionOfLoopWithBreak((ExpressionSyntax)c))
                   .Where(c => !IsInsideCatchOrFinallyBlock(c))
                   .Select(node => GetDiagnostics(node, true)))
            .Union(conditionFalse
                   .Except(conditionTrue)
                   .Where(c => !IsInsideCatchOrFinallyBlock(c))
                   .Select(node => GetDiagnostics(node, false)))
            .ToList()
            .ForEach(d => context.ReportDiagnosticWhenActive(d));

            explodedGraph.ExplorationEnded   += explorationEnded;
            explodedGraph.ConditionEvaluated += collectConditions;

            try
            {
                explodedGraph.Walk();
            }
            finally
            {
                explodedGraph.ExplorationEnded   -= explorationEnded;
                explodedGraph.ConditionEvaluated -= collectConditions;
            }
        }
Esempio n. 2
0
        private static void CheckForNullDereference(CSharpExplodedGraph explodedGraph, SyntaxNodeAnalysisContext context)
        {
            var methodSymbol = context.SemanticModel.GetSymbolInfo(context.Node).Symbol
                               ?? context.SemanticModel.GetDeclaredSymbol(context.Node);

            if (!methodSymbol.IsPubliclyAccessible())
            {
                return;
            }

            var nullPointerCheck = new NullPointerDereference.NullPointerCheck(explodedGraph);

            explodedGraph.AddExplodedGraphCheck(nullPointerCheck);

            var identifiers = new HashSet <IdentifierNameSyntax>();

            void memberAccessingHandler(object sender, MemberAccessingEventArgs args) => CollectMemberAccesses(args, identifiers, context.SemanticModel);

            nullPointerCheck.MemberAccessing += memberAccessingHandler;

            try
            {
                explodedGraph.Walk();
            }
            finally
            {
                nullPointerCheck.MemberAccessing -= memberAccessingHandler;
            }

            foreach (var identifier in identifiers)
            {
                context.ReportDiagnosticWhenActive(Diagnostic.Create(rule, identifier.GetLocation(), identifier.Identifier.ValueText));
            }
        }
        private static void CheckForRedundantConditions(CSharpExplodedGraph explodedGraph, SyntaxNodeAnalysisContext context)
        {
            var conditionTrue     = new HashSet <SyntaxNode>();
            var conditionFalse    = new HashSet <SyntaxNode>();
            var hasYieldStatement = false;

            void instructionProcessed(object sender, InstructionProcessedEventArgs args)
            {
                hasYieldStatement = hasYieldStatement || IsYieldNode(args.ProgramPoint.Block);
            }

            void collectConditions(object sender, ConditionEvaluatedEventArgs args) =>
            CollectConditions(args, conditionTrue, conditionFalse, context.SemanticModel);

            void explorationEnded(object sender, EventArgs args)
            {
                // Do not raise issue in generator functions (See #1295)
                if (hasYieldStatement)
                {
                    return;
                }
                Enumerable.Empty <Diagnostic>()
                .Union(conditionTrue
                       .Except(conditionFalse)
                       .Where(c => !IsConditionOfLoopWithBreak((ExpressionSyntax)c))
                       .Where(c => !IsInsideCatchOrFinallyBlock(c))
                       .Select(node => GetDiagnostics(node, true)))
                .Union(conditionFalse
                       .Except(conditionTrue)
                       .Where(c => !IsInsideCatchOrFinallyBlock(c))
                       .Select(node => GetDiagnostics(node, false)))
                .ToList()
                .ForEach(d => context.ReportDiagnosticWhenActive(d));
            }

            explodedGraph.InstructionProcessed += instructionProcessed;
            explodedGraph.ExplorationEnded     += explorationEnded;
            explodedGraph.ConditionEvaluated   += collectConditions;

            try
            {
                explodedGraph.Walk();
            }
            finally
            {
                explodedGraph.InstructionProcessed -= instructionProcessed;
                explodedGraph.ExplorationEnded     -= explorationEnded;
                explodedGraph.ConditionEvaluated   -= collectConditions;
            }
        }