question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Returning 0 from a generic async method triggers NullGuard

See original GitHub issue

Here’s a simplified version of the situation where I noticed this happening:

public async Task<T> GetThing(Func<T> getThing)
{
    return getThing();
}
GetThing(() => 0);

I’m guessing this happens because you can’t compare the generic return value to null, since it may not be an object. So the generated code is probably comparing to default(T), but for struct types default(T) is not null, and is typically a perfectly legitimate value.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
j2jensencommented, Sep 2, 2018

@tom-englert You, sir, are a hero.

1reaction
tom-englertcommented, Sep 1, 2018

I’ll take care of this

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tell the caller that a `async Task<T>` method may return null
My situation is similar to the one described Proper nullable annotation for async generic method that may return default(T).
Read more >
Cannot Have Nullable Return of Async Generic Method
However, I'm running into a problem with a Generic Async method. [return: MaybeNull] public async Task<T> GetAsync<T>() => default;.
Read more >
Returning null from Task-returning methods in C#
In this blog post, we'll understand this behavior and the difference between returning null from an async vs. non- async method in C#....
Read more >
Async return types (C#)
Learn about the return types that async methods can have in C# with code examples for each type.
Read more >
Help with async await
When the await is done awaiting, it automatically uses Unity SynchronizationContext and continues from where it left off some frames later, at ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found