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); } }
public ParameterCountInfo(BaseAnalysisContext <TTarget> context, [NotNull] ParameterSettings settings, bool isConstructor = false) { Guard.NotNull(settings, nameof(settings)); Context = context; this.settings = settings; this.isConstructor = isConstructor; }
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); } }
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); } }
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); } }
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); }
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); }
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)); } } }
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); } } }
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; }
public ParameterCountInfo <T> ChangeContext <T>(BaseAnalysisContext <T> context) { return(new ParameterCountInfo <T>(context, settings, isConstructor)); }