Skip to content

Instantly share code, notes, and snippets.

@controlflow
Created December 12, 2016 12:20
Show Gist options
  • Save controlflow/3dc8272d798939335a727e0bccb7ba05 to your computer and use it in GitHub Desktop.
Save controlflow/3dc8272d798939335a727e0bccb7ba05 to your computer and use it in GitHub Desktop.
foreach (var referenceExpression in codeBody.ThisAndDescendants<IReferenceExpression>())
{
var resolveResult = referenceExpression.Reference.Resolve();
if (!resolveResult.ResolveErrorType.IsAcceptable) continue;
var declaredElement = resolveResult.Result.DeclaredElement;
var field = declaredElement as IField;
if (field != null)
{
if (!field.IsReadonly && !field.IsConstant)
consumer.AddHighlighting(new NonReadonlyMemberInGetHashCodeWarning(referenceExpression, field));
}
var property = declaredElement as IProperty;
if (property != null)
{
if (property.IsAuto && property.Setter != null && referenceExpression.IsCSharp6Supported())
consumer.AddHighlighting(new NonReadonlyMemberInGetHashCodeWarning(referenceExpression, property));
}
var method = declaredElement as IMethod;
if (method.IsObjectGetHashCodeMethod() && referenceExpression.QualifierExpression.GetOperandThroughParenthesis() is IBaseExpression)
{
consumer.AddHighlighting(new BaseObjectGetHashCodeCallInGetHashCodeWarning(referenceExpression));
}
}
foreach (var referenceExpression in codeBody.ThisAndDescendants<IReferenceExpression>())
{
var resolveResult = referenceExpression.Reference.Resolve();
if (!resolveResult.ResolveErrorType.IsAcceptable) continue;
switch (resolveResult.Result.DeclaredElement)
{
case IField field when !field.IsReadonly && !field.IsConstant:
consumer.AddHighlighting(new NonReadonlyMemberInGetHashCodeWarning(referenceExpression, field));
break;
case IProperty property when property.IsAuto
&& property.Setter != null
&& referenceExpression.IsCSharp6Supported():
consumer.AddHighlighting(new NonReadonlyMemberInGetHashCodeWarning(referenceExpression, property));
break;
case IMethod method when method.IsObjectGetHashCodeMethod()
&& referenceExpression.QualifierExpression.GetOperandThroughParenthesis() is IBaseExpression:
consumer.AddHighlighting(new BaseObjectGetHashCodeCallInGetHashCodeWarning(referenceExpression));
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment