Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
0408978b
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,发现更多精彩内容 >>
提交
0408978b
编写于
3月 08, 2017
作者:
C
CyrusNajmabadi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Don't off ?. if it's going to be used in an Expression-Tree.
上级
5961e610
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
48 addition
and
12 deletion
+48
-12
src/EditorFeatures/CSharpTest/UseNullPropagation/UseNullPropagationTests.cs
.../CSharpTest/UseNullPropagation/UseNullPropagationTests.cs
+22
-0
src/Features/Core/Portable/UseNullPropagation/AbstractUseNullPropagationDiagnosticAnalyzer.cs
...opagation/AbstractUseNullPropagationDiagnosticAnalyzer.cs
+26
-12
未找到文件。
src/EditorFeatures/CSharpTest/UseNullPropagation/UseNullPropagationTests.cs
浏览文件 @
0408978b
...
...
@@ -338,5 +338,27 @@ void Foo()
}
class C { public void M(string s) { } }"
);
}
[
WorkItem
(
17623
,
"https://github.com/dotnet/roslyn/issues/17623"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseNullPropagation
)]
public
async
Task
TestInExpressionTree
()
{
await
TestMissingInRegularAndScriptAsync
(
@"
using System;
using System.Linq.Expressions;
class Program
{
void Main(string s)
{
Method<string>(t => [||]s != null ? s.ToString() : null); // works
}
public void Method<T>(Expression<Func<T, string>> functor)
{
}
}"
);
}
}
}
\ No newline at end of file
src/Features/Core/Portable/UseNullPropagation/AbstractUseNullPropagationDiagnosticAnalyzer.cs
浏览文件 @
0408978b
...
...
@@ -37,15 +37,25 @@ protected AbstractUseNullPropagationDiagnosticAnalyzer()
public
override
DiagnosticAnalyzerCategory
GetAnalyzerCategory
()
=>
DiagnosticAnalyzerCategory
.
SemanticDocumentAnalysis
;
protected
abstract
TSyntaxKind
GetSyntaxKindToAnalyze
();
protected
abstract
ISyntaxFactsService
GetSyntaxFactsService
();
protected
abstract
bool
IsEquals
(
TBinaryExpressionSyntax
condition
);
protected
abstract
bool
IsNotEquals
(
TBinaryExpressionSyntax
condition
);
protected
abstract
bool
ShouldAnalyze
(
ParseOptions
options
);
protected
abstract
ISyntaxFactsService
GetSyntaxFactsService
();
protected
abstract
ISemanticFactsService
GetSemanticFactsService
();
protected
override
void
InitializeWorker
(
AnalysisContext
context
)
=>
context
.
RegisterSyntaxNodeAction
(
AnalyzeSyntax
,
GetSyntaxKindToAnalyze
());
{
context
.
RegisterCompilationStartAction
(
startContext
=>
{
var
expressionTypeOpt
=
startContext
.
Compilation
.
GetTypeByMetadataName
(
"System.Linq.Expressions.Expression`1"
);
startContext
.
RegisterSyntaxNodeAction
(
c
=>
AnalyzeSyntax
(
c
,
expressionTypeOpt
),
GetSyntaxKindToAnalyze
());
});
private
void
AnalyzeSyntax
(
SyntaxNodeAnalysisContext
context
)
}
private
void
AnalyzeSyntax
(
SyntaxNodeAnalysisContext
context
,
INamedTypeSymbol
expressionTypeOpt
)
{
var
conditionalExpression
=
(
TConditionalExpressionSyntax
)
context
.
Node
;
if
(!
ShouldAnalyze
(
conditionalExpression
.
SyntaxTree
.
Options
))
...
...
@@ -102,7 +112,7 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
return
;
}
// Needs to be of the form
e
:
// Needs to be of the form:
// x == null ? null : ... or
// x != null ? ... : null;
if
(
isEquals
&&
!
syntaxFacts
.
IsNullLiteralExpression
(
whenTrueNode
))
...
...
@@ -124,6 +134,14 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
return
;
}
// ?. is not available in expression-trees. Disallow the fix in that case.
var
semanticFacts
=
GetSemanticFactsService
();
var
semanticModel
=
context
.
SemanticModel
;
if
(
semanticFacts
.
IsInExpressionTree
(
semanticModel
,
conditionNode
,
expressionTypeOpt
,
cancellationToken
))
{
return
;
}
var
locations
=
ImmutableArray
.
Create
(
conditionalExpression
.
GetLocation
(),
conditionPartToCheck
.
GetLocation
(),
...
...
@@ -162,26 +180,22 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context)
private
static
SyntaxNode
Unwrap
(
ISyntaxFactsService
syntaxFacts
,
SyntaxNode
node
)
{
var
invocation
=
node
as
TInvocationExpression
;
if
(
invocation
!=
null
)
if
(
node
is
TInvocationExpression
invocation
)
{
return
syntaxFacts
.
GetExpressionOfInvocationExpression
(
invocation
);
}
var
memberAccess
=
node
as
TMemberAccessExpression
;
if
(
memberAccess
!=
null
)
if
(
node
is
TMemberAccessExpression
memberAccess
)
{
return
syntaxFacts
.
GetExpressionOfMemberAccessExpression
(
memberAccess
);
}
var
conditionalAccess
=
node
as
TConditionalAccessExpression
;
if
(
conditionalAccess
!=
null
)
if
(
node
is
TConditionalAccessExpression
conditionalAccess
)
{
return
syntaxFacts
.
GetExpressionOfConditionalAccessExpression
(
conditionalAccess
);
}
var
elementAccess
=
node
as
TElementAccessExpression
;
if
(
elementAccess
!=
null
)
if
(
node
is
TElementAccessExpression
elementAccess
)
{
return
syntaxFacts
.
GetExpressionOfElementAccessExpression
(
elementAccess
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录