Skip to content

refactor: replace null-forgiving operators with checked alternatives - #1646

Merged
BenjaminMichaelis merged 1 commit into
mainfrom
refactor/remove-null-forgiving
Nov 15, 2026
Merged

BenjaminMichaelis merged 1 commit into
mainfrom
refactor/remove-null-forgiving

Conversation

@BenjaminMichaelis

Copy link
Copy Markdown
Member

Summary

Removes null-forgiving operators (!) where flow analysis, a checked throw, or the TUnit IsNotNull() return value can express the invariant. No behaviour change in production paths.

Replacement patterns

Pattern Where
Redundant ! removed (flow analysis already proves non-null) HomeController (siteMapping), ServiceCollectionExtensions.ResolveLocalEndpoint
Pattern/guard on the field instead of a pre-computed bool BookSearchTool (_SearchService is not null)
?? throw new InvalidOperationException("...") BookContentTool, AISearchService (MaxBy), ServiceCollectionExtensions (connection string), RequiresLinuxContainersAttribute (Process.Start; already caught by the existing InvalidOperationException filter)
?? string.Empty ReferralTrackingMiddleware (QueryString.Value)
Use value returned by await Assert.That(x).IsNotNull() McpApiTokenServiceTests, LocalChatServiceTests, PwnedPasswordValidatorTests

! intentionally kept

  • EF Core DbSet<T> ... = null! in EssentialCSharpWebContext (framework-assigned).
  • get => _Input!; in 14 Identity Razor Pages (model-bound [BindProperty] backing field; framework-assigned).
  • null! arguments in tests that intentionally pass null to exercise argument validation or satisfy mocked constructors (PwnedPasswordTests, PwnedPasswordValidatorTests, SiteMappingTests, AIChatServiceModerationTests).

Verification

  • dotnet build EssentialCSharp.Web.slnx -c Release /p:AccessToNugetFeed=false: 0 warnings, 0 errors
  • EssentialCSharp.Web.Tests: 202 passed
  • EssentialCSharp.Chat.Tests: 41 passed

@BenjaminMichaelis
BenjaminMichaelis merged commit d000f3d into main Nov 15, 2026
21 checks passed
@BenjaminMichaelis
BenjaminMichaelis deleted the refactor/remove-null-forgiving branch November 15, 2026 06:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant