private IEnumerable <Diagnostic> Analyze(INamedTypeSymbol marker, INamedTypeSymbol target, SemanticModel semanticModel, VariableDeclarationSyntax varDeclSyntax) { if (semanticModel.GetSymbolInfo(varDeclSyntax.Type).Symbol is not INamedTypeSymbol type) { yield break; } if (!type.IsGenericType || type.GetAttributes() .FirstOrDefault(a => SymbolEqualityComparer .Default.Equals(a.AttributeClass, marker)) is not AttributeData genericTypeAttribute) { yield break; } if (genericTypeAttribute.ConstructorArguments.FirstOrDefault().Value is not int genericIndex) { yield break; } if (type.TypeArguments.ElementAtOrDefault(genericIndex) is not INamedTypeSymbol targetType) { yield break; } if (!targetType.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, target))) { yield return(Diagnostic.Create(Rule, varDeclSyntax.GetLocation(), targetType.Name)); } }
private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context) { VariableDeclarationSyntax variableDeclaration = (context.Node as LocalDeclarationStatementSyntax).Declaration; bool isTypeApparent = IsTypeApparentInDeclaration(variableDeclaration, context.SemanticModel, context.CancellationToken); if (isTypeApparent && !variableDeclaration.Type.IsVar) { context.ReportDiagnostic( Diagnostic.Create( _rule, variableDeclaration.GetLocation(), "Implicit type decalaration should be used")); } if (!isTypeApparent && variableDeclaration.Type.IsVar) { context.ReportDiagnostic( Diagnostic.Create( _rule, variableDeclaration.GetLocation(), "Explicit type decalaration should be used")); } }
private static void AnalyzeVariable(SyntaxNodeAnalysisContext context) { VariableDeclarationSyntax node = (VariableDeclarationSyntax)context.Node; if (Helper.IsInTestClass(context)) { return; } var variables = node.Variables; if (!variables.Any()) { return; } foreach (var variable in variables) { if (!IsPositiveName(variable.Identifier.Text)) { CreateDiagnostic(context, node.GetLocation()); } } }