private static void AnalyzeObjectCreationExpressionStatement(SyntaxNodeAnalysisContext context) { if (!(context.Node is ObjectCreationExpressionSyntax node)) { return; //something went wrong } var symbolInfo = context.SemanticModel.GetSymbolInfo(node); var t = (symbolInfo.Symbol as IMethodSymbol)?.ReceiverType as INamedTypeSymbol; var ctx = CustomAnalysisContext.WithOriginalNode(context, DisposableSource.ObjectCreation, t, Detector, Configuration); if (!ctx.CouldDetectType()) { } else if (!ctx.IsDisposableOrImplementsDisposable()) { return; } else if (node.IsParentADisposeCallIgnoringParenthesis()) { return; //(new MemoryStream()).Dispose() } else if (Detector.IsIgnoredTypeOrImplementsIgnoredInterface(ctx.Type)) { } else if (node.IsReturnedInProperty()) { AnalyzeNodeInReturnStatementOfProperty(ctx); } else if (node.IsPartOfReturnStatementInBlock()) { } // return new MemoryStream() or return Task.FromResult(new MemoryStream()) else if (node.IsPartOfYieldReturnStatementInBlock()) { } //yield return new MemoryStream() else if (node.IsArrowExpressionClauseOfMethod()) { } // void Create()=>CreateMemoryStream() else if (node.IsReturnValueInLambdaExpression()) { } else if (node.IsReturnedLaterWithinMethod()) { } else if (node.IsReturnedLaterWithinParenthesizedLambdaExpression()) { } else if (node.IsPartOfMethodCall()) { AnalyzePartOfMethodCall(ctx); } else if (node.IsMaybePartOfMethodChainUsingTrackingExtensionMethod()) { var methodInvocation = node.Parent.Parent as InvocationExpressionSyntax; if (Detector.IsTrackingMethodCall(methodInvocation, context.SemanticModel)) { return; } } else if (node.IsArgumentInObjectCreation()) { AnalyzeNodeInArgumentList(ctx); } else if (node.IsPartIfArrayInitializerThatIsPartOfObjectCreation()) { var objectCreation = node.Parent.Parent.Parent.Parent.Parent as ObjectCreationExpressionSyntax; CheckIfObjectCreationTracksNode(ctx, objectCreation); } else if (node.IsDescendantOfUsingHeader()) { } //this have to be checked after IsArgumentInObjectCreation else if (node.IsDescendantOfVariableDeclarator()) { AnalyzeNodeWithinVariableDeclarator(ctx); } else if (node.IsPartOfAssignmentExpression()) { AnalyzeNodeInAssignmentExpression(ctx); } else if (node.IsPartOfPropertyExpressionBody()) { AnalyzeNodeInAutoPropertyOrPropertyExpressionBody(ctx); } else if (node.IsPartOfAutoProperty()) { AnalyzeNodeInAutoPropertyOrPropertyExpressionBody(ctx); } else { ctx.ReportNotDisposedAnonymousObject(); //new MemoryStream(); } }
private void ObjectCreationAction(SyntaxNodeAnalysisContext context) { var node = context.Node as ObjectCreationExpressionSyntax; if (node == null) { return; //something went wrong } var symbolInfo = context.SemanticModel.GetSymbolInfo(node); var type = (symbolInfo.Symbol as IMethodSymbol)?.ReceiverType as INamedTypeSymbol; if (type == null) { } else if (!type.IsDisposeableOrImplementsDisposable()) { return; } else if (node.IsParentADisposeCallIgnoringParenthesis()) { return; //(new MemoryStream()).Dispose() } else if (Detector.IsIgnoredTypeOrImplementsIgnoredInterface(type)) { } else if (node.IsReturnedInProperty()) { AnalyseNodeInReturnStatementOfProperty(context, node, DisposableSource.ObjectCreation); } else if (node.IsPartOfReturnStatementInBlock()) { } // return new MemoryStream() or return Task.FromResult(new MemoryStream()) else if (node.IsArrowExpressionClauseOfMethod()) { } // void Create()=>CreateMemoryStream() else if (node.IsReturnValueInLambdaExpression()) { } else if (node.IsReturnedLaterWithinMethod()) { } else if (node.IsReturnedLaterWithinParenthesizedLambdaExpression()) { } else if (!type.IsDisposeableOrImplementsDisposable()) { } else if (node.IsPartOfMethodCall()) { var methodInvocation = node.Parent.Parent.Parent as InvocationExpressionSyntax; if (Detector.IsTrackingMethodCall(methodInvocation, context.SemanticModel)) { return; } var rule = GetRule(DiagnosticId, CurrentSeverity); context.ReportNotDisposedAnonymousObject(rule, DisposableSource.ObjectCreation); } else if (node.IsMaybePartOfMethodChainUsingTrackingExtensionMethod()) { var methodInvokation = node.Parent.Parent as InvocationExpressionSyntax; if (Detector.IsTrackingMethodCall(methodInvokation, context.SemanticModel)) { return; } } else if (node.IsArgumentInObjectCreation()) { AnalyseNodeInArgumentList(context, node, DisposableSource.ObjectCreation); } else if (node.IsPartIfArrayInitializerThatIsPartOfObjectCreation()) { var objectCreation = node.Parent.Parent.Parent.Parent.Parent as ObjectCreationExpressionSyntax; CheckIfObjectCreationTracksNode(context, objectCreation, DisposableSource.ObjectCreation); } else if (node.IsDescendantOfUsingHeader()) { } //this have to be checked after IsArgumentInObjectCreation else if (node.IsDescendantOfVariableDeclarator()) { AnalyseNodeWithinVariableDeclarator(context, node, DisposableSource.ObjectCreation); } else if (node.IsPartOfAssignmentExpression()) { AnalyseNodeInAssignmentExpression(context, node, DisposableSource.ObjectCreation); } else if (node.IsPartOfPropertyExpressionBody()) { AnalyseNodeInAutoPropertyOrPropertyExpressionBody(context, node, DisposableSource.ObjectCreation); } else if (node.IsPartOfAutoProperty()) { AnalyseNodeInAutoPropertyOrPropertyExpressionBody(context, node, DisposableSource.ObjectCreation); } else { //new MemoryStream(); var rule = GetRule(DiagnosticId, CurrentSeverity); context.ReportNotDisposedAnonymousObject(rule, DisposableSource.ObjectCreation); } }