Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
f3435440
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,发现更多精彩内容 >>
提交
f3435440
编写于
1月 06, 2017
作者:
C
CyrusNajmabadi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Do not offer 'use throw expression' in expression trees.
上级
c752659a
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
75 addition
and
32 deletion
+75
-32
src/EditorFeatures/CSharpTest/UseThrowExpression/UseThrowExpressionTests.cs
.../CSharpTest/UseThrowExpression/UseThrowExpressionTests.cs
+25
-0
src/Features/CSharp/Portable/InlineDeclaration/CSharpInlineDeclarationDiagnosticAnalyzer.cs
...eDeclaration/CSharpInlineDeclarationDiagnosticAnalyzer.cs
+1
-23
src/Features/CSharp/Portable/UseThrowExpression/CSharpUseThrowExpressionDiagnosticAnalyzer.cs
...wExpression/CSharpUseThrowExpressionDiagnosticAnalyzer.cs
+5
-1
src/Features/Core/Portable/UseThrowExpression/AbstractUseThrowExpressionDiagnosticAnalyzer.cs
...xpression/AbstractUseThrowExpressionDiagnosticAnalyzer.cs
+22
-8
src/Workspaces/CSharp/Portable/Extensions/SyntaxNodeExtensions.cs
...spaces/CSharp/Portable/Extensions/SyntaxNodeExtensions.cs
+22
-0
未找到文件。
src/EditorFeatures/CSharpTest/UseThrowExpression/UseThrowExpressionTests.cs
浏览文件 @
f3435440
...
...
@@ -304,6 +304,31 @@ void M(string s)
if (s == null)
[|throw|] new ArgumentNullException(nameof(s));
}
}"
);
}
[
WorkItem
(
16234
,
"https://github.com/dotnet/roslyn/issues/16234"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseThrowExpression
)]
public
async
Task
TestNotInExpressionTree
()
{
await
TestMissingAsync
(
@"using System;
using System.Linq.Expressions;
class C
{
private string _s;
void Foo()
{
Expression<Action<string>> e = s =>
{
if (s == null)
[|throw|] new ArgumentNullException(nameof(s));
_s = s;
};
}
}"
);
}
}
...
...
src/Features/CSharp/Portable/InlineDeclaration/CSharpInlineDeclarationDiagnosticAnalyzer.cs
浏览文件 @
f3435440
...
...
@@ -175,7 +175,7 @@ private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context, INamedTypeSymb
return
;
}
if
(
IsInExpressionTree
(
semanticModel
,
argumentExpression
,
expressionTypeOpt
,
cancellationToken
))
if
(
argumentExpression
.
IsInExpressionTree
(
semanticModel
,
expressionTypeOpt
,
cancellationToken
))
{
// out-vars are not allowed inside expression-trees. So don't offer to
// fix if we're inside one.
...
...
@@ -234,28 +234,6 @@ private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context, INamedTypeSymb
additionalLocations
:
allLocations
));
}
private
bool
IsInExpressionTree
(
SemanticModel
semanticModel
,
SyntaxNode
argumentExpression
,
INamedTypeSymbol
expressionTypeOpt
,
CancellationToken
cancellationToken
)
{
if
(
expressionTypeOpt
!=
null
)
{
for
(
var
current
=
argumentExpression
;
current
!=
null
;
current
=
current
.
Parent
)
{
if
(
current
.
IsAnyLambda
())
{
var
typeInfo
=
semanticModel
.
GetTypeInfo
(
current
,
cancellationToken
);
if
(
expressionTypeOpt
.
Equals
(
typeInfo
.
ConvertedType
?.
OriginalDefinition
))
{
return
true
;
}
}
}
}
return
false
;
}
private
bool
WouldCauseDefiniteAssignmentErrors
(
SemanticModel
semanticModel
,
VariableDeclaratorSyntax
localDeclarator
,
BlockSyntax
enclosingBlock
,
ISymbol
outSymbol
,
CancellationToken
cancellationToken
)
...
...
src/Features/CSharp/Portable/UseThrowExpression/CSharpUseThrowExpressionDiagnosticAnalyzer.cs
浏览文件 @
f3435440
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using
System
;
using
System.Threading
;
using
Microsoft.CodeAnalysis.CSharp.Extensions
;
using
Microsoft.CodeAnalysis.Diagnostics
;
using
Microsoft.CodeAnalysis.LanguageServices
;
using
Microsoft.CodeAnalysis.UseThrowExpression
;
...
...
@@ -18,5 +19,8 @@ protected override bool IsSupported(ParseOptions options)
protected
override
ISyntaxFactsService
GetSyntaxFactsService
()
=>
CSharpSyntaxFactsService
.
Instance
;
protected
override
bool
IsInExpressionTree
(
SyntaxNode
node
,
SemanticModel
semanticModel
,
INamedTypeSymbol
expressionTypeOpt
,
CancellationToken
cancellationToken
)
=>
node
.
IsInExpressionTree
(
semanticModel
,
expressionTypeOpt
,
cancellationToken
);
}
}
\ No newline at end of file
src/Features/Core/Portable/UseThrowExpression/AbstractUseThrowExpressionDiagnosticAnalyzer.cs
浏览文件 @
f3435440
...
...
@@ -4,7 +4,6 @@
using
System.Collections.Immutable
;
using
System.Reflection
;
using
System.Threading
;
using
Microsoft.CodeAnalysis.CodeActions
;
using
Microsoft.CodeAnalysis.CodeStyle
;
using
Microsoft.CodeAnalysis.Diagnostics
;
using
Microsoft.CodeAnalysis.LanguageServices
;
...
...
@@ -48,7 +47,7 @@ public DiagnosticAnalyzerCategory GetAnalyzerCategory()
public
bool
OpenFileOnly
(
Workspace
workspace
)
=>
false
;
private
static
MethodInfo
s_registerOperationActionInfo
=
typeof
(
AnalysisContext
).
GetTypeInfo
().
GetDeclaredMethod
(
"RegisterOperationActionImmutableArrayInternal"
);
typeof
(
CompilationStart
AnalysisContext
).
GetTypeInfo
().
GetDeclaredMethod
(
"RegisterOperationActionImmutableArrayInternal"
);
private
static
MethodInfo
s_getOperationInfo
=
typeof
(
SemanticModel
).
GetTypeInfo
().
GetDeclaredMethod
(
"GetOperationInternal"
);
...
...
@@ -56,13 +55,19 @@ public DiagnosticAnalyzerCategory GetAnalyzerCategory()
protected
abstract
bool
IsSupported
(
ParseOptions
options
);
protected
override
void
InitializeWorker
(
AnalysisContext
context
)
=>
s_registerOperationActionInfo
.
Invoke
(
context
,
new
object
[]
{
new
Action
<
OperationAnalysisContext
>(
AnalyzeOperation
),
ImmutableArray
.
Create
(
OperationKind
.
ThrowStatement
)
});
{
context
.
RegisterCompilationStartAction
(
startContext
=>
{
var
expressionTypeOpt
=
startContext
.
Compilation
.
GetTypeByMetadataName
(
"System.Linq.Expressions.Expression`1"
);
s_registerOperationActionInfo
.
Invoke
(
startContext
,
new
object
[]
{
new
Action
<
OperationAnalysisContext
>(
operationContext
=>
AnalyzeOperation
(
operationContext
,
expressionTypeOpt
)),
ImmutableArray
.
Create
(
OperationKind
.
ThrowStatement
)
});
});
}
private
void
AnalyzeOperation
(
OperationAnalysisContext
context
)
private
void
AnalyzeOperation
(
OperationAnalysisContext
context
,
INamedTypeSymbol
expressionTypeOpt
)
{
var
syntaxTree
=
context
.
Operation
.
Syntax
.
SyntaxTree
;
if
(!
IsSupported
(
syntaxTree
.
Options
))
...
...
@@ -90,6 +95,11 @@ private void AnalyzeOperation(OperationAnalysisContext context)
var
compilation
=
context
.
Compilation
;
var
semanticModel
=
compilation
.
GetSemanticModel
(
throwStatement
.
SyntaxTree
);
if
(
IsInExpressionTree
(
throwStatement
,
semanticModel
,
expressionTypeOpt
,
cancellationToken
))
{
return
;
}
var
ifOperation
=
GetContainingIfOperation
(
semanticModel
,
throwOperation
,
cancellationToken
);
...
...
@@ -173,6 +183,10 @@ private void AnalyzeOperation(OperationAnalysisContext context)
}
}
protected
abstract
bool
IsInExpressionTree
(
SyntaxNode
node
,
SemanticModel
semanticModel
,
INamedTypeSymbol
expressionTypeOpt
,
CancellationToken
cancellationToken
);
protected
abstract
ISyntaxFactsService
GetSyntaxFactsService
();
private
bool
TryFindAssignmentExpression
(
...
...
src/Workspaces/CSharp/Portable/Extensions/SyntaxNodeExtensions.cs
浏览文件 @
f3435440
...
...
@@ -1120,5 +1120,27 @@ public static ConditionalAccessExpressionSyntax GetInnerMostConditionalAccessExp
return
result
;
}
public
static
bool
IsInExpressionTree
(
this
SyntaxNode
node
,
SemanticModel
semanticModel
,
INamedTypeSymbol
expressionTypeOpt
,
CancellationToken
cancellationToken
)
{
if
(
expressionTypeOpt
!=
null
)
{
for
(
var
current
=
node
;
current
!=
null
;
current
=
current
.
Parent
)
{
if
(
current
.
IsAnyLambda
())
{
var
typeInfo
=
semanticModel
.
GetTypeInfo
(
current
,
cancellationToken
);
if
(
expressionTypeOpt
.
Equals
(
typeInfo
.
ConvertedType
?.
OriginalDefinition
))
{
return
true
;
}
}
}
}
return
false
;
}
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录