private static void ChangeType(
            CodeRefactoringContext context,
            SemanticModel semanticModel,
            VariableDeclarationSyntax variableDeclaration)
        {
            TypeAnalysisResult result = VariableDeclarationAnalysis.AnalyzeType(
                variableDeclaration,
                semanticModel,
                context.CancellationToken);

            switch (result)
            {
            case TypeAnalysisResult.Explicit:
            case TypeAnalysisResult.ExplicitButShouldBeImplicit:
            {
                context.RegisterRefactoring(
                    "Change type to 'var'",
                    cancellationToken => TypeSyntaxRefactoring.ChangeTypeToImplicitAsync(context.Document, variableDeclaration.Type, cancellationToken));

                break;
            }

            case TypeAnalysisResult.Implicit:
            case TypeAnalysisResult.ImplicitButShouldBeExplicit:
            {
                ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(variableDeclaration.Type, context.CancellationToken).Type;

                context.RegisterRefactoring(
                    $"Change type to '{typeSymbol.ToDisplayString(TypeSyntaxRefactoring.SymbolDisplayFormat)}'",
                    cancellationToken => TypeSyntaxRefactoring.ChangeTypeToExplicitAsync(context.Document, variableDeclaration.Type, typeSymbol, cancellationToken));

                break;
            }
            }
        }
Exemple #2
0
        private void AnalyzeVariableDeclaration(SyntaxNodeAnalysisContext context)
        {
            if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true)
            {
                return;
            }

            var variableDeclaration = (VariableDeclarationSyntax)context.Node;

            if (variableDeclaration.Variables.Count != 1)
            {
                return;
            }

            TypeAnalysisResult result = VariableDeclarationAnalysis.AnalyzeType(
                variableDeclaration,
                context.SemanticModel,
                context.CancellationToken);

            switch (result)
            {
            case TypeAnalysisResult.Explicit:
            {
                break;
            }

            case TypeAnalysisResult.ExplicitButShouldBeImplicit:
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.DeclareImplicitType,
                    variableDeclaration.Type.GetLocation());

                break;
            }

            case TypeAnalysisResult.Implicit:
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.DeclareExplicitTypeEvenIfObvious,
                    variableDeclaration.Type.GetLocation());

                break;
            }

            case TypeAnalysisResult.ImplicitButShouldBeExplicit:
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.DeclareExplicitType,
                    variableDeclaration.Type.GetLocation());

                break;
            }
            }
        }
        private static async Task ChangeTypeAsync(
            RefactoringContext context,
            VariableDeclarationSyntax variableDeclaration)
        {
            SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

            TypeAnalysisResult result = VariableDeclarationAnalysis.AnalyzeType(
                variableDeclaration,
                semanticModel,
                context.CancellationToken);

            if (result == TypeAnalysisResult.Explicit || result == TypeAnalysisResult.ExplicitButShouldBeImplicit)
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeExplicitTypeToVar))
                {
                    context.RegisterRefactoring(
                        "Change type to 'var'",
                        cancellationToken =>
                    {
                        return(ChangeTypeRefactoring.ChangeTypeToVarAsync(
                                   context.Document,
                                   variableDeclaration.Type,
                                   cancellationToken));
                    });
                }
            }
            else if (result == TypeAnalysisResult.Implicit || result == TypeAnalysisResult.ImplicitButShouldBeExplicit)
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeVarToExplicitType))
                {
                    ITypeSymbol typeSymbol = semanticModel
                                             .GetTypeInfo(variableDeclaration.Type, context.CancellationToken)
                                             .Type;

                    ChangeType(context, variableDeclaration, typeSymbol, semanticModel, context.CancellationToken);
                }
            }
        }