Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
004377d8
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,发现更多精彩内容 >>
提交
004377d8
编写于
1月 10, 2020
作者:
M
Manish Vasani
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use appropriate code fix title for IDE0059 for unnecessary compound assigment
Fixes #38507
上级
e94cd673
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
76 addition
and
5 deletion
+76
-5
src/EditorFeatures/CSharpTest/RemoveUnusedParametersAndValues/RemoveUnusedValueAssignmentTests.cs
...edParametersAndValues/RemoveUnusedValueAssignmentTests.cs
+54
-0
src/Features/Core/Portable/RemoveUnusedParametersAndValues/AbstractRemoveUnusedValuesCodeFixProvider.cs
...ersAndValues/AbstractRemoveUnusedValuesCodeFixProvider.cs
+22
-5
未找到文件。
src/EditorFeatures/CSharpTest/RemoveUnusedParametersAndValues/RemoveUnusedValueAssignmentTests.cs
浏览文件 @
004377d8
...
...
@@ -5,6 +5,7 @@
using
Microsoft.CodeAnalysis.CodeStyle
;
using
Microsoft.CodeAnalysis.CSharp
;
using
Microsoft.CodeAnalysis.CSharp.CodeStyle
;
using
Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
;
using
Microsoft.CodeAnalysis.Options
;
using
Microsoft.CodeAnalysis.Test.Utilities
;
using
Roslyn.Test.Utilities
;
...
...
@@ -7487,5 +7488,58 @@ void M(int i)
}
}"
,
options
:
PreferDiscard
);
}
[
WorkItem
(
38507
,
"https://github.com/dotnet/roslyn/issues/38507"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsRemoveUnusedValues
)]
public
async
Task
TestCodeFixTitleForBlockBodyRedundantCompoundAssignmentReturn
()
{
var
source
=
@"
<Workspace>
<Project Language=""C#"" CommonReferences=""true"">
<Document>
class C
{
C M(C x)
{
return [|x ??= M2()|];
}
C M2() => new C();
}
</Document>
</Project>
</Workspace>
"
;
using
var
testWorkspace
=
TestWorkspace
.
Create
(
source
);
var
(
_
,
action
)
=
await
GetCodeActionsAsync
(
testWorkspace
,
parameters
:
default
);
Assert
.
Equal
(
FeaturesResources
.
Remove_redundant_assignment
,
action
.
Title
);
}
[
WorkItem
(
38507
,
"https://github.com/dotnet/roslyn/issues/38507"
)]
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsRemoveUnusedValues
)]
public
async
Task
TestCodeFixTitleForExpressionBodyRedundantCompoundAssignmentReturn
()
{
var
source
=
@"
<Workspace>
<Project Language=""C#"" CommonReferences=""true"">
<Document>
class C
{
C M(C x) => [|x ??= M2()|];
C M2() => new C();
}
</Document>
</Project>
</Workspace>
"
;
using
var
testWorkspace
=
TestWorkspace
.
Create
(
source
);
var
(
_
,
action
)
=
await
GetCodeActionsAsync
(
testWorkspace
,
parameters
:
default
);
Assert
.
Equal
(
FeaturesResources
.
Remove_redundant_assignment
,
action
.
Title
);
}
}
}
src/Features/Core/Portable/RemoveUnusedParametersAndValues/AbstractRemoveUnusedValuesCodeFixProvider.cs
浏览文件 @
004377d8
...
...
@@ -100,12 +100,12 @@ internal abstract class AbstractRemoveUnusedValuesCodeFixProvider<TExpressionSyn
SyntaxEditor
editor
,
ISyntaxFactsService
syntaxFacts
);
public
sealed
override
Task
RegisterCodeFixesAsync
(
CodeFixContext
context
)
public
sealed
override
async
Task
RegisterCodeFixesAsync
(
CodeFixContext
context
)
{
var
diagnostic
=
context
.
Diagnostics
[
0
];
if
(!
AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer
.
TryGetUnusedValuePreference
(
diagnostic
,
out
var
preference
))
{
return
Task
.
CompletedTask
;
return
;
}
var
isRemovableAssignment
=
AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer
.
GetIsRemovableAssignmentDiagnostic
(
diagnostic
);
...
...
@@ -126,10 +126,27 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
{
// Do not offer a fix to replace unused foreach iteration variable with discard.
// User should probably replace it with a for loop based on the collection length.
return
Task
.
CompletedTask
;
return
;
}
title
=
FeaturesResources
.
Use_discard_underscore
;
// Check if this is compound assignment which is not parented by an expression statement,
// for example "return x += M();" OR "=> x ??= new C();"
// If so, we will be replacing this compound assignment with the underlying binary operation.
// For the above examples, it will be "return x + M();" AND "=> x ?? new C();" respectively.
// For these cases, we want to show the title as "Remove redundant assignment" instead of "Use discard _".
var
syntaxFacts
=
context
.
Document
.
GetLanguageService
<
ISyntaxFactsService
>();
var
root
=
await
context
.
Document
.
GetSyntaxRootAsync
(
context
.
CancellationToken
).
ConfigureAwait
(
false
);
var
node
=
root
.
FindNode
(
context
.
Span
,
getInnermostNodeForTie
:
true
);
if
(
syntaxFacts
.
IsLeftSideOfAnyAssignment
(
node
)
&&
!
syntaxFacts
.
IsLeftSideOfAssignment
(
node
)
&&
!
syntaxFacts
.
IsExpressionStatement
(
node
.
Parent
))
{
title
=
FeaturesResources
.
Remove_redundant_assignment
;
}
break
;
case
UnusedValuePreference
.
UnusedLocalVariable
:
...
...
@@ -137,7 +154,7 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
break
;
default
:
return
Task
.
CompletedTask
;
return
;
}
}
...
...
@@ -148,7 +165,7 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
equivalenceKey
:
GetEquivalenceKey
(
preference
,
isRemovableAssignment
)),
diagnostic
);
return
Task
.
CompletedTask
;
return
;
}
private
static
bool
IsForEachIterationVariableDiagnostic
(
Diagnostic
diagnostic
,
Document
document
,
CancellationToken
cancellationToken
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录