Skip to content

[return: NotNullIfNotNull(paramName)] #6

Open

Description

@dgrunwald

[return: NotNullIfNotNull(paramName)] is a semi-common attribute to use, especially in some code bases that like to use:
if (input == null) return null;
at the start of many functions.

Unlike [NotNullWhen(bool)] for out parameters, I don't see a clean way to infer NotNullIfNotNull with our current algorithm.
But it would be valuable to figure something out, so I'm creating this issue to collect some cases of [NotNullIfNotNull] methods and their constraint graphs.

Activity

  1. dgrunwald commented on Jun 13, 2020

    @dgrunwald
    MemberAuthor
        /// <summary>
        /// Atomically performs the following operation:
        /// - If target is null: stores newValue in target and returns newValue.
        /// - If target is not null: returns target.
        /// </summary>
        [return: NotNullIfNotNull("newValue")]
        public static T? GetOrSet<T>(ref T? target, T? newValue) where T : class
        {
            T? oldValue = Interlocked.CompareExchange(ref target, newValue, null);
            return oldValue ?? newValue;
        }

    GetOrSet
    This one is trivial: the nullability of the return value directly depends on the parameter and on no other nodes.

    We could detect this special case after building the graph, annotate the method with the attribute, then rebuild the graph from scratch, since callers of the method will profit from the attribute (via 07b2d78), and perform inference as usual in a second pass.

  2. dgrunwald commented on Jun 14, 2020

    @dgrunwald
    MemberAuthor

    A different case is something like this:

        [return: NotNullIfNotNull("filename")]
        public static Stream? OpenFile(string? filename)
        {
            if (filename == null)
                return null;
            return new FileStream(filename, FileMode.Open);
        }

    OpenFile
    Here the graph shows no relation between the parameter and the return node.
    We could maybe detect that all incoming edges for the return node were created by code under a if (param == null) check (i.e. control-flow guaranteeing that the parameter being definitely null, not just potentially null as tracked by the flow-state implemented in #5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions