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

C#8 Nullable References #2

Closed
SeriousM opened this issue Mar 16, 2020 · 8 comments
Closed

C#8 Nullable References #2

SeriousM opened this issue Mar 16, 2020 · 8 comments
Labels
enhancement New feature or request

Comments

@SeriousM
Copy link

Hi! Your article was very nice, ty for that.
We're using the new feature from c# to annotate nullable reference types with poco classes.
Since the new feature requires properties to be initialized if not mentioned in a constructor we came up with this pattern:

public class ModelPoco
{
  public User CurrentUser { get; set; } = default!;
}

This way the adaption on legacy code is quite simple by just appending ` = default!;" to each property.

Could your extension help to avoid this additional code?

Best, Bernhard

@cezarypiatek
Copy link
Owner

If you are going to use [InitOnly] or [InitRequired] with non-nullable fields then it's perfectly fine to disable CS8618 rule. You can do that via .editorconfig with the following entry

[*.cs]
#CS8618: Non-nullable property ‘PropertyName’ is uninitialized.
dotnet_diagnostic.CS8618.severity = none

@SeriousM
Copy link
Author

Hmm, well, our codebase is quite large and the disabling the rule CS8618 would be counter-intuitive. Could your analyser disable this rule just for types/members that have your attribute applied?

@cezarypiatek
Copy link
Owner

It looks like Roslyn support such scenario thanks to DiagnosticSuppressor https://docs.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.diagnostics.diagnosticsuppressor?view=roslyn-dotnet

I will try to implement it. Thanks for reporting this suggestion.

@cezarypiatek cezarypiatek added the enhancement New feature or request label Mar 16, 2020
@SeriousM
Copy link
Author

Thank you for investigating it!

@cezarypiatek
Copy link
Owner

I have a working POC. I will try to finish it and publish it today afternoon.

@cezarypiatek
Copy link
Owner

@SeriousM I've just released version 2.1.31 with the requested feature. Please let me know if it works as expected.

@cezarypiatek
Copy link
Owner

@SeriousM I've introduced another attribute [assembly:InitRequiredForNotNull] which works on assembly level. Everything described on my blog https://cezarypiatek.github.io/post/better-non-nullable-handling/

@stijnherreman
Copy link

For anyone trying out this library with VS Code: DiagnosticSuppressor is not yet supported by OmniSharp (OmniSharp/omnisharp-roslyn#1711) so the Problems panel will still show the warnings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants