Esempio n. 1
0
        private static ExpressionSyntax SimplifyCast(CastExpressionSyntax node, SemanticModel semanticModel, OptionSet optionSet, CancellationToken cancellationToken)
        {
            if (!node.IsUnnecessaryCast(semanticModel, cancellationToken))
            {
                return node;
            }

            var leadingTrivia = node.OpenParenToken.LeadingTrivia
                .Concat(node.OpenParenToken.TrailingTrivia)
                .Concat(node.Type.GetLeadingTrivia())
                .Concat(node.Type.GetTrailingTrivia())
                .Concat(node.CloseParenToken.LeadingTrivia)
                .Concat(node.CloseParenToken.TrailingTrivia)
                .Concat(node.Expression.GetLeadingTrivia())
                .Where(t => !t.IsElastic());

            var trailingTrivia = node.GetTrailingTrivia().Where(t => !t.IsElastic());

            var resultNode = node.Expression
                .WithLeadingTrivia(leadingTrivia)
                .WithTrailingTrivia(trailingTrivia);

            resultNode = SimplificationHelpers.CopyAnnotations(from: node, to: resultNode);

            return resultNode;
        }
Esempio n. 2
0
        private static ExpressionSyntax SimplifyCast(CastExpressionSyntax node, SemanticModel semanticModel, OptionSet optionSet, CancellationToken cancellationToken)
        {
            if (!node.IsUnnecessaryCast(semanticModel, cancellationToken))
            {
                return(node);
            }

            var leadingTrivia = node.OpenParenToken.LeadingTrivia
                                .Concat(node.OpenParenToken.TrailingTrivia)
                                .Concat(node.Type.GetLeadingTrivia())
                                .Concat(node.Type.GetTrailingTrivia())
                                .Concat(node.CloseParenToken.LeadingTrivia)
                                .Concat(node.CloseParenToken.TrailingTrivia)
                                .Concat(node.Expression.GetLeadingTrivia())
                                .Where(t => !t.IsElastic());

            var trailingTrivia = node.GetTrailingTrivia().Where(t => !t.IsElastic());

            var resultNode = node.Expression
                             .WithLeadingTrivia(leadingTrivia)
                             .WithTrailingTrivia(trailingTrivia);

            resultNode = SimplificationHelpers.CopyAnnotations(from: node, to: resultNode);

            return(resultNode);
        }
Esempio n. 3
0
        private static ExpressionSyntax SimplifyCast(CastExpressionSyntax node, SemanticModel semanticModel, OptionSet optionSet, CancellationToken cancellationToken)
        {
            if (!node.IsUnnecessaryCast(semanticModel, cancellationToken))
            {
                return(node);
            }

            return(node.Uncast());
        }
        private bool TryRemoveCastExpression(
            SemanticModel model, CastExpressionSyntax node, out Diagnostic diagnostic, CancellationToken cancellationToken)
        {
            diagnostic = default(Diagnostic);

            if (!node.IsUnnecessaryCast(model, cancellationToken))
            {
                return(false);
            }

            var tree = model.SyntaxTree;
            var span = TextSpan.FromBounds(node.OpenParenToken.SpanStart, node.CloseParenToken.Span.End);

            if (tree.OverlapsHiddenPosition(span, cancellationToken))
            {
                return(false);
            }

            diagnostic = Diagnostic.Create(DiagnosticMD, tree.GetLocation(span));
            return(true);
        }