private static bool IsDisposableField(ExpressionSyntax expression, SemanticModel semanticModel) { var fieldSymbol = semanticModel.GetSymbolInfo(expression).Symbol as IFieldSymbol; return(fieldSymbol != null && DisposableMemberInNonDisposableClass.IsNonStaticNonPublicDisposableField(fieldSymbol) && fieldSymbol.ContainingType.Implements(KnownType.System_IDisposable)); }
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); }
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)); }
private static void CollectDisposableFields(FieldDeclarationSyntax field, SemanticModel semanticModel, ref ImmutableHashSet <IFieldSymbol> disposableFields, ref ImmutableHashSet <IFieldSymbol> fieldsAssigned) { foreach (var variableDeclaratorSyntax in field.Declaration.Variables) { var fieldSymbol = semanticModel.GetDeclaredSymbol(variableDeclaratorSyntax) as IFieldSymbol; if (!DisposableMemberInNonDisposableClass.IsNonStaticNonPublicDisposableField(fieldSymbol)) { continue; } disposableFields = disposableFields.Add(fieldSymbol); if (variableDeclaratorSyntax.Initializer == null || !(variableDeclaratorSyntax.Initializer.Value is ObjectCreationExpressionSyntax)) { return; } fieldsAssigned = fieldsAssigned.Add(fieldSymbol); } }
private static bool IsDisposableLocalSymbol(ILocalSymbol localSymbol) { return(localSymbol != null && DisposableMemberInNonDisposableClass.ImplementsIDisposable(localSymbol.Type as INamedTypeSymbol)); }