Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
eefdbeb3
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,发现更多精彩内容 >>
提交
eefdbeb3
编写于
6月 26, 2017
作者:
C
CyrusNajmabadi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support removing unused exception variables.
上级
2b57b223
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
74 addition
and
21 deletion
+74
-21
src/EditorFeatures/CSharpTest/RemoveUnusedVariable/RemoveUnusedVariableTest.cs
...harpTest/RemoveUnusedVariable/RemoveUnusedVariableTest.cs
+31
-0
src/Features/CSharp/Portable/RemoveUnusedVariable/CSharpRemoveUnusedVariableCodeFixProvider.cs
...usedVariable/CSharpRemoveUnusedVariableCodeFixProvider.cs
+4
-1
src/Features/Core/Portable/RemoveUnusedVariable/AbstractRemoveUnusedVariableCodeFixProvider.cs
...edVariable/AbstractRemoveUnusedVariableCodeFixProvider.cs
+32
-17
src/Features/VisualBasic/Portable/RemoveUnusedVariable/VisualBasicRemoveUnusedVariableCodeFixProvider.vb
...ariable/VisualBasicRemoveUnusedVariableCodeFixProvider.vb
+7
-3
未找到文件。
src/EditorFeatures/CSharpTest/RemoveUnusedVariable/RemoveUnusedVariableTest.cs
浏览文件 @
eefdbeb3
...
...
@@ -172,6 +172,37 @@ void Method()
void Method()
{
}
}"
);
}
[
WorkItem
(
20466
,
"https://github.com/dotnet/roslyn/issues/20466"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsRemoveUnusedVariable
)]
public
async
Task
RemoveUnusedCatchVariable
()
{
await
TestInRegularAndScriptAsync
(
@"class Class
{
void Method()
{
try
{
}
catch (System.Exception [|e|])
{
}
}
}"
,
@"class Class
{
void Method()
{
try
{
}
catch (System.Exception)
{
}
}
}"
);
}
}
...
...
src/Features/CSharp/Portable/RemoveUnusedVariable/CSharpRemoveUnusedVariableCodeFixProvider.cs
浏览文件 @
eefdbeb3
...
...
@@ -17,5 +17,8 @@ internal partial class CSharpRemoveUnusedVariableCodeFixProvider : AbstractRemov
public
sealed
override
ImmutableArray
<
string
>
FixableDiagnosticIds
=>
ImmutableArray
.
Create
(
CS0168
,
CS0219
);
protected
override
bool
IsCatchDeclarationIdentifier
(
SyntaxToken
token
)
=>
token
.
Parent
is
CatchDeclarationSyntax
catchDeclaration
&&
catchDeclaration
.
Identifier
==
token
;
}
}
\ No newline at end of file
src/Features/Core/Portable/RemoveUnusedVariable/AbstractRemoveUnusedVariableCodeFixProvider.cs
浏览文件 @
eefdbeb3
...
...
@@ -8,22 +8,27 @@
using
Microsoft.CodeAnalysis.CodeActions
;
using
Microsoft.CodeAnalysis.CodeFixes
;
using
Microsoft.CodeAnalysis.Editing
;
using
Microsoft.CodeAnalysis.Formatting
;
using
Microsoft.CodeAnalysis.Shared.Extensions
;
using
Roslyn.Utilities
;
namespace
Microsoft.CodeAnalysis.RemoveUnusedVariable
{
internal
abstract
class
AbstractRemoveUnusedVariableCodeFixProvider
<
TLocalDeclarationStatement
,
TVariableDeclarator
,
TVariableDeclaration
>
:
SyntaxEditorBasedCodeFixProvider
where
TLocalDeclarationStatement
:
SyntaxNode
where
TVariableDeclarator
:
SyntaxNode
where
TVariableDeclaration
:
SyntaxNode
where
TLocalDeclarationStatement
:
SyntaxNode
where
TVariableDeclarator
:
SyntaxNode
where
TVariableDeclaration
:
SyntaxNode
{
protected
abstract
bool
IsCatchDeclarationIdentifier
(
SyntaxToken
token
);
public
async
override
Task
RegisterCodeFixesAsync
(
CodeFixContext
context
)
{
foreach
(
var
diagnostic
in
context
.
Diagnostics
)
{
var
root
=
await
context
.
Document
.
GetSyntaxRootAsync
(
context
.
CancellationToken
).
ConfigureAwait
(
false
);
var
token
=
root
.
FindToken
(
diagnostic
.
Location
.
SourceSpan
.
Start
);
if
(!
IsCatchDeclarationIdentifier
(
token
))
{
var
ancestor
=
token
.
GetAncestor
<
TLocalDeclarationStatement
>();
if
(
ancestor
==
null
)
...
...
@@ -31,6 +36,7 @@ public async override Task RegisterCodeFixesAsync(CodeFixContext context)
return
;
}
}
}
context
.
RegisterCodeFix
(
new
MyCodeAction
(
c
=>
FixAsync
(
context
.
Document
,
context
.
Diagnostics
.
First
(),
c
)),
...
...
@@ -42,7 +48,15 @@ protected override Task FixAllAsync(Document document, ImmutableArray<Diagnostic
var
root
=
editor
.
OriginalRoot
;
foreach
(
var
diagnostic
in
diagnostics
)
{
var
token
=
root
.
FindToken
(
diagnostic
.
Location
.
SourceSpan
.
Start
);
var
token
=
diagnostic
.
Location
.
FindToken
(
cancellationToken
);
if
(
IsCatchDeclarationIdentifier
(
token
))
{
editor
.
ReplaceNode
(
token
.
Parent
,
token
.
Parent
.
ReplaceToken
(
token
,
default
(
SyntaxToken
)).
WithAdditionalAnnotations
(
Formatter
.
Annotation
));
}
else
{
var
variableDeclarator
=
token
.
GetAncestor
<
TVariableDeclarator
>();
var
variableDeclarators
=
token
.
GetAncestor
<
TVariableDeclaration
>().
ChildNodes
().
Where
(
x
=>
x
is
TVariableDeclarator
);
...
...
@@ -56,6 +70,7 @@ protected override Task FixAllAsync(Document document, ImmutableArray<Diagnostic
editor
.
RemoveNode
(
variableDeclarator
);
}
}
}
return
SpecializedTasks
.
EmptyTask
;
}
...
...
src/Features/VisualBasic/Portable/RemoveUnusedVariable/VisualBasicRemoveUnusedVariableCodeFixProvider.vb
浏览文件 @
eefdbeb3
...
...
@@ -7,16 +7,20 @@ Imports Microsoft.CodeAnalysis.RemoveUnusedVariable
Imports
Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace
Microsoft.CodeAnalysis.VisualBasic.RemoveUnusedVariable
<
ExportCodeFixProviderAttribute
(
LanguageNames
.
VisualBasic
,
Name
:
=
PredefinedCodeFixProviderNames
.
RemoveUnusedVariable
),
[
Shared
]
>
<
ExportCodeFixProvider
(
LanguageNames
.
VisualBasic
,
Name
:
=
PredefinedCodeFixProviderNames
.
RemoveUnusedVariable
),
[
Shared
]
>
<
ExtensionOrder
(
After
:
=
PredefinedCodeFixProviderNames
.
AddImport
)
>
Friend
Class
VisualBasicRemoveUnusedVariableCodeFixProvider
Inherits
AbstractRemoveUnusedVariableCodeFixProvider
(
Of
LocalDeclarationStatementSyntax
,
ModifiedIdentifierSyntax
,
VariableDeclaratorSyntax
)
Private
Const
BC42024
As
String
=
"BC42024"
Private
Const
BC42024
As
String
=
NameOf
(
BC42024
)
Public
Overrides
ReadOnly
Property
FixableDiagnosticIds
As
ImmutableArray
(
Of
String
)
=
ImmutableArray
.
Create
(
BC42024
)
Protected
Overrides
Function
IsCatchDeclarationIdentifier
(
token
As
SyntaxToken
)
As
Boolean
' VB does not support catch declarations without an identifier in them
Return
False
End
Function
End
Class
End
Namespace
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录