Skip to content

Instantly share code, notes, and snippets.

@thomaslevesque
Created June 5, 2020 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaslevesque/4317d8562dc47cdfef499a12d1d214ac to your computer and use it in GitHub Desktop.
Save thomaslevesque/4317d8562dc47cdfef499a12d1d214ac to your computer and use it in GitHub Desktop.
Repro for CS8762 false positive
using System.Diagnostics.CodeAnalysis;
namespace TestNullable
{
class Test
{
static bool TrySomething1(string s, [NotNullWhen(false)] out string? failReason)
{
// warning CS8762: Parameter 'failReason' must have a non-null value when exiting with 'false'.
return s.Length % 2 == 0
? Foo(s, out failReason)
: Bar(s, out failReason);
}
static bool TrySomething2(string s, [NotNullWhen(false)] out string? failReason)
{
// no warning
if (s.Length % 2 == 0)
{
return Foo(s, out failReason);
}
else
{
return Bar(s, out failReason);
}
}
static bool Foo(string s, [NotNullWhen(false)] out string? failReason)
{
// Actual implementation not relevant
failReason = null;
return true;
}
static bool Bar(string s, [NotNullWhen(false)] out string? failReason)
{
// Actual implementation not relevant
failReason = null;
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment