static bool HasPredicateVersion(IMethodSymbol member) { if (!ReplaceWithSingleCallToAnyAnalyzer.IsQueryExtensionClass(member.ContainingType)) { return(false); } return(member.Name == "Last"); }
static bool TryGetDiagnostic(SyntaxNodeAnalysisContext nodeContext, out Diagnostic diagnostic) { diagnostic = default(Diagnostic); if (nodeContext.IsFromGeneratedCode()) { return(false); } var anyInvoke = nodeContext.Node as InvocationExpressionSyntax; var info = nodeContext.SemanticModel.GetSymbolInfo(anyInvoke); IMethodSymbol anyResolve = info.Symbol as IMethodSymbol; if (anyResolve == null) { anyResolve = info.CandidateSymbols.OfType <IMethodSymbol>().FirstOrDefault(candidate => HasPredicateVersion(candidate)); } if (anyResolve == null || !HasPredicateVersion(anyResolve)) { return(false); } ExpressionSyntax target; InvocationExpressionSyntax whereInvoke; if (!ReplaceWithSingleCallToAnyAnalyzer.MatchWhere(anyInvoke, out target, out whereInvoke)) { return(false); } info = nodeContext.SemanticModel.GetSymbolInfo(whereInvoke); IMethodSymbol whereResolve = info.Symbol as IMethodSymbol; if (whereResolve == null) { whereResolve = info.CandidateSymbols.OfType <IMethodSymbol>().FirstOrDefault(candidate => candidate.Name == "Where" && ReplaceWithSingleCallToAnyAnalyzer.IsQueryExtensionClass(candidate.ContainingType)); } if (whereResolve == null || whereResolve.Name != "Where" || !ReplaceWithSingleCallToAnyAnalyzer.IsQueryExtensionClass(whereResolve.ContainingType)) { return(false); } if (whereResolve.Parameters.Length != 1) { return(false); } var predResolve = whereResolve.Parameters[0]; if (predResolve.Type.GetTypeParameters().Length != 2) { return(false); } diagnostic = Diagnostic.Create( descriptor, anyInvoke.GetLocation() ); return(true); }