private void LookForMultiplePropertyUsedWithInappropriateValueOrValuesProperty(SyntaxNodeAnalysisContext context)
{
var initializer = context.Node as InitializerExpressionSyntax;
if (initializer == null)
return;
var propertyInitialisers = initializer.ChildNodes()
.OfType<AssignmentExpressionSyntax>()
.Select(propertyInitialiser => new
{
PropertyName = ((IdentifierNameSyntax)propertyInitialiser.Left).Identifier.ValueText,
ValueExpression = propertyInitialiser.Right
});
var multiplePropertyInitialiserIfAny = propertyInitialisers.FirstOrDefault(propertyInitialiser => propertyInitialiser.PropertyName == "Multiple");
bool multiplePropertyValue;
if (multiplePropertyInitialiserIfAny == null)
{
// If there's no explicit "Multiple" property setter then presume that it's false, since that's what it will default to. This could result
// in a false positive warnings if a SelectAttributes instance is created and "Values" set in the initialiser and then "Multiple" set to
// true in a separate statement but the code would (imo) be clearer if both properties were set initially, together, so I'm not upset
// about that particular false positive.
multiplePropertyValue = false;
}
else
{
if (multiplePropertyInitialiserIfAny.ValueExpression.Kind() == SyntaxKind.TrueLiteralExpression)
multiplePropertyValue = true;
else if (multiplePropertyInitialiserIfAny.ValueExpression.Kind() == SyntaxKind.FalseLiteralExpression)
multiplePropertyValue = false;
else
{
// This analyser is only looking for simple cases, where Multiple is set to true or false (or not set at all) - if it's set according
// to a method return value or set to a variable value then all bets are off and we'll bail now (this is just intended to catch obvious
// mistakes, not to perform deep and complex analysis on calling code)
return;
}
}
var parentObjectCreation = initializer.Parent as ObjectCreationExpressionSyntax;
if (parentObjectCreation == null)
return;
if (multiplePropertyValue && propertyInitialisers.Any(propertyInitialiser => propertyInitialiser.PropertyName == "Value"))
{
if (TargetTypeIsSelectAttributes(parentObjectCreation, context))
{
context.ReportDiagnostic(Diagnostic.Create(
DoNotUseValueWhenMultipleIsTrueRule,
parentObjectCreation.Initializer.GetLocation()
));
}
}
else if (!multiplePropertyValue && propertyInitialisers.Any(propertyInitialiser => propertyInitialiser.PropertyName == "Values"))
{
if (TargetTypeIsSelectAttributes(parentObjectCreation, context))
{
context.ReportDiagnostic(Diagnostic.Create(
DoNotUseValuesWhenMultipleIsFalseRule,
parentObjectCreation.Initializer.GetLocation()
));
}
}
}