Skip to content

NullabilityInfoContext does not properly respect CodeAnalysis attribute on indexer property #63849

Closed

Description

@madelson

Description

In most cases code analysis attributes like AllowNull are respected, but this is not true for indexer properties.

Reproduction Steps

public void Main()
{
        var context = new NullabilityInfoContext();
        var indexerInfo = context.Create(typeof(HasIndexer).GetProperty("Item")!);
       Console.WriteLine(indexerInfo.WriteState); // NotNull (should be Nullable)
}

    public class HasIndexer
    {
        [AllowNull] public string this[string? s]
        {
            get => default!;
            set { }
        }
    }

Expected behavior

The write state should be nullable because the following compiles without warnings:

new HasIndexer()["a"] = null;

Actual behavior

Write state is not null as if the attribute were not present.

Regression?

No response

Known Workarounds

No response

Configuration

VS 17.0.2, .NET 6, Windows 10 x64.

I don't have any reason to believe this is configuration specific.

Other information

CC @Shane32

Activity

  1. added
    untriaged
    on Jan 17, 2022
  2. danmoseley commented on Jan 17, 2022

    @danmoseley
    Contributor

    Thanks for opening these nullable annotation bugs @madelson . Are you interested in offering a PR for any of them?

  3. madelson commented on Jan 17, 2022

    @madelson
    ContributorAuthor

    @danmoseley yes I'm working on fixes for some of them with the idea of sending a PR.

  4. removed
    untriaged
    on Jan 19, 2022
  5. added this to the 7.0.0 milestone on Jan 19, 2022
  6. added a commit that references this issue on Jan 22, 2022
    93cb0f6
  7. ghost added
    in-pr
    on Jan 22, 2022
  8. ghost removed
    in-pr
    on Jan 26, 2022
  9. ghost locked as resolved and limited conversation to collaborators on Feb 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions