You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classFoo{publicTaskCompletionSource<bool>?_completionSource;publicboolIsStopped=>!(_completionSource isnull);// orpublicboolIsStopped=>_completionSource!=null;publicvoidBar(){if(IsStopped){// Possible dereference
_completionSource.SetResult(true);}if(!(_completionSource isnull)){// This is fine
_completionSource.SetResult(true);}}}
I know I can stick a dammit operator in there, was just wondering if there are plans to support such null checks in analysis.
The text was updated successfully, but these errors were encountered:
This will be supported in C# 9 after all, but requires an attribute to be placed saying that the return value of IsStopped affects our knowledge of the state of _completionSource.
class Foo
{
public TaskCompletionSource<bool>? _completionSource;
[MemberNotNullWhen(true, "_completionSource")]
public bool IsStopped => !(_completionSource is null);
public void Bar()
{
if (IsStopped)
{
// Possible dereference
_completionSource.SetResult(true);
}
if (!(_completionSource is null))
{
// This is fine
_completionSource.SetResult(true);
}
}
}
namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)]
public sealed class MemberNotNullAttribute : Attribute
{
public MemberNotNullAttribute(params string[] members) { }
public MemberNotNullAttribute(string member) { }
}
}
namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)]
public sealed class MemberNotNullWhenAttribute : Attribute
{
public MemberNotNullWhenAttribute(bool when, params string[] members) { }
public MemberNotNullWhenAttribute(bool when, string member) { }
}
}
I know I can stick a dammit operator in there, was just wondering if there are plans to support such null checks in analysis.
The text was updated successfully, but these errors were encountered: