-
Notifications
You must be signed in to change notification settings - Fork 113
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
[Nullable Context] Proposal for better null-state static analysis with IPostConfigureOptions
#6950
[Nullable Context] Proposal for better null-state static analysis with IPostConfigureOptions
#6950
Conversation
// Guaranteed to not be null by StoragePostConfigureOptions.PostConfigure. | ||
string dumpTempFolder = _storageOptions.CurrentValue.DumpTempFolder!; | ||
StorageOptions options = _storageOptions.CurrentValue; | ||
OptionsUtils.ThrowIfNotConfigured<StorageOptions>(options.Configured); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This properly informs the compiler's null-state static analysis because:
Options.ThrowIfNotConfigured
statically tell the compiler that ifoptions.Configured
is false it will never return.StorageOptions.Configured
's getter method statically tells the compiler that if it is true then certain members (e.g.DumpTempFolder
) will not be null.
Ref: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/nullable-analysis
IPostConfigureOptions
IPostConfigureOptions
|
||
internal bool Configured | ||
{ | ||
[MemberNotNullWhen(true, nameof(DumpTempFolder))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: This supports specifying multiple members, e.g.
[MemberNotNullWhen(true, nameof(DumpTempFolder), nameof(SharedLibraryPath))]
The 'no-recent-activity' label has been added to this pull request due to four weeks without any activity. If there is no activity in the next six weeks, this pull request will automatically be closed. You can learn more about our stale PR policy here: https://github.com/dotnet/dotnet-monitor/blob/main/CONTRIBUTING.md#stale-pr-policy |
Summary
This PR is a proposal for how we can have better null-state static analysis when dealing with our Options classes the rely on
IPostConfigureOptions
to set default values.(Also partially addresses #6929)
Release Notes Entry