コード例 #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);
        }
コード例 #2
0
        private static bool IsDisposableNotLocalIdentifier(IdentifierNameSyntax identifier, SemanticModel semanticModel)
        {
            var expressionSymbol = semanticModel.GetSymbolInfo(identifier).Symbol;

            if (expressionSymbol == null)
            {
                return(true);
            }

            var localSymbol    = expressionSymbol as ILocalSymbol;
            var expressionType = semanticModel.GetTypeInfo(identifier).Type;

            return(localSymbol == null &&
                   expressionType != null &&
                   DisposableMemberInNonDisposableClass.ImplementsIDisposable(expressionType as INamedTypeSymbol));
        }
コード例 #3
0
 private static bool IsDisposableLocalSymbol(ILocalSymbol localSymbol)
 {
     return(localSymbol != null &&
            DisposableMemberInNonDisposableClass.ImplementsIDisposable(localSymbol.Type as INamedTypeSymbol));
 }