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

Analyzer to ensure correct C# nullable annotation when AllowDefault = true #317

Open
drewnoakes opened this issue May 5, 2022 · 1 comment

Comments

@drewnoakes
Copy link
Member

#nullable enable

[Import(AllowDefault = true)]
public IBar Bar { get; private set; }

[ImportingConstructor]
public Foo([Import(AllowDefault = true)] IBar bar)
{}

Both of these imports lead to potential runtime problems if a null value is not guarded against.

When in a nullable context, use of AllowDefault = true on a non-nullable type should be flagged with a diagnostic. A codefix could also be added to change the type to be nullable.

@AArnott
Copy link
Member

AArnott commented May 5, 2022

Strictly speaking, no import of a reference type is guaranteed to be non-null. Because you can export a property that returns null and that's perfectly legal.

But at least when AllowDefault = true it's much more likely to be null, so an analyzer that requires a nullable annotation at least for that case sounds like a good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants