Support NotNullIfNotNull on Task<T?> return type #2793
Replies: 11 comments 17 replies
-
Moving to csharplang as this is a design question around the language, not an implementation bug in the compiler. |
Beta Was this translation helpful? Give feedback.
-
Also, Dapper has Otherwise, you get no warnings if you forget to check a reference type for null: var maybeNull = await QuerySingleOrDefaultAsync<string>(...);
maybeNull.IndexOf("foo"); In this particular instance, a nice alternative would be if there was a way for |
Beta Was this translation helpful? Give feedback.
-
I just hit this case too. I'll never return a null Task, but the T will be null if the parameter is null. This annotation says I won't return a task that's null, but I'm much more interested in saying that T won't be null. [return:System.Diagnostics.CodeAnalysis.NotNullIfNotNull("instance")]
public static Task<MyType2?> ConvertObject(this MyType? instance)
{
if (instance == null)
return Task.FromResult<MyType2?>(null);
return Task.Result(ConvertObjectHelper(instance)); //Won't return null MyType2
} |
Beta Was this translation helpful? Give feedback.
-
I'm hitting this case as well, using .NET 5. @jaredpar Do you have any idea whether this is under the attention of the C# language team? Or should this be registered as an Issue to achieve that? |
Beta Was this translation helpful? Give feedback.
-
Maybe something like this: public Task<[NotNullIfNotNull("source")] string?> PassThroughAsync(string? value) |
Beta Was this translation helpful? Give feedback.
-
I'll consider adding this to #6888 when revising it. It feels like a broader, more coherent take on nullable attributes and tasks would be appropriate. |
Beta Was this translation helpful? Give feedback.
-
@RikkiGibson I would argue that this two are a bit different. Issue is not with string? s1 = GetValueOrDefault<string>(null);
string s2 = GetValueOrDefault<string>(""); // works, C# realizes that the return value can't be null
IEnumerable<string?> e1 = GetValuesOrDefault<string>(null);
IEnumerable<string> e2 = GetValuesOrDefault<string>(""); // I want to get rid of the warning here
[return: NotNullIfNotNull(nameof(defaultValue))]
public static T? GetValueOrDefault<T>(T? defaultValue)
{
return GetValueFromSomewhere<T>() ?? defaultValue;
}
[return: ???]
public IEnumerable<T?> GetValuesOrDefault<T>(T? defaultValue)
{
return GetValuesFromSomewhere<T>().Select(x => x ?? defaultValue);
} |
Beta Was this translation helpful? Give feedback.
-
(This is really disappointing that 4.5 years later we don't have support for this) Could we add a special case for |
Beta Was this translation helpful? Give feedback.
-
This is still not an insignificant issue when writing any kind of library code, fallback value parameters are a rather common usecase. The rather obvious solution specifically for Task ValueTask types would be: [result: NotNullIfNotNull(nameof(fallbackValue))]
public async ValueTask<T?> MyMethodAsync(T? fallbackValue = default(T?))
{
/* Code guaranteeing the contract is fulfilled */
} |
Beta Was this translation helpful? Give feedback.
-
Is this even a language question? If I recall correctly, nullability flow is handled by analyzers, not the compiler. Essentially, regardless of modifying the existing attributes or adding new ones, this problem would be handled by In fact, I'm sure one could write an analyzer that suppresses nullability warnings based on the attributes. |
Beta Was this translation helpful? Give feedback.
-
Direly needed. No progress yet? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Version Used: .NET Core 3.0 preview 8
Steps to Reproduce:
Expected Behavior:
No warning
Actual Behavior:
Warning CS8600 Converting null literal or possible null value to non - nullable type.
Beta Was this translation helpful? Give feedback.
All reactions