Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIs that could use annotations or attributes for nullability #28086

Closed
jcouv opened this issue Dec 6, 2018 · 1 comment
Closed

APIs that could use annotations or attributes for nullability #28086

jcouv opened this issue Dec 6, 2018 · 1 comment
Labels
area-Meta enhancement Product code improvement that does NOT require public API changes/additions
Milestone

Comments

@jcouv
Copy link
Member

jcouv commented Dec 6, 2018

In C# 8.0 with the nullability analysis turned on, libraries that don't carry explicit nullable annotations are treated as "oblivious", which means the analysis assumes the best (to minimize nullability warnings).

This is generally a reasonable assumption, but it hides some potentially useful warnings. As a rule of thumb:

  • any API which could return a null should declare that.
  • any API which throws an exception on a null input should declare that. For instance, Path.GetDirectoryName(string! name).

The experience of using nullability analysis on APIs can be further enhanced with some attributes that provide hints about the semantics of the API:

  • bool string.IsNullOrEmpty([NotNullWhenFalse] string value)
  • bool string.IsNullOrWhiteSpace([NotNullWhenFalse] string value)
  • [MaybeNull] T FirstOrDefault(...)
  • Debug.Assert([EnsuresTrue] ...)

We are still working to finalize the set of such attributes (tracked by dotnet/roslyn#26761).

I'm opening this issue to collect such examples, so that we have some idea which APIs should be annotated first, once we are ready to do that.

@danmoseley
Copy link
Member

@jcouv this issue seems maybe redundant right now. I'm not sure all the above still applies, and discussion is in dotnet/roslyn#26761.

Feel free to reopen.

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 3.0 milestone Feb 1, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Meta enhancement Product code improvement that does NOT require public API changes/additions
Projects
None yet
Development

No branches or pull requests

3 participants