Am I using this correctly? #258
-
Hello! Am I using this incorrectly or why does the compiler think that the return of var sptf = Guard.Against.Null(sample.Sptfs.FirstOrDefault(x => x.Key == SptfKey), nameof(SptfKey), "I have no idea why this might be null");
var thisMightBeNull = sptf.Key; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@mwasson74 This is happening because the compiler does not have enough information to tell that var thisMightBeNull = sptf!.Key @ardalis This is possibly because public static T Null<T>(this IGuardClause guardClause,
[NotNull] [ValidatedNotNull] T input,
[CallerArgumentExpression("input")] string? parameterName = null,
string? message = null) If the T input to T? input the warning mentioned in the question goes away. Edit: PR #261 which fixes this issue has been merged. The fix is available since version 4.1.0. |
Beta Was this translation helpful? Give feedback.
@mwasson74 This is happening because the compiler does not have enough information to tell that
sptf.Key
will not benull
after callingGuard.Against.Null()
. For now, you can safely suppress this warning using the null forgiving operator (!
):@ardalis This is possibly because
Guard.Against.Null
is defined as:If the
input
parameter definition is changed from:to
the warning mentioned in the question goes away.
Since the type
T
does not h…