未验证 提交 5d31a3b0 编写于 作者: J Julien Couvreur 提交者: GitHub

Tweak interaction of Maybe/NotNull with Not/MaybeNullWhen (#37159)

上级 448b9d1d
...@@ -3536,9 +3536,11 @@ static TypeWithState applyPostConditionsWhenTrue(TypeWithState typeWithState, Fl ...@@ -3536,9 +3536,11 @@ static TypeWithState applyPostConditionsWhenTrue(TypeWithState typeWithState, Fl
{ {
bool notNullWhenTrue = (annotations & FlowAnalysisAnnotations.NotNullWhenTrue) != 0; bool notNullWhenTrue = (annotations & FlowAnalysisAnnotations.NotNullWhenTrue) != 0;
bool maybeNullWhenTrue = (annotations & FlowAnalysisAnnotations.MaybeNullWhenTrue) != 0; bool maybeNullWhenTrue = (annotations & FlowAnalysisAnnotations.MaybeNullWhenTrue) != 0;
bool maybeNullWhenFalse = (annotations & FlowAnalysisAnnotations.MaybeNullWhenFalse) != 0;
if (maybeNullWhenTrue) if (maybeNullWhenTrue && !(maybeNullWhenFalse && notNullWhenTrue))
{ {
// [MaybeNull, NotNullWhen(true)] means [MaybeNullWhen(false)]
return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeNull); return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeNull);
} }
else if (notNullWhenTrue) else if (notNullWhenTrue)
...@@ -3552,10 +3554,12 @@ static TypeWithState applyPostConditionsWhenTrue(TypeWithState typeWithState, Fl ...@@ -3552,10 +3554,12 @@ static TypeWithState applyPostConditionsWhenTrue(TypeWithState typeWithState, Fl
static TypeWithState applyPostConditionsWhenFalse(TypeWithState typeWithState, FlowAnalysisAnnotations annotations) static TypeWithState applyPostConditionsWhenFalse(TypeWithState typeWithState, FlowAnalysisAnnotations annotations)
{ {
bool notNullWhenFalse = (annotations & FlowAnalysisAnnotations.NotNullWhenFalse) != 0; bool notNullWhenFalse = (annotations & FlowAnalysisAnnotations.NotNullWhenFalse) != 0;
bool maybeNullWhenTrue = (annotations & FlowAnalysisAnnotations.MaybeNullWhenTrue) != 0;
bool maybeNullWhenFalse = (annotations & FlowAnalysisAnnotations.MaybeNullWhenFalse) != 0; bool maybeNullWhenFalse = (annotations & FlowAnalysisAnnotations.MaybeNullWhenFalse) != 0;
if (maybeNullWhenFalse) if (maybeNullWhenFalse && !(maybeNullWhenTrue && notNullWhenFalse))
{ {
// [MaybeNull, NotNullWhen(false)] means [MaybeNullWhen(true)]
return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeNull); return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeNull);
} }
else if (notNullWhenFalse) else if (notNullWhenFalse)
......
...@@ -28926,7 +28926,7 @@ public string Method2(C<string> wr, bool b) ...@@ -28926,7 +28926,7 @@ public string Method2(C<string> wr, bool b)
); );
} }
[Fact] [Fact, WorkItem(36410, "https://github.com/dotnet/roslyn/issues/36410")]
public void MaybeNull_WithNotNullWhenTrue() public void MaybeNull_WithNotNullWhenTrue()
{ {
var source = var source =
...@@ -28936,9 +28936,9 @@ internal class C<T> where T : class? ...@@ -28936,9 +28936,9 @@ internal class C<T> where T : class?
public bool TryGetTarget([MaybeNull, NotNullWhen(true)] out T target) => throw null!; public bool TryGetTarget([MaybeNull, NotNullWhen(true)] out T target) => throw null!;
public string Method(C<string> wr) public string Method(C<string> wr)
{ {
if (!wr.TryGetTarget(out string? s)) if (wr.TryGetTarget(out string? s))
{ {
s = """"; return s;
} }
return s; // 1 return s; // 1
} }
...@@ -28951,6 +28951,31 @@ public string Method(C<string> wr) ...@@ -28951,6 +28951,31 @@ public string Method(C<string> wr)
); );
} }
[Fact, WorkItem(36410, "https://github.com/dotnet/roslyn/issues/36410")]
public void MaybeNull_WithNotNullWhenFalse()
{
var source =
@"using System.Diagnostics.CodeAnalysis;
internal class C<T> where T : class?
{
public bool TryGetTarget([MaybeNull, NotNullWhen(false)] out T target) => throw null!;
public string Method(C<string> wr)
{
if (wr.TryGetTarget(out string? s))
{
return s; // 1
}
return s;
}
}";
var comp = CreateNullableCompilation(new[] { MaybeNullAttributeDefinition, NotNullWhenAttributeDefinition, source });
comp.VerifyDiagnostics(
// (9,20): warning CS8603: Possible null reference return.
// return s; // 1
Diagnostic(ErrorCode.WRN_NullReferenceReturn, "s").WithLocation(9, 20)
);
}
[Fact] [Fact]
public void MaybeNull_ReturnValue_01() public void MaybeNull_ReturnValue_01()
{ {
...@@ -31397,6 +31422,56 @@ public class Base ...@@ -31397,6 +31422,56 @@ public class Base
comp.VerifyDiagnostics(); comp.VerifyDiagnostics();
} }
[Fact, WorkItem(36410, "https://github.com/dotnet/roslyn/issues/36410")]
public void NotNull_WithMaybeNullWhenTrue()
{
var source =
@"using System.Diagnostics.CodeAnalysis;
internal class C<T> where T : class?
{
public bool TryGetTarget([NotNull, MaybeNullWhen(true)] out T target) => throw null!;
public string Method(C<string?> wr)
{
if (wr.TryGetTarget(out string? s))
{
return s; // 1
}
return s;
}
}";
var comp = CreateNullableCompilation(new[] { NotNullAttributeDefinition, MaybeNullWhenAttributeDefinition, source });
comp.VerifyDiagnostics(
// (9,20): warning CS8603: Possible null reference return.
// return s; // 1
Diagnostic(ErrorCode.WRN_NullReferenceReturn, "s").WithLocation(9, 20)
);
}
[Fact, WorkItem(36410, "https://github.com/dotnet/roslyn/issues/36410")]
public void NotNull_WithMaybeNullWhenFalse()
{
var source =
@"using System.Diagnostics.CodeAnalysis;
internal class C<T> where T : class?
{
public bool TryGetTarget([NotNull, MaybeNullWhen(false)] out T target) => throw null!;
public string Method(C<string?> wr)
{
if (wr.TryGetTarget(out string? s))
{
return s;
}
return s; // 1
}
}";
var comp = CreateNullableCompilation(new[] { NotNullAttributeDefinition, MaybeNullWhenAttributeDefinition, source });
comp.VerifyDiagnostics(
// (11,16): warning CS8603: Possible null reference return.
// return s; // 1
Diagnostic(ErrorCode.WRN_NullReferenceReturn, "s").WithLocation(11, 16)
);
}
[Fact] [Fact]
public void NullableAnnotationAttributes_Deconstruction() public void NullableAnnotationAttributes_Deconstruction()
{ {
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册