/// <summary>
        /// Validates the given lambda to see if it is a valid LINQ to Entities expression
        /// </summary>
        /// <param name="lambda">The lambda syntax node</param>
        /// <param name="rootQueryableType">The type of the IQueryable instance where a known LINQ operator is invoked on with this lambda</param>
        /// <param name="context">The analysis context</param>
        /// <param name="efContext">The EF-specific view of the semantic model</param>
        /// <param name="treatAsWarning">If true, instructs any diagnostic reports to be flagged as warnings instead of errors. This is normally true when the analyzer cannot fully determine that the LINQ expression is made against an actual DbSet</param>
        public static void ValidateLinqToEntitiesExpression(LambdaExpressionSyntax lambda, EFCodeFirstClassInfo rootQueryableType, SyntaxNodeAnalysisContext context, EFUsageContext efContext, bool treatAsWarning = false)
        {
            var descendants    = lambda.DescendantNodes();
            var parameterNodes = ContextualLinqParameter.BuildContext(descendants.OfType <ParameterSyntax>(), context, efContext);

            ValidateLinqToEntitiesUsageInSyntaxNodes(descendants, rootQueryableType, context, efContext, parameterNodes, treatAsWarning);
        }
예제 #2
0
            private void LambdaExpressionHandler(LambdaExpressionSyntax node)
            {
                LambdaMetricsResults.LambdaCount++;

                IEnumerable <IdentifierNameSyntax> variablesUsed = node
                                                                   .DescendantNodes()
                                                                   .OfType <IdentifierNameSyntax>();

                foreach (IdentifierNameSyntax variableUsed in variablesUsed)
                {
                    var symbol = _semanticModel.GetSymbolInfo(variableUsed);

                    if (symbol.Symbol is null)
                    {
                        continue;
                    }

                    switch (symbol.Symbol.Kind)
                    {
                    case SymbolKind.Local when !((ILocalSymbol)symbol.Symbol).HasConstantValue:
                        LambdaMetricsResults.LocalVariableUsageCount++;
                        break;

                    case SymbolKind.Field when !((IFieldSymbol)symbol.Symbol).HasConstantValue:
                        LambdaMetricsResults.FieldVariableUsageCount++;
                        break;

                    default:
                        continue;
                    }

                    if (IsMutated(variableUsed))
                    {
                        LambdaMetricsResults.SideEffects++;
                    }
                }
            }
 /// <summary>
 /// Validates the given lambda to see if it is a valid LINQ to Entities expression
 /// </summary>
 /// <param name="lambda">The lambda syntax node</param>
 /// <param name="rootQueryableType">The type of the IQueryable instance where a known LINQ operator is invoked on with this lambda</param>
 /// <param name="context">The analysis context</param>
 /// <param name="efContext">The EF-specific view of the semantic model</param>
 /// <param name="treatAsWarning">If true, instructs any diagnostic reports to be flagged as warnings instead of errors. This is normally true when the analyzer cannot fully determine that the LINQ expression is made against an actual DbSet</param>
 public static void ValidateLinqToEntitiesExpression(LambdaExpressionSyntax lambda, EFCodeFirstClassInfo rootQueryableType, SyntaxNodeAnalysisContext context, EFUsageContext efContext, bool treatAsWarning = false)
 {
     var descendants = lambda.DescendantNodes();
     var parameterNodes = ContextualLinqParameter.BuildContext(descendants.OfType<ParameterSyntax>(), context, efContext);
     ValidateLinqToEntitiesUsageInSyntaxNodes(descendants, rootQueryableType, context, efContext, parameterNodes, treatAsWarning);
 }