jstedfast / MimeKit

A .NET MIME creation and parser library with support for S/MIME, PGP, DKIM, TNEF and Unix mbox spools.
http://www.mimekit.net
MIT License
1.81k stars 368 forks source link

Annotate nullable reference types in public APIs #1067

Open daniel-lerch opened 3 weeks ago

daniel-lerch commented 3 weeks ago

Is your feature request related to a problem? Please describe. I just came across a NullReferenceException in my app which uses nullable reference types because I did not read MimeKit's documentation properly and missed that a type can be null.

Describe the solution you'd like I consider nullable reference types a great feature of C# that makes you as a developer think twice whether you can really omit a null check. In my opinion it would be great if MimeKit would at least add nullable attributes to its public API. It might even make sense to enable nullable reference types for the entire code base.

Describe alternatives you've considered N/A

Additional context N/A

jstedfast commented 3 weeks ago

The problem is that I'm still trying to support .NET Framework which doesn't have this.

I'm not saying it can't be done, it's just a lot of work to #if/#else/#endif everything.

Sicos1977 commented 3 weeks ago

As far as I know this should work in .net framework. You probably have to upgrade to language version 8 or higher.

daniel-lerch commented 3 weeks ago

I can confirm that nullable reference types as such do work with .NET Framework. Some very useful attributes like [NotNullWhen] or [MemberNotNull] require .NET Standard 2.1 or .NET 5.0.

I enabled this feature for some classes in #1068 and added #if statements to add attributes for more modern .NET versions.

I would be willing to contribute further pull request to enable nullable reference types for more classes. Enabling for the entire project would be too much work at once for me, though.

jstedfast commented 3 days ago

FWIW, I created a new branch for this work. I also added compatibility attributes to reduce #if/#else's littering the codebase.

I just need to find time to work on it more, haha.

I wonder if I should look into writing a Roslyn-based tool to automate this.