Skip to content

Use NotNullWhen(true) instead of MaybeNullWhen(false) for concrete-type Try-methods - #655

Merged
rexm merged 1 commit into
masterfrom
fix/issue-654-nullable-try-pattern
Aug 6, 2026
Merged

Use NotNullWhen(true) instead of MaybeNullWhen(false) for concrete-type Try-methods#655
rexm merged 1 commit into
masterfrom
fix/issue-654-nullable-try-pattern

Conversation

@rexm

@rexm rexm commented Aug 6, 2026

Copy link
Copy Markdown
Member

Summary

PR #642 annotated all Try* out-parameters with [MaybeNullWhen(false)] out T value (non-nullable T), regardless of whether T was an unconstrained generic type parameter or a concrete reference type. That pattern is only correct for the generic case (mirroring Dictionary<TKey,TValue>.TryGetValue, which can't write TValue? without a class constraint). For concrete reference types, the BCL's own convention (e.g. Uri.TryCreate) is [NotNullWhen(true)] out T? - which also gives a stronger compiler guarantee: dereferencing the out value without checking the return value first now correctly warns, whereas MaybeNullWhen(false) on a non-nullable T silently allowed it.

This is a compile-time-only change (nullable annotations erase to metadata, not IL), so it's not binary breaking, and normal if (TryX(..., out var x)) call sites are unaffected.

  • IHelperResolver.TryResolveHelper / TryResolveBlockHelper
  • IObjectDescriptorProvider.TryGetDescriptor and all implementers (ObjectDescriptorProvider, ObjectDescriptorFactory, LayoutViewModel.DescriptorProvider, the dictionary/enumerable/dynamic descriptor providers)
  • ObjectDescriptor.TryCreate (both overloads)
  • IFormatterProvider.TryCreateFormatter and all implementers
  • TypeExtensions.IsAssignableToGenericType
  • BlockAccumulatorContext.IsDetachedClosingElement

Left unchanged: the generic-TValue collection Try-methods (LookupSlim, DictionarySlim, FixedSizeDictionary, CascadeIndex, ObservableIndex) - these correctly mirror the BCL's own generic pattern already.

Fixes #654

Test plan

  • dotnet build across all target frameworks (netstandard1.3/2.0/2.1, net6.0, net8.0) - 0 errors, no new warnings introduced (verified via clean-rebuild diff against master)
  • dotnet test - 1867/1867 passing

…te-type Try-methods

PR #642 annotated all Try* out-parameters with [MaybeNullWhen(false)] on a
non-nullable T, which is the correct pattern for unconstrained generic type
parameters (as in Dictionary<TKey,TValue>.TryGetValue) but not for concrete
reference types. For concrete types, the BCL convention (e.g. Uri.TryCreate)
is [NotNullWhen(true)] out T? - this also gives callers a compiler warning
if they dereference the out value without checking the return value first,
which MaybeNullWhen(false) on non-nullable T silently allows.

Updates IHelperResolver, IObjectDescriptorProvider (and implementers),
IFormatterProvider (and implementers), TypeExtensions.IsAssignableToGenericType,
and BlockAccumulatorContext.IsDetachedClosingElement. Generic-TValue Try-methods
(LookupSlim, DictionarySlim, FixedSizeDictionary, CascadeIndex, ObservableIndex)
are left as-is since they correctly mirror the BCL generic pattern.

Fixes #654
@rexm
rexm enabled auto-merge August 6, 2026 01:27
@rexm
rexm disabled auto-merge August 6, 2026 01:27
@rexm
rexm enabled auto-merge August 6, 2026 01:28
@sonarqubecloud

sonarqubecloud Bot commented Aug 6, 2026

Copy link
Copy Markdown

@rexm
rexm merged commit 2ff8e5c into master Aug 6, 2026
7 checks passed
@rexm
rexm deleted the fix/issue-654-nullable-try-pattern branch August 6, 2026 01:37
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.

A question about the new nullable enable and [MaybeNullWhen(false)]

1 participant