private void AnalyzeSyntax(SyntaxNodeAnalysisContext context) { var assignment = (AssignmentExpressionSyntax)context.Node; if (assignment.Parent is InitializerExpressionSyntax || assignment.Left == null) { return; } var memberSymbol = context.SemanticModel.GetSymbolInfo(assignment.Left).Symbol; if (memberSymbol is IPropertySymbol || memberSymbol is IFieldSymbol) { if (SymbolHelper.IsMarkedWithAttribute(memberSymbol, SmartAnnotations.InitOnly) || SymbolHelper.IsMarkedWithAttribute(memberSymbol.ContainingType, SmartAnnotations.InitOnly)) { var parentMethod = SyntaxHelper.FindNearestContainer <BaseMethodDeclarationSyntax>(assignment.Parent); if (parentMethod is ConstructorDeclarationSyntax) { var constructorSymbol = context.SemanticModel.GetDeclaredSymbol(parentMethod); if (constructorSymbol != null && constructorSymbol.ContainingType == memberSymbol.ContainingType) { return; } } var diagnostic = Diagnostic.Create(Rule, assignment.GetLocation()); context.ReportDiagnostic(diagnostic); } } }
public bool IsMarkedWithAttribute(ISymbol type, string attributeName) { if (_symbolAttributes.ContainsKey(type) == false) { _symbolAttributes.Add(type, new Dictionary <string, bool>()); } var bucket = _symbolAttributes[type]; if (bucket.ContainsKey(attributeName) == false) { bucket[attributeName] = SymbolHelper.IsMarkedWithAttribute(@type, attributeName); } return(bucket[attributeName]); }
private IEnumerable <ISymbol> GetMembersForRequiredInit(ITypeSymbol type, ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel) { var membersExtractor = new MembersExtractor(semanticModel, objectCreation); if (IsInsideInitBlockWithFullInit(objectCreation) || SymbolHelper.IsMarkedWithAttribute(type, SmartAnnotations.InitRequired) || SymbolHelper.IsMarkedWithAttribute(type, SmartAnnotations.InitOnly)) { return(membersExtractor.GetAllMembersThatCanBeInitialized(type)); } var symbolCache = new SymbolHelperCache(); return(membersExtractor.GetAllMembersThatCanBeInitialized(type).Where(memberSymbol => SymbolHelper.IsMarkedWithAttribute(memberSymbol, SmartAnnotations.InitRequired) || SymbolHelper.IsMarkedWithAttribute(memberSymbol, SmartAnnotations.InitOnly) || NonNullableShouldBeInitialized(memberSymbol, symbolCache))); }
private static bool IsInitOnlyMember(ISymbol memberSymbol) { return(SymbolHelper.IsMarkedWithAttribute(memberSymbol, SmartAnnotations.InitOnly) || SymbolHelper.IsMarkedWithAttribute(memberSymbol, SmartAnnotations.InitOnlyOptional) || SymbolHelper.IsMarkedWithAttribute(memberSymbol.ContainingType, SmartAnnotations.InitOnly)); }