Skip to content

Warn on misused nullability attributes #36073

@jcouv

Description

@jcouv
  1. In void M([DisallowNull]string x) the attribute is redundant.
    Same in void M<T>([DisallowNull]T t) where T : class or void M<T>([DisallowNull]T t) where T : struct.

  2. [MaybeNull]int or [MaybeNull]int?.

  3. void M([MaybeNull] string s) (the attribute will affect the state of the argument, but why would anyone want that?)

  4. void M([DoesNotReturnIf(true)] out bool)

  5. [MaybeNull, NotNullWhen(false)] (should work per Tweak interaction of outbound annotations ([MaybeNull, NotNullWhen(false)]) #36410)

  6. MaybeNull and NotNull conflict

  7. MaybeNullWhen(b) and NotNullWhen(b) (same b) conflict

  8. int M([MaybeNullWhen(true) string x) should warn because return type is not bool

  9. [DisallowNull] string? Property { get { ... } } or [MaybeNull] string Property { set { ... } } or [DisallowNull] readonly string? field = ...;

  10. using [DisallowNull] or other attributes outside of a nullable annotations context (Usage of flow analysis attributes should warn outside of annotation context #36588)

  11. using [NotNullIfNotNull(parameter)] for a parameter that doesn't exist

Note that some of those may be useful on ref parameters: void M([DisallowNull]ref string? s) or void M([MaybeNull]ref string s).
Also, the conditional attributes seem useful on non-generic types: [MaybeNullWhen(true)]string s or [NotNullWhen(true)]string? s.

Relates to #36009 (Should [DisallowNull] apply to value types or nullable value types?)

Relates to PR #35955

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions