Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only enforce Member/NotNullWhen on constants #48425

Merged
merged 7 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8213,10 +8213,10 @@ private TypeWithAnnotations GetDeclaredParameterResult(ParameterSymbol parameter

private void SplitIfBooleanConstant(BoundExpression node)
{
if (node.ConstantValue is { IsBoolean: true })
if (node.ConstantValue is { IsBoolean: true, BooleanValue: bool booleanValue })
{
Split();
if (node.ConstantValue.BooleanValue)
if (booleanValue)
{
StateWhenFalse = UnreachableState();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24151,8 +24151,6 @@ bool Init()
}
return !Init(); // 2, 3
}

bool M([MaybeNullWhen(true)]out string s) => throw null!;
}
", MemberNotNullWhenAttributeDefinition, MaybeNullWhenAttributeDefinition });
Copy link
Member

@333fred 333fred Oct 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused? #Resolved


Expand Down Expand Up @@ -25228,6 +25226,23 @@ public static bool TryGetValue([NotNullWhen(true)] out string? s)
comp.VerifyDiagnostics();
}

[Fact]
public void NotNullWhenTrue_EnforceInMethodBody_ConditionalWithThrow()
{
var source = @"
using System.Diagnostics.CodeAnalysis;
public class C
{
static bool M([NotNullWhen(true)] object? o)
{
return o == null ? true : throw null!;
}
}
";
var comp = CreateNullableCompilation(new[] { source, NotNullWhenAttributeDefinition });
comp.VerifyDiagnostics();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I super don't like this behavior. It's obviously incorrect code.

}

[Fact]
public void NotNullWhenTrue_EnforceInMethodBody_WithMaybeNull_CallingObliviousAPI()
{
Expand Down