private bool AnalyzeConstructorAccessibility(SubstituteContext <TInvocationExpressionSyntax> substituteContext, ConstructorContext constructorContext) { if (constructorContext.ConstructorType.TypeKind == TypeKind.Class && constructorContext.AccessibleConstructors != null && constructorContext.AccessibleConstructors.Any() == false) { var diagnostic = Diagnostic.Create( DiagnosticDescriptorsProvider.SubstituteForWithoutAccessibleConstructor, substituteContext.InvocationExpression.GetLocation(), constructorContext.ConstructorType.ToString()); substituteContext.SyntaxNodeAnalysisContext.ReportDiagnostic(diagnostic); return(true); } return(false); }
private bool AnalyzeConstructorParametersCount(SubstituteContext <TInvocationExpressionSyntax> substituteContext, ConstructorContext constructorContext) { var invocationArgumentTypes = constructorContext.InvocationParameters?.Length; switch (constructorContext.ConstructorType.TypeKind) { case TypeKind.Interface when invocationArgumentTypes > 0: var diagnostic = Diagnostic.Create( DiagnosticDescriptorsProvider.SubstituteConstructorArgumentsForInterface, substituteContext.InvocationExpression.GetLocation(), GetSubstituteMethodWithoutConstructorArguments(substituteContext.InvocationExpression, substituteContext.MethodSymbol)); substituteContext.SyntaxNodeAnalysisContext.ReportDiagnostic(diagnostic); return(true); case TypeKind.Interface: return(false); case TypeKind.Delegate when invocationArgumentTypes > 0: var delegateDiagnostic = Diagnostic.Create( DiagnosticDescriptorsProvider.SubstituteConstructorArgumentsForDelegate, substituteContext.InvocationExpression.GetLocation(), GetSubstituteMethodWithoutConstructorArguments(substituteContext.InvocationExpression, substituteContext.MethodSymbol)); substituteContext.SyntaxNodeAnalysisContext.ReportDiagnostic(delegateDiagnostic); return(true); case TypeKind.Delegate: return(false); } if (constructorContext.PossibleConstructors != null && constructorContext.PossibleConstructors.Any() == false) { var symbol = substituteContext.SyntaxNodeAnalysisContext.SemanticModel.GetSymbolInfo(substituteContext.InvocationExpression); var diagnostic = Diagnostic.Create( DiagnosticDescriptorsProvider.SubstituteForConstructorParametersMismatch, substituteContext.InvocationExpression.GetLocation(), symbol.Symbol.ToMinimalMethodString(substituteContext.SyntaxNodeAnalysisContext.SemanticModel), constructorContext.ConstructorType); substituteContext.SyntaxNodeAnalysisContext.ReportDiagnostic(diagnostic); return(true); } return(false); }
private bool AnalyzeConstructorInvocation(SubstituteContext <TInvocationExpressionSyntax> substituteContext, ConstructorContext constructorContext) { if (constructorContext.ConstructorType.TypeKind != TypeKind.Class || constructorContext.InvocationParameters == null || constructorContext.PossibleConstructors == null) { return(false); } if (constructorContext.PossibleConstructors.All(ctor => _substituteConstructorMatcher.MatchesInvocation( substituteContext.SyntaxNodeAnalysisContext.SemanticModel.Compilation, ctor, constructorContext.InvocationParameters) == false)) { var diagnostic = Diagnostic.Create( DiagnosticDescriptorsProvider.SubstituteConstructorMismatch, substituteContext.InvocationExpression.GetLocation(), substituteContext.MethodSymbol.ToMinimalMethodString(substituteContext.SyntaxNodeAnalysisContext.SemanticModel), constructorContext.ConstructorType.ToString()); substituteContext.SyntaxNodeAnalysisContext.ReportDiagnostic(diagnostic); return(true); } return(false); }
private void AnalyzeConstructor(SubstituteContext <TInvocationExpressionSyntax> substituteContext, ConstructorContext constructorContext) { if (AnalyzeConstructorAccessibility(substituteContext, constructorContext)) { return; } if (AnalyzeConstructorParametersCount(substituteContext, constructorContext)) { return; } if (AnalyzeConstructorInvocation(substituteContext, constructorContext)) { return; } }