Exemplo n.º 1
0
        private static void ReportStaticOrNonStaticNotDisposedMember(CustomAnalysisContext context, string variableName)
        {
            var node            = context.Node;
            var containingClass = node.FindContainingClass();

            if (containingClass == null)
            {
                return;
            }
            var fieldDeclarationSyntax = containingClass.FindFieldNamed(variableName);

            if (fieldDeclarationSyntax != null)
            {
                if (fieldDeclarationSyntax.IsStatic())
                {
                    context.ReportNotDisposedStaticField(variableName);
                }
                else
                {
                    context.ReportNotDisposedField(variableName);
                }
            }
            else
            {
                var prop = containingClass.FindPropertyNamed(variableName);
                if (prop == null)
                {
                    return;               //this should never happen
                }
                if (prop.IsStatic())
                {
                    context.ReportNotDisposedStaticProperty(variableName);
                }
                else
                {
                    context.ReportNotDisposedProperty(variableName);
                }
            }
        }
Exemplo n.º 2
0
        private static void AnalyzeNodeInFieldDeclaration(CustomAnalysisContext context,
                                                          string fieldName)
        {
            if (context.Node.IsDisposedInDisposingMethod(fieldName, Configuration, context.SemanticModel))
            {
                return;
            }

            var containeringClass = context.Node.FindContainingClass();
            var fieldDeclaration  = containeringClass?.FindFieldNamed(fieldName);

            if (fieldDeclaration == null)
            {
                return;
            }
            if (fieldDeclaration.IsStatic())
            {
                context.ReportNotDisposedStaticField(fieldName);
            }
            else
            {
                context.ReportNotDisposedField(fieldName);
            }
        }
Exemplo n.º 3
0
        private static void AnalyzeNodeInAssignmentExpression(CustomAnalysisContext context)
        {
            var node = context.Node;
            //is local or global variable
            var assignmentExpressionSyntax = node.Parent as AssignmentExpressionSyntax;
            var variableName = (assignmentExpressionSyntax?.Left as IdentifierNameSyntax)?.Identifier.Text;

            if (node.TryFindContainingMethod(out var containingMethod))
            {
                if (containingMethod.ContainsDisposeCallFor(variableName, context.SemanticModel, Configuration))
                {
                    return;
                }

                if (containingMethod.HasDecendentVariableDeclaratorFor(variableName))
                {
                    //local declaration in method
                    if (containingMethod.Returns(variableName))
                    {
                        return;
                    }
                    if (node.IsDescendantOfUsingHeader())
                    {
                        return;
                    }
                    if (node.IsArgumentInObjectCreation())
                    {
                        AnalyzeNodeInArgumentList(context);
                        return;
                    }

                    if (containingMethod.HasInterlockedExchangeWith(variableName))
                    {
                        return;
                    }
                    //is part of tracking call
                    context.ReportNotDisposedLocalVariable();
                    return;
                }
                if (node.IsDisposedInDisposingMethod(variableName, Configuration, context.SemanticModel))
                {
                    return;
                }
                if (node.IsArgumentInObjectCreation())
                {
                    AnalyzeNodeInArgumentList(context);
                    return;
                }

                //assignment to field or property
                ReportStaticOrNonStaticNotDisposedMember(context, variableName);

                return;
            }

            if (node.TryFindContainingCtor(out var ctor))
            {
                if (ctor.HasDecendentVariableDeclaratorFor(variableName))
                {
                    //local variable in ctor
                    if (ctor.HasInterlockedExchangeWith(variableName))
                    {
                        return;
                    }
                    if (node.IsDescendantOfUsingHeader())
                    {
                        return;
                    }
                    if (node.IsArgumentInObjectCreation())
                    {
                        AnalyzeNodeInArgumentList(context);
                        return;
                    }
                    if (ctor.ContainsDisposeCallFor(variableName, context.SemanticModel, Configuration))
                    {
                        return;
                    }
                    context.ReportNotDisposedLocalVariable();
                }
                else //field or property
                {
                    if (node.IsDisposedInDisposingMethod(variableName, Configuration, context.SemanticModel))
                    {
                        return;
                    }

                    if (node.IsAssignmentToProperty(variableName, out var isStatic))
                    {
                        if (isStatic)
                        {
                            context.ReportNotDisposedStaticProperty(variableName);
                        }
                        else
                        {
                            context.ReportNotDisposedProperty(variableName);
                        }
                    }
                    else
                    {
                        if (node.IsAssignmentToStaticField(variableName))
                        {
                            context.ReportNotDisposedStaticField(variableName);
                        }
                        else
                        {
                            context.ReportNotDisposedField(variableName);
                        }
                    }
                }
            }
        }