From 1392bececfc803e6d364c06e118a143d65a1f4fa Mon Sep 17 00:00:00 2001 From: Iliar Turdushev Date: Thu, 26 Sep 2024 11:58:10 +0200 Subject: [PATCH] Added a comment describing the need to use "!" operator --- src/Polly.Core/ResilienceProperties.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Polly.Core/ResilienceProperties.cs b/src/Polly.Core/ResilienceProperties.cs index 6866be4a190..a5f43d54086 100644 --- a/src/Polly.Core/ResilienceProperties.cs +++ b/src/Polly.Core/ResilienceProperties.cs @@ -29,6 +29,12 @@ public bool TryGetValue(ResiliencePropertyKey key, [MaybeNullWhe } else if (val is null) { + // We have to use null-forgiving operator "!" here to suppress a null-state analysis warning. + // The reason is the following. The output type "TValue" doesn't have any type constraints as + // "notnull", "class" or "struct", therefore the analyzer considers "TValue" as non-nullable + // and warns us that we're assigning "null" to it. But that's not correct, because "TValue" + // could be a nullable type, e.g. "string?", and assigning "null" to it is correct. Therefore + // it is reasonable to use "!" here to suppress the warning. value = default!; return true; }