Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
5d31a3b0
R
roslyn
项目概览
lwm1986
/
roslyn
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
roslyn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
5d31a3b0
编写于
7月 12, 2019
作者:
J
Julien Couvreur
提交者:
GitHub
7月 12, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Tweak interaction of Maybe/NotNull with Not/MaybeNullWhen (#37159)
上级
448b9d1d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
84 addition
and
5 deletion
+84
-5
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
+6
-2
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
...rp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
+78
-3
未找到文件。
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
浏览文件 @
5d31a3b0
...
@@ -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
)
...
...
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
浏览文件 @
5d31a3b0
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录