Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
aa061313
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,体验更适合开发者的 AI 搜索 >>
提交
aa061313
编写于
8月 13, 2020
作者:
C
Cyrus Najmabadi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use C# 'not' pattern when available.
上级
5562a230
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
43 addition
and
18 deletion
+43
-18
src/Analyzers/CSharp/CodeFixes/UsePatternMatching/CSharpAsAndNullCheckCodeFixProvider.cs
...UsePatternMatching/CSharpAsAndNullCheckCodeFixProvider.cs
+34
-12
src/Analyzers/CSharp/Tests/UsePatternMatching/CSharpAsAndNullCheckTests.cs
...arp/Tests/UsePatternMatching/CSharpAsAndNullCheckTests.cs
+9
-6
未找到文件。
src/Analyzers/CSharp/CodeFixes/UsePatternMatching/CSharpAsAndNullCheckCodeFixProvider.cs
浏览文件 @
aa061313
...
...
@@ -43,20 +43,23 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
return
Task
.
CompletedTask
;
}
protected
override
Task
FixAllAsync
(
protected
override
async
Task
FixAllAsync
(
Document
document
,
ImmutableArray
<
Diagnostic
>
diagnostics
,
SyntaxEditor
editor
,
CancellationToken
cancellationToken
)
{
using
var
_1
=
PooledHashSet
<
Location
>.
GetInstance
(
out
var
declaratorLocations
);
using
var
_2
=
PooledHashSet
<
SyntaxNode
>.
GetInstance
(
out
var
statementParentScopes
);
var
tree
=
await
document
.
GetRequiredSyntaxTreeAsync
(
cancellationToken
).
ConfigureAwait
(
false
);
var
languageVersion
=
((
CSharpParseOptions
)
tree
.
Options
).
LanguageVersion
;
foreach
(
var
diagnostic
in
diagnostics
)
{
cancellationToken
.
ThrowIfCancellationRequested
();
if
(
declaratorLocations
.
Add
(
diagnostic
.
AdditionalLocations
[
0
]))
{
AddEdits
(
editor
,
diagnostic
,
RemoveStatement
,
cancellationToken
);
AddEdits
(
editor
,
diagnostic
,
languageVersion
,
RemoveStatement
,
cancellationToken
);
}
}
...
...
@@ -71,7 +74,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
});
}
return
Task
.
CompletedTask
;
return
;
void
RemoveStatement
(
StatementSyntax
statement
)
{
...
...
@@ -86,6 +89,7 @@ void RemoveStatement(StatementSyntax statement)
private
static
void
AddEdits
(
SyntaxEditor
editor
,
Diagnostic
diagnostic
,
LanguageVersion
languageVersion
,
Action
<
StatementSyntax
>
removeStatement
,
CancellationToken
cancellationToken
)
{
...
...
@@ -106,15 +110,8 @@ void RemoveStatement(StatementSyntax statement)
var
declarationPattern
=
SyntaxFactory
.
DeclarationPattern
(
((
TypeSyntax
)
asExpression
.
Right
).
WithoutTrivia
().
WithTrailingTrivia
(
SyntaxFactory
.
ElasticMarker
),
SyntaxFactory
.
SingleVariableDesignation
(
newIdentifier
));
ExpressionSyntax
isExpression
=
SyntaxFactory
.
IsPatternExpression
(
asExpression
.
Left
,
declarationPattern
);
// We should negate the is-expression if we have something like "x == null" or "x is null"
if
(
comparison
.
IsKind
(
SyntaxKind
.
EqualsExpression
,
SyntaxKind
.
IsPatternExpression
))
{
isExpression
=
SyntaxFactory
.
PrefixUnaryExpression
(
SyntaxKind
.
LogicalNotExpression
,
isExpression
.
Parenthesize
());
}
var
condition
=
GetCondition
(
languageVersion
,
comparison
,
asExpression
,
declarationPattern
);
if
(
declarator
.
Parent
is
VariableDeclarationSyntax
declaration
&&
declaration
.
Parent
is
LocalDeclarationStatementSyntax
localDeclaration
&&
...
...
@@ -134,7 +131,32 @@ void RemoveStatement(StatementSyntax statement)
editor
.
RemoveNode
(
declarator
,
SyntaxRemoveOptions
.
KeepUnbalancedDirectives
);
}
editor
.
ReplaceNode
(
comparison
,
isExpression
.
WithTriviaFrom
(
comparison
));
editor
.
ReplaceNode
(
comparison
,
condition
.
WithTriviaFrom
(
comparison
));
}
private
static
ExpressionSyntax
GetCondition
(
LanguageVersion
languageVersion
,
ExpressionSyntax
comparison
,
BinaryExpressionSyntax
asExpression
,
DeclarationPatternSyntax
declarationPattern
)
{
var
isPatternExpression
=
SyntaxFactory
.
IsPatternExpression
(
asExpression
.
Left
,
declarationPattern
);
// We should negate the is-expression if we have something like "x == null" or "x is null"
if
(!
comparison
.
IsKind
(
SyntaxKind
.
EqualsExpression
,
SyntaxKind
.
IsPatternExpression
))
return
isPatternExpression
;
#if !CODE_STYLE
if
(
languageVersion
>=
LanguageVersion
.
CSharp9
)
{
// In C# 9 and higher, convert to `x is not string s`.
return
isPatternExpression
.
WithPattern
(
SyntaxFactory
.
UnaryPattern
(
SyntaxFactory
.
Token
(
SyntaxKind
.
NotKeyword
),
isPatternExpression
.
Pattern
));
}
#endif
// In C# 8 and lower, convert to `!(x is string s)`
return
SyntaxFactory
.
PrefixUnaryExpression
(
SyntaxKind
.
LogicalNotExpression
,
isPatternExpression
.
Parenthesize
());
}
private
class
MyCodeAction
:
CustomCodeActions
.
DocumentChangeAction
...
...
src/Analyzers/CSharp/Tests/UsePatternMatching/CSharpAsAndNullCheckTests.cs
浏览文件 @
aa061313
...
...
@@ -36,11 +36,14 @@ internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProvider
[
InlineData
(
"(x = o as string) == null"
,
"!(o is string x)"
)]
[
InlineData
(
"null == (x = o as string)"
,
"!(o is string x)"
)]
[
InlineData
(
"(x = o as string) is null"
,
"!(o is string x)"
)]
public
async
Task
InlineTypeCheck1
(
string
input
,
string
output
)
#if !CODE_STYLE
[
InlineData
(
"x == null"
,
"o is not string x"
,
LanguageVersion
.
CSharp9
)]
#endif
public
async
Task
InlineTypeCheck1
(
string
input
,
string
output
,
LanguageVersion
version
=
LanguageVersion
.
CSharp8
)
{
await
TestStatement
(
$"if (
{
input
}
)
{{
}}
"
,
$"if (
{
output
}
)
{{
}}
"
);
await
TestStatement
(
$"var y =
{
input
}
;"
,
$"var y =
{
output
}
;"
);
await
TestStatement
(
$"return
{
input
}
;"
,
$"return
{
output
}
;"
);
await
TestStatement
(
$"if (
{
input
}
)
{{
}}
"
,
$"if (
{
output
}
)
{{
}}
"
,
version
);
await
TestStatement
(
$"var y =
{
input
}
;"
,
$"var y =
{
output
}
;"
,
version
);
await
TestStatement
(
$"return
{
input
}
;"
,
$"return
{
output
}
;"
,
version
);
}
[
Theory
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsInlineTypeCheck
)]
...
...
@@ -52,7 +55,7 @@ public async Task InlineTypeCheck1(string input, string output)
public
async
Task
InlineTypeCheck2
(
string
input
,
string
output
)
=>
await
TestStatement
(
$"while (
{
input
}
)
{{
}}
"
,
$"while (
{
output
}
)
{{
}}
"
);
private
async
Task
TestStatement
(
string
input
,
string
output
)
private
async
Task
TestStatement
(
string
input
,
string
output
,
LanguageVersion
version
=
LanguageVersion
.
CSharp8
)
{
await
TestInRegularAndScript1Async
(
$@"class C
...
...
@@ -69,7 +72,7 @@ void M(object o)
{{
{
output
}
}}
}}
"
);
}}
"
,
new
TestParameters
(
CSharpParseOptions
.
Default
.
WithLanguageVersion
(
version
))
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsInlineTypeCheck
)]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录