Skip to content

Instantly share code, notes, and snippets.

Created December 24, 2012 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4369136 to your computer and use it in GitHub Desktop.
Save anonymous/4369136 to your computer and use it in GitHub Desktop.
let isBadUseOfCount (node:BinaryExpressionSyntax) (model: SemanticModel) =
// Are we checking for ==, >= or !=?
let isEquality = match node.Kind with
| SyntaxKind.EqualsExpression
| SyntaxKind.NotEqualsExpression
| SyntaxKind.GreaterThanExpression -> true
| _-> false
if not isEquality then
None
else
//Is either side a literal (ie a value)?
let literal = match node.Left, node.Right with
| LiteralExpression expr, _ -> Some(expr)
| _, LiteralExpression expr -> Some(expr)
| _, _ -> None
//Is the literal a 0
let literal = match literal with
| Some(literal) when literal.Token.ValueText = "0" -> Some(literal)
| _ -> None
match literal with
| Some(_) ->
//Is either side a method invocation
let invocation = match node.Left, node.Right with
| InvocationExpression expr, _ -> Some(expr.Expression)
| _, InvocationExpression expr -> Some(expr.Expression)
| _ -> None
//Is the invocation of Count()
let memberAccess = match invocation with
| Some(MemberAccessExpression(expr)) when expr.Name.Identifier.ValueText = "Count" -> Some(expr)
| _ -> None
//AST contains no type information!
//Must use semantic model
match memberAccess with
| Some(memberAccess) -> let typeInfo = model.GetTypeInfo(memberAccess.Expression)
if (typeInfo.Type.MetadataName = "IEnumerable`1") then
Some(memberAccess)
else
None
| None -> None
| None -> None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment