xunit/xunit#3423: Suppress CS8618 for members set in InitializeAsync#203
Open
MatanTsach wants to merge 2 commits intoxunit:mainfrom
Open
xunit/xunit#3423: Suppress CS8618 for members set in InitializeAsync#203MatanTsach wants to merge 2 commits intoxunit:mainfrom
MatanTsach wants to merge 2 commits intoxunit:mainfrom
Conversation
Add a DiagnosticSuppressor that suppresses CS8618 (non-nullable field/property not initialized) when the member is directly assigned in an IAsyncLifetime.InitializeAsync implementation. Handles both fields and properties, including this-qualified access. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Author
|
@dotnet-policy-service agree |
When a class has an explicit constructor, CS8618 fires on the constructor rather than the field/property. The suppressor now resolves the target member by checking AdditionalLocations and falling back to parsing the member name from the diagnostic message. Also adds V3 (ValueTask-based IAsyncLifetime) test coverage for all scenarios, bringing total tests from 6 to 16. Inspired by edge cases identified by @ssaporito in #3423.
Author
|
Hi, just checking in — is there anything else needed from my side on this PR? Happy to make adjustments if needed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
DiagnosticSuppressorthat suppresses CS8618 ("Non-nullable field/property must contain a non-null value when exiting constructor") when the member is directly assigned in anIAsyncLifetime.InitializeAsyncimplementation. Fixes xunit/xunit#3423.Changes
NonNullableFieldInitializationSuppressor— walks theInitializeAsyncmethod body looking for direct assignments to the flagged memberCS8618_Suppressiondescriptor toDescriptors.Suppressors.csVerifyCompilerWarningSuppressortest helpers toCSharpVerifier.Suppressors.cs(setsCompilerDiagnostics.Warningsso CS8618 is included in analysis)this.-qualified access, and selective suppression with multiple fieldsDesign
The suppressor:
IAsyncLifetimeInitializeAsyncimplementation viaFindImplementationForInterfaceMemberKnown Limitations
InitializeAsyncbody — assignments through helper methods called fromInitializeAsyncare not detected (conservative approach)Testing