Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return nullable #279

Open
JohanLarsson opened this issue Nov 9, 2020 · 0 comments
Open

Return nullable #279

JohanLarsson opened this issue Nov 9, 2020 · 0 comments

Comments

@JohanLarsson
Copy link
Member

Before:

        internal static bool FindSingle(IFieldSymbol field, SemanticModel semanticModel, CancellationToken cancellationToken, [NotNullWhen(true)] out ExpressionSyntax? expression)
        {
            expression = null;
            if (field.IsReadOnly &&
                field.TrySingleDeclaration(cancellationToken, out VariableDeclarationSyntax? declaration) &&
                declaration.Variables.TrySingle(out var variable))
            {
                using var walker = MutationWalker.For(field, semanticModel, cancellationToken);
                if (walker.IsEmpty)
                {
                    expression = variable.Initializer?.Value;
                    return expression != null;
                }

                if (variable.Initializer is null &&
                    walker.TrySingle(out var node) &&
                    node.Parent is AssignmentExpressionSyntax assignment)
                {
                    expression = assignment.Right;
                    return expression != null;
                }

                return false;
            }

            return false;
        }

After

        internal static ExpressionSyntax? FindSingle(IFieldSymbol field, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            if (field.IsReadOnly &&
                field.TrySingleDeclaration(cancellationToken, out VariableDeclarationSyntax? declaration) &&
                declaration.Variables.TrySingle(out var variable))
            {
                using var walker = MutationWalker.For(field, semanticModel, cancellationToken);
                if (walker.IsEmpty)
                {
                    return variable.Initializer?.Value;
                }

                if (variable.Initializer is null &&
                    walker.TrySingle(out var node) &&
                    node.Parent is AssignmentExpressionSyntax assignment)
                {
                    return assignment.Right;
                }

                return null;
            }

            return null;
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant