Esempio n. 1
0
        private static void AnalyzePositionalArgumentsAndParameters(
            SymbolAnalysisContext context,
            AttributeData attribute,
            ImmutableArray <TypedConstant> attributePositionalArguments,
            ImmutableArray <IParameterSymbol> methodParameters)
        {
            for (var i = 0; i < attributePositionalArguments.Length; i++)
            {
                var attributeArgument = attributePositionalArguments[i];
                var(methodParameterType, methodParameterName, methodParameterParamsType) =
                    TestCaseUsageAnalyzer.GetParameterType(methodParameters, i);

                if (methodParameterType.IsTypeParameterAndDeclaredOnMethod())
                {
                    continue;
                }

                ITypeSymbol?argumentType = attributeArgument.Type;

                var argumentTypeMatchesParameterType = attributeArgument.CanAssignTo(
                    methodParameterType,
                    context.Compilation,
                    allowImplicitConversion: true,
                    allowEnumToUnderlyingTypeConversion: true);

                if (methodParameterParamsType == null && argumentTypeMatchesParameterType)
                {
                    continue;
                }

                if (methodParameterParamsType != null)
                {
                    var argumentTypeMatchesElementType = attributeArgument.CanAssignTo(
                        methodParameterParamsType,
                        context.Compilation,
                        allowImplicitConversion: true,
                        allowEnumToUnderlyingTypeConversion: true);

                    if (argumentTypeMatchesElementType ||
                        (argumentTypeMatchesParameterType && (argumentType != null || !methodParameterParamsType.IsValueType)))
                    {
                        continue;
                    }
                }

                var attributeArgumentSyntax = attribute.GetConstructorArgumentSyntax(i, context.CancellationToken);

                if (attributeArgumentSyntax is null)
                {
                    continue;
                }

                context.ReportDiagnostic(Diagnostic.Create(parameterTypeMismatch,
                                                           attributeArgumentSyntax.GetLocation(),
                                                           i,
                                                           argumentType?.ToDisplayString() ?? "<null>",
                                                           methodParameterName,
                                                           methodParameterType));

                context.CancellationToken.ThrowIfCancellationRequested();
            }
        }