本文整理汇总了C#中OperationAnalysisContext类的典型用法代码示例。如果您正苦于以下问题:C# OperationAnalysisContext类的具体用法?C# OperationAnalysisContext怎么用?C# OperationAnalysisContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OperationAnalysisContext类属于命名空间,在下文中一共展示了OperationAnalysisContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AnalyzeOperation
private void AnalyzeOperation(OperationAnalysisContext context)
{
IArrayCreationExpression arrayCreationExpression = context.Operation as IArrayCreationExpression;
// We can't replace array allocations in attributes, as they're persisted to metadata
// TODO: Once we have operation walkers, we can replace this syntactic check with an operation-based check.
if (arrayCreationExpression.Syntax.Ancestors().Any(IsAttributeSyntax))
{
return;
}
if (arrayCreationExpression.DimensionSizes.Length == 1)
{
IOperation dimensionSize = arrayCreationExpression.DimensionSizes[0];
if (dimensionSize.HasConstantValue(0))
{
// Workaround for https://github.com/dotnet/roslyn/issues/10214
// Bail out for compiler generated param array creation.
if (IsCompilerGeneratedParamsArray(arrayCreationExpression, context))
{
return;
}
// pointers can't be used as generic arguments
if (arrayCreationExpression.ElementType.TypeKind != TypeKind.Pointer)
{
context.ReportDiagnostic(context.Operation.Syntax.CreateDiagnostic(UseArrayEmptyDescriptor));
}
}
}
}
示例2: AnalyzeOperation
private void AnalyzeOperation(OperationAnalysisContext context)
{
IArrayCreationExpression arrayCreationExpression = context.Operation as IArrayCreationExpression;
// We can't replace array allocations in attributes, as they're persisted to metadata
// TODO: Once we have operation walkers, we can replace this syntactic check with an operation-based check.
if (arrayCreationExpression.Syntax.Ancestors().Any(IsAttributeSyntax))
{
return;
}
if (arrayCreationExpression.DimensionSizes.Length == 1)
{
var dimensionSize = arrayCreationExpression.DimensionSizes[0];
if (dimensionSize.ConstantValue.HasValue && (int)dimensionSize.ConstantValue.Value == 0)
{
// pointers can't be used as generic arguments
if (arrayCreationExpression.ElementType.TypeKind != TypeKind.Pointer)
{
context.ReportDiagnostic(context.Operation.Syntax.CreateDiagnostic(UseArrayEmptyDescriptor));
}
}
}
}
示例3: AnalyzeOperation
private static void AnalyzeOperation(OperationAnalysisContext context, ImmutableArray<INamedTypeSymbol> taskTypes)
{
IAwaitExpression awaitExpression = context.Operation as IAwaitExpression;
// Get the type of the expression being awaited and check it's a task type.
ITypeSymbol typeOfAwaitedExpression = awaitExpression?.AwaitedValue?.Type;
if (typeOfAwaitedExpression != null && taskTypes.Contains(typeOfAwaitedExpression.OriginalDefinition))
{
context.ReportDiagnostic(awaitExpression.AwaitedValue.Syntax.CreateDiagnostic(Rule));
}
}
示例4: AnalyzeOperation
private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol containingType)
{
var operation = context.Operation as IInvocationExpression;
var method = operation.TargetMethod;
if (method != null &&
(method.IsAbstract || method.IsVirtual) &&
method.ContainingType == containingType)
{
context.ReportDiagnostic(operation.Syntax.CreateDiagnostic(Rule));
}
}
示例5: AnalyzeOperation
private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol stringComparisonType)
{
OperationKind kind = context.Operation.Kind;
if (kind == OperationKind.InvocationExpression)
{
AnalyzeInvocationExpression((IInvocationExpression)context.Operation, stringComparisonType, context.ReportDiagnostic);
}
else
{
AnalyzeBinaryExpression((IBinaryOperatorExpression)context.Operation, context.ReportDiagnostic);
}
}
示例6: AnalyzeNode
private static void AnalyzeNode(OperationAnalysisContext context)
{
switch (context.Operation.Kind)
{
case OperationKind.InvocationExpression:
AnalyzeInvocationExpression(context);
break;
default:
AnalyzeBinaryExpression(context);
break;
}
}
示例7: AnalyzeInvocationExpression
/// <summary>
/// Check to see if we have an invocation to string.Equals that has an empty string as an argument.
/// </summary>
private static void AnalyzeInvocationExpression(OperationAnalysisContext context)
{
var invocationOperation = (IInvocationExpression)context.Operation;
if (invocationOperation.ArgumentsInSourceOrder.Length > 0)
{
IMethodSymbol methodSymbol = invocationOperation.TargetMethod;
if (methodSymbol != null &&
IsStringEqualsMethod(methodSymbol) &&
HasAnEmptyStringArgument(invocationOperation))
{
context.ReportDiagnostic(invocationOperation.Syntax.CreateDiagnostic(s_rule));
}
}
}
示例8: AnalyzeBinaryExpression
/// <summary>
/// Check to see if we have a equals or not equals expression where an empty string is being
/// compared.
/// </summary>
private static void AnalyzeBinaryExpression(OperationAnalysisContext context)
{
var binaryOperation = (IBinaryOperatorExpression)context.Operation;
if (binaryOperation.BinaryOperationKind != BinaryOperationKind.StringEquals &&
binaryOperation.BinaryOperationKind != BinaryOperationKind.StringNotEquals)
{
return;
}
if (IsEmptyString(binaryOperation.LeftOperand) || IsEmptyString(binaryOperation.RightOperand))
{
context.ReportDiagnostic(binaryOperation.Syntax.CreateDiagnostic(s_rule));
}
}
示例9: AnalyzeOperation
private void AnalyzeOperation(OperationAnalysisContext context)
{
var switchOperation = (ISwitchStatement)context.Operation;
var switchBlock = switchOperation.Syntax;
var tree = switchBlock.SyntaxTree;
if (SwitchIsIncomplete(switchOperation, out var missingCases, out var missingDefaultCase) &&
!tree.OverlapsHiddenPosition(switchBlock.Span, context.CancellationToken))
{
Debug.Assert(missingCases || missingDefaultCase);
var properties = ImmutableDictionary<string, string>.Empty
.Add(PopulateSwitchHelpers.MissingCases, missingCases.ToString())
.Add(PopulateSwitchHelpers.MissingDefaultCase, missingDefaultCase.ToString());
var diagnostic = Diagnostic.Create(
HiddenDescriptor, switchBlock.GetLocation(), properties: properties);
context.ReportDiagnostic(diagnostic);
}
}
示例10: AnalyzeOperation
private void AnalyzeOperation(OperationAnalysisContext context)
{
var syntaxTree = context.Operation.Syntax.SyntaxTree;
var cancellationToken = context.CancellationToken;
var optionSet = context.Options.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();
if (optionSet == null)
{
return;
}
var option = optionSet.GetOption(CodeStyleOptions.PreferExplicitTupleNames, context.Compilation.Language);
var severity = option.Notification.Value;
if (severity == DiagnosticSeverity.Hidden)
{
return;
}
var fieldReferenceOperation = (IFieldReferenceExpression)context.Operation;
var field = fieldReferenceOperation.Field;
if (field.ContainingType.IsTupleType)
{
if (field.CorrespondingTupleField.Equals(field))
{
var namedField = GetNamedField(field.ContainingType, field, cancellationToken);
if (namedField != null)
{
var memberAccessSyntax = fieldReferenceOperation.Syntax;
var nameNode = memberAccessSyntax.ChildNodesAndTokens().Reverse().FirstOrDefault();
if (nameNode != null)
{
var properties = ImmutableDictionary<string, string>.Empty.Add(
nameof(ElementName), namedField.Name);
context.ReportDiagnostic(Diagnostic.Create(
GetDescriptorWithSeverity(severity),
nameNode.GetLocation(),
properties));
}
}
}
}
}
示例11: AnalyzeObjectCreation
private static void AnalyzeObjectCreation(
OperationAnalysisContext context,
ISymbol owningSymbol,
ITypeSymbol argumentExceptionType)
{
var creation = (IObjectCreationExpression)context.Operation;
if (!creation.Type.Inherits(argumentExceptionType))
{
return;
}
if (creation.ArgumentsInParameterOrder.Length == 0)
{
if (HasMessageOrParameterNameConstructor(creation.Type))
{
// Call the {0} constructor that contains a message and/ or paramName parameter
ReportDiagnostic(context, s_localizableMessageNoArguments, creation.Type.Name);
}
}
else
{
foreach (IArgument argument in creation.ArgumentsInParameterOrder)
{
if (argument.Parameter.Type.SpecialType != SpecialType.System_String)
{
continue;
}
string value = argument.Value.ConstantValue.HasValue ? argument.Value.ConstantValue.Value as string : null;
if (value == null)
{
continue;
}
CheckArgument(owningSymbol, creation.Type, argument.Parameter, value, context);
}
}
}
示例12: TestAscendingArgument
private static void TestAscendingArgument(OperationAnalysisContext operationContext, IOperation argument, ref long priorArgumentValue)
{
Optional<object> argumentValue = argument.ConstantValue;
if (argumentValue.HasValue && argument.Type.SpecialType == SpecialType.System_Int32)
{
int integerArgument = (int)argumentValue.Value;
if (integerArgument < priorArgumentValue)
{
Report(operationContext, argument.Syntax, OutOfNumericalOrderArgumentsDescriptor);
}
priorArgumentValue = integerArgument;
}
}
示例13: Report
private static void Report(OperationAnalysisContext context, SyntaxNode syntax, DiagnosticDescriptor descriptor)
{
context.ReportDiagnostic(Diagnostic.Create(descriptor, syntax.GetLocation()));
}
示例14: ReportDiagnostic
private static void ReportDiagnostic(
OperationAnalysisContext oaContext,
IInvocationExpression invocationExpression,
IMethodSymbol targetMethod,
IMethodSymbol correctOverload)
{
oaContext.ReportDiagnostic(
invocationExpression.Syntax.CreateDiagnostic(
Rule,
targetMethod.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat),
oaContext.ContainingSymbol.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat),
correctOverload.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat)));
}
示例15: AnalyzeObjectCreationOperation
private void AnalyzeObjectCreationOperation(OperationAnalysisContext context)
{
AnalyzeObjectCreationInternal(context, null, context.Operation);
}