예제 #1
0
        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var invocation   = (InvocationExpressionSyntax)c.Node;
                var memberAccess = invocation.Expression as MemberAccessExpressionSyntax;
                if (memberAccess == null)
                {
                    return;
                }

                var fieldSymbol = c.SemanticModel.GetSymbolInfo(memberAccess.Expression).Symbol as IFieldSymbol;
                if (fieldSymbol == null ||
                    !DisposableMemberInNonDisposableClass.IsNonStaticNonPublicDisposableField(fieldSymbol) ||
                    !DisposableMemberInNonDisposableClass.ImplementsIDisposable(fieldSymbol.ContainingType))
                {
                    return;
                }

                var methodSymbol = c.SemanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol;
                if (methodSymbol == null)
                {
                    return;
                }

                var disposeMethod = DisposableNotDisposed.GetDisposeMethod(c.SemanticModel.Compilation);
                if (disposeMethod == null)
                {
                    return;
                }

                if (!methodSymbol.Equals(
                        methodSymbol.ContainingType.FindImplementationForInterfaceMember(disposeMethod)))
                {
                    return;
                }

                var enclosingSymbol = c.SemanticModel.GetEnclosingSymbol(invocation.SpanStart);
                if (enclosingSymbol == null)
                {
                    return;
                }

                var enclosingMethodSymbol = enclosingSymbol as IMethodSymbol;
                if (enclosingMethodSymbol == null ||
                    enclosingMethodSymbol.Name != DisposeMethodName)
                {
                    c.ReportDiagnostic(Diagnostic.Create(Rule, memberAccess.Name.GetLocation()));
                }
            },
                SyntaxKind.InvocationExpression);
        }