Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
7b0aaff7
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,发现更多精彩内容 >>
提交
7b0aaff7
编写于
5月 26, 2020
作者:
A
Allison Chou
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implicit var switch expression fix
上级
725e3fc4
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
68 addition
and
5 deletion
+68
-5
src/Analyzers/CSharp/Tests/UseImplicitOrExplicitType/UseImplicitTypeTests.cs
...p/Tests/UseImplicitOrExplicitType/UseImplicitTypeTests.cs
+30
-0
src/Features/CSharp/Portable/ChangeSignature/CSharpChangeSignatureService.cs
.../Portable/ChangeSignature/CSharpChangeSignatureService.cs
+0
-3
src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Utilities/TypeStyle/CSharpUseImplicitTypeHelper.cs
...CSharp/Utilities/TypeStyle/CSharpUseImplicitTypeHelper.cs
+38
-2
未找到文件。
src/Analyzers/CSharp/Tests/UseImplicitOrExplicitType/UseImplicitTypeTests.cs
浏览文件 @
7b0aaff7
...
@@ -2735,5 +2735,35 @@ static void Main(string[] args)
...
@@ -2735,5 +2735,35 @@ static void Main(string[] args)
}"
,
}"
,
options
:
ImplicitTypeEverywhere
());
options
:
ImplicitTypeEverywhere
());
}
}
[
WpfFact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsUseExplicitType
)]
[
WorkItem
(
44507
,
"https://github.com/dotnet/roslyn/issues/44507"
)]
public
async
Task
DoNotSuggestVarInAmbiguousSwitchExpression
()
{
await
TestMissingAsync
(
@"using System;
class C
{
void M()
{
var i = 1;
[||]C x = i switch
{
0 => new A(),
1 => new B(),
_ => throw new ArgumentException(),
};
}
}
class A : C
{
}
class B : C
{
}"
,
parameters
:
new
TestParameters
(
options
:
ImplicitTypeEverywhere
()));
}
}
}
}
}
src/Features/CSharp/Portable/ChangeSignature/CSharpChangeSignatureService.cs
浏览文件 @
7b0aaff7
...
@@ -560,10 +560,7 @@ private static bool IsParamsArrayExpanded(SemanticModel semanticModel, SyntaxNod
...
@@ -560,10 +560,7 @@ private static bool IsParamsArrayExpanded(SemanticModel semanticModel, SyntaxNod
}
}
else
else
{
{
#pragma warning disable IDE0007 // Use implicit type - Using 'var' causes "error CS8506: No best type was found for the switch expression"
// TODO: File a bug on IDE0007 analyzer
BaseArgumentListSyntax
?
argumentList
=
node
switch
BaseArgumentListSyntax
?
argumentList
=
node
switch
#pragma warning restore IDE0007 // Use implicit type
{
{
InvocationExpressionSyntax
invocation
=>
invocation
.
ArgumentList
,
InvocationExpressionSyntax
invocation
=>
invocation
.
ArgumentList
,
ObjectCreationExpressionSyntax
objectCreation
=>
objectCreation
.
ArgumentList
,
ObjectCreationExpressionSyntax
objectCreation
=>
objectCreation
.
ArgumentList
,
...
...
src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Utilities/TypeStyle/CSharpUseImplicitTypeHelper.cs
浏览文件 @
7b0aaff7
...
@@ -15,6 +15,7 @@
...
@@ -15,6 +15,7 @@
using
Microsoft.CodeAnalysis.Shared.Utilities
;
using
Microsoft.CodeAnalysis.Shared.Utilities
;
using
Microsoft.CodeAnalysis.Simplification
;
using
Microsoft.CodeAnalysis.Simplification
;
using
Microsoft.CodeAnalysis.Operations
;
using
Microsoft.CodeAnalysis.Operations
;
using
Microsoft.CodeAnalysis.PooledObjects
;
#if CODE_STYLE
#if CODE_STYLE
using
OptionSet
=
Microsoft
.
CodeAnalysis
.
Diagnostics
.
AnalyzerConfigOptions
;
using
OptionSet
=
Microsoft
.
CodeAnalysis
.
Diagnostics
.
AnalyzerConfigOptions
;
...
@@ -362,13 +363,15 @@ private static bool IsSwitchExpressionAndCannotUseVar(TypeSyntax typeName, Expre
...
@@ -362,13 +363,15 @@ private static bool IsSwitchExpressionAndCannotUseVar(TypeSyntax typeName, Expre
if
(
initializer
.
IsKind
(
SyntaxKind
.
SwitchExpression
))
if
(
initializer
.
IsKind
(
SyntaxKind
.
SwitchExpression
))
{
{
// We compare the variable declaration type to each arm's type to see if there is an exact match, or if the
// We compare the variable declaration type to each arm's type to see if there is an exact match, or if the
// arm type inherits from the variable declaration type. If not, we must use the explicit type instead of var.
// arm type inherits from the variable declaration type. We also must verify that the arm types are all
// in the same line of inheritance. If not, we must use the explicit type instead of var.
// Even if 'true' is returned from this method, it is not guaranteed that we can use var. Further checks should occur
// Even if 'true' is returned from this method, it is not guaranteed that we can use var. Further checks should occur
// after this method is called, such as checking if multiple implicit coversions exist.
// after this method is called, such as checking if multiple implicit coversions exist.
var
declarationType
=
semanticModel
.
GetTypeInfo
(
typeName
).
Type
;
var
declarationType
=
semanticModel
.
GetTypeInfo
(
typeName
).
Type
;
var
noValidTypeExpressions
=
true
;
var
noValidTypeExpressions
=
true
;
if
(
declarationType
!=
null
)
if
(
declarationType
!=
null
)
{
{
using
var
_
=
ArrayBuilder
<
ITypeSymbol
>.
GetInstance
(
out
var
seenTypes
);
foreach
(
var
arm
in
((
SwitchExpressionSyntax
)
initializer
).
Arms
)
foreach
(
var
arm
in
((
SwitchExpressionSyntax
)
initializer
).
Arms
)
{
{
var
expression
=
arm
.
Expression
;
var
expression
=
arm
.
Expression
;
...
@@ -381,10 +384,43 @@ private static bool IsSwitchExpressionAndCannotUseVar(TypeSyntax typeName, Expre
...
@@ -381,10 +384,43 @@ private static bool IsSwitchExpressionAndCannotUseVar(TypeSyntax typeName, Expre
{
{
noValidTypeExpressions
=
false
;
noValidTypeExpressions
=
false
;
var
expressionType
=
semanticModel
.
GetTypeInfo
(
expression
).
Type
;
var
expressionType
=
semanticModel
.
GetTypeInfo
(
expression
).
Type
;
if
(
expressionType
!=
null
&&
!
expressionType
.
InheritsFromOrEquals
(
declarationType
))
if
(
expressionType
==
null
)
{
continue
;
}
if
(!
expressionType
.
InheritsFromOrEquals
(
declarationType
))
{
{
return
true
;
return
true
;
}
}
// All arms must be in the same direct line of inheritance.
// e.g. Given the tree:
// C
// / \
// A B
//
// We cannot substitute var for 'x' in the following switch expression,
// as it will introduce a compiler error.
// C x = i switch
// {
// 0 => new A(),
// 1 => new B(),
// _ => throw new ArgumentException(),
// };
if
(
expressionType
.
Equals
(
declarationType
)
||
seenTypes
.
Contains
(
expressionType
))
{
continue
;
}
var
invalidType
=
seenTypes
.
Any
(
t
=>
!
t
.
InheritsFromOrEquals
(
expressionType
)
&&
!
expressionType
.
InheritsFromOrEquals
(
t
));
if
(
invalidType
)
{
return
true
;
}
seenTypes
.
Add
(
expressionType
);
}
}
}
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录