Skip to content

Fix CS8618 suggesting 'required' on events#82424

Open
apoorvdarshan wants to merge 2 commits intodotnet:mainfrom
apoorvdarshan:fix/cs8618-required-on-events
Open

Fix CS8618 suggesting 'required' on events#82424
apoorvdarshan wants to merge 2 commits intodotnet:mainfrom
apoorvdarshan:fix/cs8618-required-on-events

Conversation

@apoorvdarshan
Copy link

Summary

Fixes #81679

CS8618 fires for non-nullable events with the message suggesting required, but required is not valid on events (CS0106). This PR adds a new dedicated warning CS9360 (WRN_UninitializedNonNullableEvent) that omits the invalid required suggestion:

Non-nullable event 'X' must contain a non-null value when exiting constructor. Consider declaring the event as nullable.

  • Follows the precedent of WRN_UninitializedNonNullableBackingField (CS9264)
  • New error code WRN_UninitializedNonNullableEvent = 9360 with event-specific message
  • NullableWalker now selects the new code when symbol.Kind == SymbolKind.Event
  • All existing event-related test assertions updated to use the new warning

Test plan

  • eng/generate-compiler-code.cs regenerates ErrorFacts.Generated.cs correctly
  • dotnet build src/Compilers/CSharp/Portable — 0 warnings, 0 errors
  • UninitializedNonNullableFieldTests — 100 tests pass
  • DiagnosticTest — 68 tests pass
  • NullableContextTests — 2615 tests pass
  • AttributeTests_Nullable — 169 tests pass
  • SymbolEqualityTests, NullablePublicAPITests, FieldKeywordTests — all pass

Add new warning CS9360 (WRN_UninitializedNonNullableEvent) for non-nullable
events that omits the invalid 'required' modifier suggestion, since 'required'
is not valid on events (CS0106).
@apoorvdarshan apoorvdarshan requested a review from a team as a code owner February 17, 2026 07:44
@dotnet-policy-service dotnet-policy-service bot added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Feb 17, 2026
@AlekseyTs AlekseyTs requested a review from 333fred February 18, 2026 06:19
@jjonescz
Copy link
Member

Looks like some more tests need updating, see failing CI.

Three tests used #pragma warning disable 8618 to suppress the old
event warning but not the new CS9360. Add 9360 to those pragmas.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-Compilers Community The pull request was submitted by a contributor who is not a Microsoft employee.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CS8618 suggests using required on events.

2 participants

Comments