Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
aa98f1a5
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,发现更多精彩内容 >>
提交
aa98f1a5
编写于
12月 06, 2017
作者:
S
Steve Chovanec
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Recognize condition with logical negation
上级
250d883c
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
307 addition
and
8 deletion
+307
-8
src/EditorFeatures/CSharpTest/UseNullPropagation/UseNullPropagationTests.cs
.../CSharpTest/UseNullPropagation/UseNullPropagationTests.cs
+179
-4
src/EditorFeatures/VisualBasicTest/UseNullPropagation/UseNullPropagationTests.vb
...alBasicTest/UseNullPropagation/UseNullPropagationTests.vb
+115
-4
src/Features/Core/Portable/UseNullPropagation/AbstractUseNullPropagationDiagnosticAnalyzer.cs
...opagation/AbstractUseNullPropagationDiagnosticAnalyzer.cs
+13
-0
未找到文件。
src/EditorFeatures/CSharpTest/UseNullPropagation/UseNullPropagationTests.cs
浏览文件 @
aa98f1a5
...
...
@@ -506,7 +506,7 @@ void M(C c)
[
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)]
public
async
Task
TestWithNullableTypeAndReferenceEquals
()
public
async
Task
TestWithNullableTypeAndReferenceEquals
1
()
{
await
TestInRegularAndScriptAsync
(
@"
...
...
@@ -531,7 +531,7 @@ void M(C c)
[
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)]
public
async
Task
TestWithNullableTypeAndReferenceEquals
Reversed
()
public
async
Task
TestWithNullableTypeAndReferenceEquals
2
()
{
await
TestInRegularAndScriptAsync
(
@"
...
...
@@ -556,7 +556,7 @@ void M(C c)
[
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)]
public
async
Task
TestWithNullableTypeAndReferenceEqualsWithObject
()
public
async
Task
TestWithNullableTypeAndReferenceEqualsWithObject
1
()
{
await
TestInRegularAndScriptAsync
(
@"
...
...
@@ -581,7 +581,7 @@ void M(C c)
[
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)]
public
async
Task
TestWithNullableTypeAndReferenceEqualsWithObject
Reversed
()
public
async
Task
TestWithNullableTypeAndReferenceEqualsWithObject
2
()
{
await
TestInRegularAndScriptAsync
(
@"
...
...
@@ -595,6 +595,181 @@ void M(C c)
}"
,
@"
class C
{
public int? f;
void M(C c)
{
int? x = c?.f;
}
}"
);
}
[
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)]
public
async
Task
TestWithNullableTypeAndNotIsNull
()
{
await
TestInRegularAndScriptAsync
(
@"
class C
{
public int? f;
void M(C c)
{
int? x = [||]!(c is null) ? c.f : null;
}
}"
,
@"
class C
{
public int? f;
void M(C c)
{
int? x = c?.f;
}
}"
);
}
[
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)]
public
async
Task
TestWithNullableTypeAndLogicalNotReferenceEquals1
()
{
await
TestInRegularAndScriptAsync
(
@"
class C
{
public int? f;
void M(C c)
{
int? x = [||]!ReferenceEquals(c, null) ? c.f : null;
}
}"
,
@"
class C
{
public int? f;
void M(C c)
{
int? x = c?.f;
}
}"
);
}
[
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)]
public
async
Task
TestWithNullableTypeAndLogicalNotReferenceEquals2
()
{
await
TestInRegularAndScriptAsync
(
@"
class C
{
public int? f;
void M(C c)
{
int? x = [||]!ReferenceEquals(null, c) ? c.f : null;
}
}"
,
@"
class C
{
public int? f;
void M(C c)
{
int? x = c?.f;
}
}"
);
}
[
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)]
public
async
Task
TestWithNullableTypeAndLogicalNotReferenceEqualsWithObject1
()
{
await
TestInRegularAndScriptAsync
(
@"
class C
{
public int? f;
void M(C c)
{
int? x = [||]!object.ReferenceEquals(c, null) ? c.f : null;
}
}"
,
@"
class C
{
public int? f;
void M(C c)
{
int? x = c?.f;
}
}"
);
}
[
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)]
public
async
Task
TestWithNullableTypeAndLogicalNotReferenceEqualsWithObject2
()
{
await
TestInRegularAndScriptAsync
(
@"
class C
{
public int? f;
void M(C c)
{
int? x = [||]!object.ReferenceEquals(null, c) ? c.f : null;
}
}"
,
@"
class C
{
public int? f;
void M(C c)
{
int? x = c?.f;
}
}"
);
}
[
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)]
public
async
Task
TestEqualsWithLogicalNot
()
{
await
TestInRegularAndScriptAsync
(
@"
class C
{
public int? f;
void M(C c)
{
int? x = [||]!(c == null) ? c.f : null;
}
}"
,
@"
class C
{
public int? f;
void M(C c)
{
int? x = c?.f;
}
}"
);
}
[
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)]
public
async
Task
TestNotEqualsWithLogicalNot
()
{
await
TestInRegularAndScriptAsync
(
@"
class C
{
public int? f;
void M(C c)
{
int? x = [||]!(c != null) ? null : c.f;
}
}"
,
@"
class C
{
public int? f;
void M(C c)
...
...
src/EditorFeatures/VisualBasicTest/UseNullPropagation/UseNullPropagationTests.vb
浏览文件 @
aa98f1a5
...
...
@@ -326,7 +326,7 @@ End Class")
<
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)
>
<
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)
>
Public
Async
Function
TestWithNullableTypeAndReferenceEquals
()
As
Task
Public
Async
Function
TestWithNullableTypeAndReferenceEquals
1
()
As
Task
Await
TestInRegularAndScriptAsync
(
"
Imports System
...
...
@@ -348,7 +348,7 @@ End Class")
<
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)
>
<
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)
>
Public
Async
Function
TestWithNullableTypeAndReferenceEquals
Reversed
()
As
Task
Public
Async
Function
TestWithNullableTypeAndReferenceEquals
2
()
As
Task
Await
TestInRegularAndScriptAsync
(
"
Imports System
...
...
@@ -370,7 +370,7 @@ End Class")
<
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)
>
<
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)
>
Public
Async
Function
TestWithNullableTypeAndReferenceEqualsWithObject
()
As
Task
Public
Async
Function
TestWithNullableTypeAndReferenceEqualsWithObject
1
()
As
Task
Await
TestInRegularAndScriptAsync
(
"
Imports System
...
...
@@ -392,7 +392,7 @@ End Class")
<
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)
>
<
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)
>
Public
Async
Function
TestWithNullableTypeAndReferenceEqualsWithObject
Reversed
()
As
Task
Public
Async
Function
TestWithNullableTypeAndReferenceEqualsWithObject
2
()
As
Task
Await
TestInRegularAndScriptAsync
(
"
Imports System
...
...
@@ -439,5 +439,116 @@ Class C
End Sub
End Class"
)
End
Function
<
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)
>
<
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)
>
Public
Async
Function
TestWithNullableTypeAndLogicalNotReferenceEquals1
()
As
Task
Await
TestInRegularAndScriptAsync
(
"
Imports System
Class C
Sub M(o As Object)
Dim v = [||]If (Not ReferenceEquals(o, Nothing), o.ToString(), Nothing)
End Sub
End Class"
,
"
Imports System
Class C
Sub M(o As Object)
Dim v = o?.ToString()
End Sub
End Class"
)
End
Function
<
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)
>
<
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)
>
Public
Async
Function
TestWithNullableTypeAndLogicalNotReferenceEquals2
()
As
Task
Await
TestInRegularAndScriptAsync
(
"
Imports System
Class C
Sub M(o As Object)
Dim v = [||]If (Not ReferenceEquals(Nothing, o), o.ToString(), Nothing)
End Sub
End Class"
,
"
Imports System
Class C
Sub M(o As Object)
Dim v = o?.ToString()
End Sub
End Class"
)
End
Function
<
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)
>
<
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)
>
Public
Async
Function
TestWithNullableTypeAndLogicalNotReferenceEqualsWithObject1
()
As
Task
Await
TestInRegularAndScriptAsync
(
"
Imports System
Class C
Sub M(o As Object)
Dim v = [||]If (Not Object.ReferenceEquals(o, Nothing), o.ToString(), Nothing)
End Sub
End Class"
,
"
Imports System
Class C
Sub M(o As Object)
Dim v = o?.ToString()
End Sub
End Class"
)
End
Function
<
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)
>
<
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)
>
Public
Async
Function
TestEqualsWithLogicalNot
()
As
Task
Await
TestInRegularAndScriptAsync
(
"
Imports System
Class C
Sub M(o As Object)
Dim v = [||]If (Not (o Is Nothing), o.ToString(), Nothing)
End Sub
End Class"
,
"
Imports System
Class C
Sub M(o As Object)
Dim v = o?.ToString()
End Sub
End Class"
)
End
Function
<
WorkItem
(
23043
,
"https://github.com/dotnet/roslyn/issues/23043"
)
>
<
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)
>
Public
Async
Function
TestNotEqualsWithLogicalNot
()
As
Task
Await
TestInRegularAndScriptAsync
(
"
Imports System
Class C
Sub M(o As Object)
Dim v = [||]If (Not (o IsNot Nothing), Nothing, o.ToString())
End Sub
End Class"
,
"
Imports System
Class C
Sub M(o As Object)
Dim v = o?.ToString()
End Sub
End Class"
)
End
Function
End
Class
End
Namespace
src/Features/Core/Portable/UseNullPropagation/AbstractUseNullPropagationDiagnosticAnalyzer.cs
浏览文件 @
aa98f1a5
...
...
@@ -100,6 +100,14 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context, INamedTypeSymbol e
conditionNode
=
syntaxFacts
.
WalkDownParentheses
(
conditionNode
);
var
conditionIsNegated
=
false
;
if
(
syntaxFacts
.
IsLogicalNotExpression
(
conditionNode
))
{
conditionIsNegated
=
true
;
conditionNode
=
syntaxFacts
.
WalkDownParentheses
(
syntaxFacts
.
GetOperandOfPrefixUnaryExpression
(
conditionNode
));
}
var
isEqualityLikeCondition
=
TryAnalyzeCondition
(
context
,
syntaxFacts
,
referenceEqualsMethodOpt
,
conditionNode
,
out
var
conditionPartToCheck
,
out
var
isEquals
);
...
...
@@ -108,6 +116,11 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context, INamedTypeSymbol e
return
;
}
if
(
conditionIsNegated
)
{
isEquals
=
!
isEquals
;
}
// Needs to be of the form:
// x == null ? null : ... or
// x != null ? ... : null;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录