private void AnalyzeObjectCreationSyntax(SyntaxNodeAnalysisContext context, [NotNull] MemberRegistry memberRegistry, [NotNull] ExternMemberHeuristic heuristic) { var objectCreationSyntax = (ObjectCreationExpressionSyntax)context.Node; var methodSymbol = context.SemanticModel.GetSymbolInfo(context.Node).Symbol as IMethodSymbol; if (methodSymbol?.MethodKind != MethodKind.Constructor) { return; } string systemTypeName = methodSymbol.ContainingType.GetCompleteTypeName(); string testableMemberName = memberRegistry.TryResolveSystemConstructor(systemTypeName); if (testableMemberName != null) { ReportInternDiagnosticAt(objectCreationSyntax.Type.GetLocation(), systemTypeName, testableMemberName, true, context.ReportDiagnostic); } else if (heuristic.HasPathAsStringParameter(methodSymbol)) { ReportExternDiagnosticAt(objectCreationSyntax.Type.GetLocation(), systemTypeName, true, context.ReportDiagnostic); } }
public override void Initialize([NotNull] AnalysisContext context) { context.RegisterCompilationStartAction(startContext => { var typeRegistry = new TypeRegistry(startContext.Compilation); if (!typeRegistry.IsComplete) { return; } var memberRegistry = new MemberRegistry(typeRegistry); var heuristic = new ExternMemberHeuristic(startContext.Compilation); startContext.RegisterSyntaxNodeAction( syntaxContext => AnalyzeMemberAccessSyntax(syntaxContext, typeRegistry, memberRegistry, heuristic), SyntaxKind.SimpleMemberAccessExpression); startContext.RegisterSyntaxNodeAction( syntaxContext => AnalyzeObjectCreationSyntax(syntaxContext, memberRegistry, heuristic), SyntaxKind.ObjectCreationExpression); startContext.RegisterSyntaxNodeAction(syntaxContext => AnalyzeClassDeclarationSyntax(syntaxContext, typeRegistry), SyntaxKind.ClassDeclaration); startContext.RegisterSyntaxNodeAction(syntaxContext => AnalyzeParameterSyntax(syntaxContext, typeRegistry), SyntaxKind.Parameter); startContext.RegisterSymbolAction(symbolContext => AnalyzeFieldSymbol(symbolContext, typeRegistry), SymbolKind.Field); startContext.RegisterSymbolAction(symbolContext => AnalyzePropertySymbol(symbolContext, typeRegistry), SymbolKind.Property); startContext.RegisterSymbolAction(symbolContext => AnalyzeMethodSymbol(symbolContext, typeRegistry), SymbolKind.Method); }); }
private void AnalyzeMemberAccessSyntax(SyntaxNodeAnalysisContext context, [NotNull] TypeRegistry typeRegistry, [NotNull] MemberRegistry memberRegistry, [NotNull] ExternMemberHeuristic heuristic) { var memberAccessSyntax = (MemberAccessExpressionSyntax)context.Node; ISymbol symbol = context.SemanticModel.GetSymbolInfo(memberAccessSyntax).Symbol; if (symbol == null || symbol.ToDisplayString() == "System.IO.Path.GetFullPath(string, string)") { return; } string systemMemberName = symbol.GetCompleteMemberName(); string testableMemberName = memberRegistry.TryResolveSystemMember(systemMemberName); if (testableMemberName != null) { Location location = GetMemberInvocationLocation(memberAccessSyntax); ReportInternDiagnosticAt(location, systemMemberName, testableMemberName, false, context.ReportDiagnostic); } else if (symbol is IMethodSymbol methodSymbol && !typeRegistry.SystemTypes.Contains(methodSymbol.ContainingType) && heuristic.HasPathAsStringParameter(methodSymbol)) { Location location = GetMemberInvocationLocation(memberAccessSyntax); ReportExternDiagnosticAt(location, systemMemberName, false, context.ReportDiagnostic); } }