private static void ComputeCodeFix(
            CodeFixContext context,
            Diagnostic diagnostic,
            ExpressionSyntax expression,
            VariableDeclaratorSyntax variableDeclarator,
            SemanticModel semanticModel)
        {
            if (!(variableDeclarator.Parent is VariableDeclarationSyntax variableDeclaration))
            {
                return;
            }

            TypeSyntax type = variableDeclaration.Type;

            if (type == null)
            {
                return;
            }

            ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(expression, context.CancellationToken);

            if ((variableDeclaration.Parent is FieldDeclarationSyntax fieldDeclaration) &&
                fieldDeclaration.Modifiers.Contains(SyntaxKind.ConstKeyword) &&
                typeSymbol?.SupportsConstantValue() != true)
            {
                return;
            }

            if (typeSymbol?.SupportsExplicitDeclaration() == true)
            {
                CodeFixRegistrator.ChangeType(context, diagnostic, type, typeSymbol, semanticModel, CodeFixIdentifiers.ChangeTypeAccordingToInitializer);

                if (typeSymbol.OriginalDefinition.EqualsOrInheritsFromTaskOfT(semanticModel))
                {
                    ISymbol enclosingSymbol = semanticModel.GetEnclosingSymbol(variableDeclaration.SpanStart, context.CancellationToken);

                    if (enclosingSymbol.IsAsyncMethod())
                    {
                        ITypeSymbol typeArgument = ((INamedTypeSymbol)typeSymbol).TypeArguments[0];

                        ChangeTypeAndAddAwait(context, diagnostic, expression, variableDeclaration, type, typeArgument, semanticModel);
                    }
                }
            }

            if (variableDeclaration.IsParentKind(SyntaxKind.LocalDeclarationStatement, SyntaxKind.UsingStatement))
            {
                CodeFixRegistrator.ChangeTypeToVar(context, diagnostic, type, CodeFixIdentifiers.ChangeTypeToVar);
            }
        }
Exemple #2
0
        private static void ComputeCodeFix(
            CodeFixContext context,
            Diagnostic diagnostic,
            ExpressionSyntax expression,
            VariableDeclaratorSyntax variableDeclarator,
            SemanticModel semanticModel)
        {
            if (!(variableDeclarator.Parent is VariableDeclarationSyntax variableDeclaration))
            {
                return;
            }

            TypeSyntax type = variableDeclaration.Type;

            if (type == null)
            {
                return;
            }

            ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(expression, context.CancellationToken);

            if ((variableDeclaration.Parent is FieldDeclarationSyntax fieldDeclaration) &&
                fieldDeclaration.Modifiers.Contains(SyntaxKind.ConstKeyword) &&
                typeSymbol?.SupportsConstantValue() != true)
            {
                return;
            }

            if (typeSymbol?.SupportsExplicitDeclaration() == true)
            {
                CodeFixRegistrator.ChangeType(context, diagnostic, type, typeSymbol, semanticModel, CodeFixIdentifiers.ChangeTypeAccordingToInitializer);

                ComputeChangeTypeAndAddAwait(context, diagnostic, variableDeclaration, type, expression, typeSymbol, semanticModel);
            }

            if (variableDeclaration.IsParentKind(SyntaxKind.LocalDeclarationStatement, SyntaxKind.UsingStatement))
            {
                CodeFixRegistrator.ChangeTypeToVar(context, diagnostic, type, CodeFixIdentifiers.ChangeTypeToVar);
            }
        }
        public static void ComputeCodeFix(
            CodeFixContext context,
            Diagnostic diagnostic,
            ExpressionSyntax expression,
            SemanticModel semanticModel)
        {
            if (!(expression.Parent is EqualsValueClauseSyntax equalsValueClause))
            {
                return;
            }

            if (equalsValueClause.Parent is VariableDeclaratorSyntax variableDeclarator)
            {
                if (!(variableDeclarator.Parent is VariableDeclarationSyntax variableDeclaration))
                {
                    return;
                }

                TypeSyntax type = variableDeclaration.Type;

                if (type != null)
                {
                    ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(expression, context.CancellationToken);

                    if ((variableDeclaration.Parent is FieldDeclarationSyntax fieldDeclaration) &&
                        fieldDeclaration.Modifiers.Contains(SyntaxKind.ConstKeyword) &&
                        typeSymbol?.SupportsConstantValue() != true)
                    {
                        return;
                    }

                    if (typeSymbol?.SupportsExplicitDeclaration() == true)
                    {
                        CodeFixRegistrator.ChangeType(context, diagnostic, type, typeSymbol, semanticModel, CodeFixIdentifiers.ChangeTypeAccordingToInitializer);

                        if (typeSymbol.IsConstructedFromTaskOfT(semanticModel))
                        {
                            ISymbol enclosingSymbol = semanticModel.GetEnclosingSymbol(variableDeclaration.SpanStart, context.CancellationToken);

                            if (enclosingSymbol.IsAsyncMethod())
                            {
                                ITypeSymbol typeArgument = ((INamedTypeSymbol)typeSymbol).TypeArguments[0];

                                ChangeTypeAndAddAwait(context, diagnostic, expression, variableDeclaration, type, typeArgument, semanticModel);
                            }
                        }
                    }

                    if (variableDeclaration.IsParentKind(SyntaxKind.LocalDeclarationStatement, SyntaxKind.UsingStatement))
                    {
                        CodeFixRegistrator.ChangeTypeToVar(context, diagnostic, type, CodeFixIdentifiers.ChangeTypeToVar);
                    }
                }
            }
            else if (equalsValueClause.Parent is PropertyDeclarationSyntax propertyDeclaration)
            {
                TypeSyntax type = propertyDeclaration.Type;

                if (type != null)
                {
                    ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(expression, context.CancellationToken);

                    if (typeSymbol?.SupportsExplicitDeclaration() == true)
                    {
                        ISymbol symbol = semanticModel.GetDeclaredSymbol(propertyDeclaration, context.CancellationToken);

                        if (symbol?.IsOverride == false &&
                            !symbol.ImplementsInterfaceMember())
                        {
                            CodeFixRegistrator.ChangeType(context, diagnostic, type, typeSymbol, semanticModel, CodeFixIdentifiers.ChangeTypeAccordingToInitializer);
                        }
                    }
                }
            }
            else
            {
                Debug.Fail(equalsValueClause.Parent.Kind().ToString());
            }
        }