Exemplo n.º 1
0
 private static void ReportParameterCount(BaseAnalysisContext <ISymbol> context, [NotNull] string name, int parameterCount)
 {
     if (!context.Target.IsSynthesized())
     {
         Diagnostic diagnostic = Diagnostic.Create(ParameterCountRule, context.Target.Locations[0], name, parameterCount);
         context.ReportDiagnostic(diagnostic);
     }
 }
Exemplo n.º 2
0
            public ParameterCountInfo(BaseAnalysisContext <TTarget> context, [NotNull] ParameterSettings settings,
                                      bool isConstructor = false)
            {
                Guard.NotNull(settings, nameof(settings));

                Context            = context;
                this.settings      = settings;
                this.isConstructor = isConstructor;
            }
Exemplo n.º 3
0
 private static void ReportTupleReturn(BaseAnalysisContext <ISymbol> context, [NotNull] string memberName,
                                       int tupleElementCount)
 {
     if (!context.Target.IsSynthesized())
     {
         var diagnostic = Diagnostic.Create(TupleReturnRule, context.Target.Locations[0], memberName, tupleElementCount);
         context.ReportDiagnostic(diagnostic);
     }
 }
Exemplo n.º 4
0
 private static void ReportTupleParameter(BaseAnalysisContext <IParameterSymbol> context, [NotNull] string memberName,
                                          [NotNull] string parameterName)
 {
     if (!context.Target.IsSynthesized())
     {
         var diagnostic = Diagnostic.Create(TupleParameterRule, context.Target.Locations[0], memberName, parameterName);
         context.ReportDiagnostic(diagnostic);
     }
 }
Exemplo n.º 5
0
        private static void AnalyzeReturnType(BaseAnalysisContext <ITypeSymbol> context, [NotNull] ISymbol member,
                                              [NotNull] string memberName)
        {
            int?elementCount = TryGetValueTupleElementCount(context.Target) ?? TryGetSystemTupleElementCount(context.Target);

            if (elementCount > 2)
            {
                ReportTupleReturn(context.WithTarget(member), memberName, elementCount.Value);
            }
        }
Exemplo n.º 6
0
        private static void InnerAnalyzeMethod(BaseAnalysisContext <IMethodSymbol> context,
                                               [NotNull] DiagnosticCollector collector)
        {
            SyntaxNode bodySyntax = context.Target.TryGetBodySyntaxForMethod(context.CancellationToken);

            if (bodySyntax == null)
            {
                return;
            }

            AnalyzeParametersInMethod(context.WithTarget(context.Target.Parameters), bodySyntax, collector);
        }
Exemplo n.º 7
0
        private static void AnalyzeStructParameters(BaseAnalysisContext <ICollection <IParameterSymbol> > context,
                                                    [NotNull] SyntaxNode bodySyntax, [NotNull] DiagnosticCollector collector)
        {
            // A user-defined struct can reassign its 'this' parameter on invocation. That's why the compiler dataflow
            // analysis reports all access as writes. Because that's not very practical, we run our own assignment analysis.

            SemanticModel model         = context.Compilation.GetSemanticModel(bodySyntax.SyntaxTree);
            IOperation    bodyOperation = model.GetOperation(bodySyntax);

            if (bodyOperation == null || bodyOperation.HasErrors(context.Compilation, context.CancellationToken))
            {
                return;
            }

            CollectAssignedStructParameters(context.Target, bodyOperation, collector);
        }
Exemplo n.º 8
0
        private static void AnalyzeOrdinaryParameters(BaseAnalysisContext <ICollection <IParameterSymbol> > context,
                                                      [NotNull] SyntaxNode bodySyntax, [NotNull] DiagnosticCollector collector)
        {
            DataFlowAnalysis dataFlowAnalysis = TryAnalyzeDataFlow(bodySyntax, context.Compilation);

            if (dataFlowAnalysis == null)
            {
                return;
            }

            foreach (IParameterSymbol parameter in context.Target)
            {
                if (dataFlowAnalysis.WrittenInside.Contains(parameter))
                {
                    collector.Add(Diagnostic.Create(Rule, parameter.Locations[0], parameter.Name));
                }
            }
        }
Exemplo n.º 9
0
        private static void AnalyzeParameters(BaseAnalysisContext <ImmutableArray <IParameterSymbol> > context,
                                              [NotNull] ISymbol member, [NotNull] string memberName)
        {
            ImmutableArray <IParameterSymbol> parameters = context.Target;

            if (parameters.Length > MaxParameterCount)
            {
                ReportParameterCount(context.WithTarget(member), memberName, parameters.Length);
            }

            foreach (IParameterSymbol parameter in parameters)
            {
                if (parameter.Type.IsTupleType || TryGetSystemTupleElementCount(parameter.Type) != null)
                {
                    ReportTupleParameter(context.WithTarget(parameter), memberName, parameter.Name);
                }
            }
        }
Exemplo n.º 10
0
        private static void AnalyzeParametersInMethod(BaseAnalysisContext <ImmutableArray <IParameterSymbol> > context,
                                                      [NotNull] SyntaxNode bodySyntax, [NotNull] DiagnosticCollector collector)
        {
            IGrouping <bool, IParameterSymbol>[] parameterGrouping = context.Target
                                                                     .Where(parameter => parameter.RefKind == RefKind.None && !parameter.IsSynthesized()).GroupBy(IsUserDefinedStruct)
                                                                     .ToArray();

            ICollection <IParameterSymbol> ordinaryParameters =
                parameterGrouping.Where(group => !group.Key).SelectMany(group => group).ToArray();

            if (ordinaryParameters.Any())
            {
                AnalyzeOrdinaryParameters(context.WithTarget(ordinaryParameters), bodySyntax, collector);
            }

            ICollection <IParameterSymbol> structParameters =
                parameterGrouping.Where(group => group.Key).SelectMany(group => group).ToArray();

            if (structParameters.Any())
            {
                AnalyzeStructParameters(context.WithTarget(structParameters), bodySyntax, collector);
            }
        }
 public ParameterCountInfo(BaseAnalysisContext <TTarget> context, int maxParameterCount)
 {
     Context           = context;
     MaxParameterCount = maxParameterCount;
 }
Exemplo n.º 12
0
 public ParameterCountInfo <T> ChangeContext <T>(BaseAnalysisContext <T> context)
 {
     return(new ParameterCountInfo <T>(context, settings, isConstructor));
 }