Exemple #1
0
    public override ExpressionStatementSyntax GenerateNullCheckStatement(
        Document document,
        NullableParameter nullableParameter)
    {
        string parameterName   = nullableParameter.Syntax.Identifier.Text;
        var    nullCheckMethod = "NotNull";

        if (nullableParameter.Symbol.Type.Kind == SymbolKind.PointerType)
        {
            nullCheckMethod = "NotNullPtr";
        }
        var generator          = SyntaxGenerator.GetGenerator(document);
        var nullCheckStatement = generator.ExpressionStatement(
            generator.InvocationExpression(
                generator.MemberAccessExpression(
                    generator.IdentifierName("Requires"),
                    generator.IdentifierName(nullCheckMethod)),
                new[]
        {
            generator.IdentifierName(parameterName)
        }))
                                 .WithTrailingTrivia(SyntaxFactory.EndOfLine(Environment.NewLine));

        return((ExpressionStatementSyntax)nullCheckStatement);
    }
Exemple #2
0
    public override ExpressionStatementSyntax GenerateNullCheckStatement(
        Document document,
        NullableParameter nullableParameter)
    {
        string     parameterName    = nullableParameter.Syntax.Identifier.Text;
        var        generator        = SyntaxGenerator.GetGenerator(document);
        SyntaxNode assertExpression = nullableParameter.Symbol.Type.SpecialType switch
        {
            SpecialType.System_String => generator.LogicalNotExpression(
                generator.InvocationExpression(
                    generator.MemberAccessExpression(
                        generator.TypeExpression(SpecialType.System_String),
                        generator.IdentifierName(nameof(string.IsNullOrWhiteSpace))),
                    new[] { IdentifierName(parameterName) })),
            _ => IsPatternExpression(
                IdentifierName(parameterName),
                UnaryPattern(
                    ConstantPattern(
                        LiteralExpression(SyntaxKind.NullLiteralExpression))))
        };
        var nullCheckStatement = generator.ExpressionStatement(
            generator.InvocationExpression(
                generator.MemberAccessExpression(
                    generator.IdentifierName(nameof(Debug)),
                    generator.IdentifierName(nameof(Debug.Assert))),
                new[] { assertExpression }))
                                 .WithTrailingTrivia(EndOfLine(Environment.NewLine));

        return((ExpressionStatementSyntax)nullCheckStatement);
    }
}
Exemple #3
0
    private Document FixDiagnostic(
        Document document,
        NullableParameter nullableParameter,
        SyntaxNode root,
        SemanticModel model,
        SyntaxNode node,
        CancellationToken token)
    {
        var addNullChecksRewriter = GetRewriter(
            document,
            model,
            ImmutableArray.Create(nullableParameter),
            token);
        var newNode     = addNullChecksRewriter.Visit(node);
        var newDocument = document.WithSyntaxRoot(root.ReplaceNode(node, newNode));

        return(newDocument);
    }
    public override ExpressionStatementSyntax GenerateNullCheckStatement(
        Document document,
        NullableParameter nullableParameter)
    {
        string parameterName      = nullableParameter.Syntax.Identifier.Text;
        var    generator          = SyntaxGenerator.GetGenerator(document);
        var    nullCheckStatement = generator.ExpressionStatement(
            generator.InvocationExpression(
                generator.MemberAccessExpression(
                    generator.IdentifierName(nameof(Debug)),
                    generator.IdentifierName(nameof(Debug.Assert))),
                new[]
        {
            IsPatternExpression(
                IdentifierName(parameterName),
                UnaryPattern(
                    ConstantPattern(
                        LiteralExpression(SyntaxKind.NullLiteralExpression))))
        }))
                                    .WithTrailingTrivia(EndOfLine(Environment.NewLine));

        return((ExpressionStatementSyntax)nullCheckStatement);
    }
Exemple #5
0
 public override bool ShouldRegisterCodeFix(NullableParameter parameter)
 {
     return(parameter.Symbol.Type.Kind == SymbolKind.ArrayType ||
            parameter.Symbol.Type.SpecialType == SpecialType.System_String);
 }
Exemple #6
0
 public virtual bool ShouldRegisterCodeFix(NullableParameter parameter) => true;
 public abstract ExpressionStatementSyntax GenerateNullCheckStatement(
     Document document,
     NullableParameter nullableParameter);