Exemple #1
0
        private static SyntaxKind GetReplacementKind(
            ReturnStatementSyntax returnStatement,
            ISymbol containingSymbol,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            SyntaxToken      returnKeyword = returnStatement.ReturnKeyword;
            ExpressionSyntax expression    = returnStatement.Expression;

            if (semanticModel.ContainsCompilerDiagnostic(CSharpErrorCodes.CannotImplicitlyConvertType, expression.Span, cancellationToken))
            {
                return(SyntaxKind.YieldReturnStatement);
            }
            else if (semanticModel.ContainsCompilerDiagnostic(CSharpErrorCodes.CannotReturnValueFromIterator, returnKeyword.Span, cancellationToken))
            {
                ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(expression, cancellationToken);

                containingSymbol = containingSymbol ?? semanticModel.GetEnclosingSymbol(returnStatement.SpanStart, cancellationToken);

                if (containingSymbol?.IsKind(SymbolKind.Method) == true)
                {
                    var methodSymbol = (IMethodSymbol)containingSymbol;

                    ITypeSymbol returnType = methodSymbol.ReturnType;

                    if (returnType.SpecialType == SpecialType.System_Collections_IEnumerable)
                    {
                        if (typeSymbol.IsIEnumerableOrConstructedFromIEnumerableOfT())
                        {
                            return(SyntaxKind.ForEachStatement);
                        }
                        else
                        {
                            return(SyntaxKind.YieldReturnStatement);
                        }
                    }
                    else if (returnType.IsNamedType())
                    {
                        var namedTypeSymbol = (INamedTypeSymbol)returnType;

                        if (namedTypeSymbol.ConstructedFrom.SpecialType == SpecialType.System_Collections_Generic_IEnumerable_T)
                        {
                            if (semanticModel
                                .ClassifyConversion(expression, namedTypeSymbol.TypeArguments[0])
                                .IsImplicit)
                            {
                                return(SyntaxKind.YieldReturnStatement);
                            }
                            else
                            {
                                return(SyntaxKind.ForEachStatement);
                            }
                        }
                    }
                }
            }

            return(SyntaxKind.None);
        }
Exemple #2
0
 private static bool ContainsDiagnostic(SyntaxToken identifier, SemanticModel semanticModel, CancellationToken cancellationToken)
 {
     return(semanticModel.ContainsCompilerDiagnostic(
                CSharpErrorCodes.CannotChangeAccessModifiersWhenOverridingInheritedMember,
                identifier.Span,
                cancellationToken));
 }
Exemple #3
0
        public static async Task ComputeRefactoringAsync(RefactoringContext context, UsingStatementSyntax usingStatement)
        {
            VariableDeclarationSyntax declaration = usingStatement.Declaration;

            if (declaration != null &&
                usingStatement.IsParentKind(SyntaxKind.Block))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                if (semanticModel.ContainsCompilerDiagnostic(
                        CSharpErrorCodes.TypeUsedInUsingStatementMustBeImplicitlyConvertibleToIDisposable,
                        declaration.Span,
                        context.CancellationToken))
                {
                    if (context.Span.IsContainedInSpanOrBetweenSpans(declaration))
                    {
                        RegisterRefactoring(context, usingStatement);
                    }
                }
                else if (context.Span.IsBetweenSpans(declaration))
                {
                    RegisterRefactoring(context, usingStatement);
                }
            }
        }
Exemple #4
0
        public static async Task ComputeRefactoringAsync(RefactoringContext context, LiteralExpressionSyntax literalExpression)
        {
            if (literalExpression.IsKind(SyntaxKind.StringLiteralExpression) &&
                literalExpression.Token.ValueText.Length == 1)
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                if (semanticModel.ContainsCompilerDiagnostic(CSharpErrorCodes.CannotImplicitlyConvertType, literalExpression.Span, context.CancellationToken))
                {
                    context.RegisterRefactoring(
                        "Replace string literal with character literal",
                        cancellationToken => RefactorAsync(context.Document, literalExpression, cancellationToken));
                }
            }
        }
        public static bool CanRefactor(MethodDeclarationSyntax methodDeclaration, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            if (methodDeclaration.Body != null)
            {
                TypeSyntax returnType = methodDeclaration.ReturnType;

                if (returnType?.IsMissing == false)
                {
                    ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(returnType, cancellationToken);

                    return(typeSymbol?.IsErrorType() == false &&
                           !typeSymbol.IsIEnumerableOrConstructedFromIEnumerableOfT() &&
                           !typeSymbol.IsVoid() &&
                           semanticModel.ContainsCompilerDiagnostic(CSharpErrorCodes.NotAllCodePathsReturnValue, methodDeclaration.Identifier.Span, cancellationToken));
                }
            }

            return(false);
        }
Exemple #6
0
 private static bool CanRefactor(TextSpan span, SemanticModel semanticModel, CancellationToken cancellationToken)
 {
     return(semanticModel.ContainsCompilerDiagnostic(CSharpErrorCodes.MissingXmlComment, span, cancellationToken));
 }