public override SyntaxNode?VisitFieldDeclaration(FieldDeclarationSyntax node)
        {
            if (_uninitializedVariables.Contains(node))
            {
                return(node.WithDeclaration(
                           node.Declaration.WithType(NullUtilities.ToNullable(node.Declaration.Type))));
            }

            return(node);
        }
Beispiel #2
0
        public override SyntaxNode?VisitPropertyDeclaration(PropertyDeclarationSyntax node)
        {
            if (HasCanBeNullAttribute(node) ||
                IsInClassOrStruct(node) &&
                (GetterReturnsNull(node) ||
                 IsUninitialized(node)))
            {
                return(node.WithType(NullUtilities.ToNullable(node.Type)));
            }

            return(node);
        }
Beispiel #3
0
        public override SyntaxNode?VisitCastExpression(CastExpressionSyntax node)
        {
            var type = node.Type;

            if (type is NullableTypeSyntax)
            {
                return(node);
            }

            return(NullUtilities.CanBeNull(node.Expression, _semanticModel)
          ? node.WithType(NullUtilities.ToNullable(type))
          : node);
        }
        public override SyntaxNode?VisitMethodDeclaration(MethodDeclarationSyntax node)
        {
            if (_nullableInterfaces.TryGetValue(node, out var parameterNames))
            {
                if (parameterNames.Contains("#return"))
                {
                    node = node.WithReturnType(NullUtilities.ToNullable(node.ReturnType));
                }
                var newParameterList = parameterNames.Aggregate(node.ParameterList, ToNullableParameter);
                return(node.WithParameterList(newParameterList));
            }

            return(base.VisitMethodDeclaration(node));
        }
        public override SyntaxNode?VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
        {
            if (node.Declaration.Type.IsVar)
            {
                return(node);
            }

            if (node.Declaration.Type is NullableTypeSyntax)
            {
                return(node);
            }

            var type = node.Declaration.Type;

            var isNullable = node.Declaration.Variables
                             .Where(variable => variable.Initializer != null)
                             .Any(variable => NullUtilities.CanBeNull(variable.Initializer !.Value, _semanticModel));

            return(isNullable
          ? node.WithDeclaration(node.Declaration.WithType(NullUtilities.ToNullable(type)))
          : node);
        }
Beispiel #6
0
        public override SyntaxNode?VisitMethodDeclaration(MethodDeclarationSyntax node)
        {
            var newParameters = node.ParameterList.Parameters;

            foreach (var parameter in node.ParameterList.Parameters)
            {
                if (IsNullableParameter(parameter))
                {
                    if (parameter.Type == null)
                    {
                        continue;
                    }

                    var toReplace = newParameters.SingleOrDefault(param => param.Identifier.ToString() == parameter.Identifier.ToString());
                    if (toReplace.Type != null)
                    {
                        newParameters = newParameters.Replace(toReplace, toReplace.WithType(NullUtilities.ToNullable(toReplace.Type)));
                    }
                }
            }

            return(node.WithParameterList(node.ParameterList.WithParameters(newParameters)));
        }