Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. void Method()
  2. {
  3. bool hasInstanceUsage = false;
  4. bool hasStaticUsage = false;
  5.  
  6. foreach (var identifier in outerClass.DescendantNodes().OfType<IdentifierNameSyntax>())
  7. {
  8. var memberSymbol = context.SemanticModel.GetSymbolInfo(identifier);
  9. if (memberSymbol.Symbol.Equals(propertySymbol))
  10. {
  11. var constructor = identifier.Ancestors().OfType<ConstructorDeclarationSyntax>().FirstOrDefault();
  12. var isInConstructor = constructor != null;
  13. var isAssignmentExpression = identifier.Ancestors().OfType<AssignmentExpressionSyntax>().FirstOrDefault() != null;
  14.  
  15. // Skip anything that isn't a setter
  16. if (!isAssignmentExpression)
  17. {
  18. continue;
  19. }
  20.  
  21. // if it is a setter but outside the constructor, we don't report any diagnostic
  22. if (!isInConstructor)
  23. {
  24. return;
  25. }
  26.  
  27. var isStaticConstructor = context.SemanticModel.GetDeclaredSymbol(constructor).IsStatic;
  28. if (isStaticConstructor && isStaticProperty)
  29. {
  30. hasStaticUsage = true;
  31. }
  32.  
  33. if (!isStaticConstructor && !isStaticProperty)
  34. {
  35. hasInstanceUsage = true;
  36. }
  37. }
  38. }
  39.  
  40. // We can't set it to readonly if it's set in both the instance and the static constructor
  41. // We need a NAND operation: either it's never set, it's set in ctor 1 or it's set in ctor 2
  42. if (!(hasStaticUsage & hasInstanceUsage))
  43. {
  44. context.ReportDiagnostic(Diagnostic.Create(Rule, property.Identifier.GetLocation(), propertySymbol.Name));
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement