private static SyntaxNode GetSemanticBoundary(DefaultExpressionSyntax node)
 {
     // Notes:
     // 1. Syntax which doesn't fall into one of the "safe buckets" will get placed into a single group keyed off
     //    the root of the tree. If more than one such node exists in the document, all will be verified.
     // 2. Cannot include ArgumentSyntax because it could affect generic argument inference.
     return(node.FirstAncestorOrSelf <SyntaxNode>(n =>
                                                  n is StatementSyntax ||
                                                  n is ParameterSyntax ||
                                                  n is VariableDeclaratorSyntax ||
                                                  n.Parent == null));
 }
Ejemplo n.º 2
0
        private static ITypeSymbol GetEnclosingDeclarationReturnType(DefaultExpressionSyntax expression, SemanticModel semanticModel)
        {
            var method = expression.FirstAncestorOrSelf <MethodDeclarationSyntax>();

            if (method != null)
            {
                return(semanticModel.GetDeclaredSymbol(method).ReturnType);
            }

            var @operator = expression.FirstAncestorOrSelf <OperatorDeclarationSyntax>();

            if (@operator != null)
            {
                return(semanticModel.GetDeclaredSymbol(@operator).ReturnType);
            }

            var conversion = expression.FirstAncestorOrSelf <ConversionOperatorDeclarationSyntax>();

            if (conversion != null)
            {
                return(semanticModel.GetDeclaredSymbol(conversion).ReturnType);
            }

            var property = expression.FirstAncestorOrSelf <BasePropertyDeclarationSyntax>(); // Properties and indexers.

            if (property != null)
            {
                return(((IPropertySymbol)semanticModel.GetDeclaredSymbol(property)).Type);
            }

            // TODO: Support finding the type for other enclosing declarations in which the return statement can appear.
            //       So far we will check just the most important ones. It is anyway very unlikely to have the default(T) in most of them.
            //       Right now we will simply return null and the check for the type equality in the query will return false and thus
            //       at the moment simply ignore those cases.
            return(null);
        }