Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
e92e5293
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,发现更多精彩内容 >>
未验证
提交
e92e5293
编写于
7月 06, 2020
作者:
C
CyrusNajmabadi
提交者:
GitHub
7月 06, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #45102 from paul1956/fixCastAnalyzerIsUnnecessary
FIx Redundant Cast incorrect for SimpleArguments #44516
上级
29542b0a
f0c26a43
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
67 addition
and
21 deletion
+67
-21
src/Analyzers/VisualBasic/Tests/RemoveUnnecessaryCast/RemoveUnnecessaryCastTests.vb
...Tests/RemoveUnnecessaryCast/RemoveUnnecessaryCastTests.vb
+48
-14
src/Workspaces/SharedUtilitiesAndExtensions/Compiler/VisualBasic/Utilities/CastAnalyzer.vb
...Extensions/Compiler/VisualBasic/Utilities/CastAnalyzer.vb
+19
-7
未找到文件。
src/Analyzers/VisualBasic/Tests/RemoveUnnecessaryCast/RemoveUnnecessaryCastTests.vb
浏览文件 @
e92e5293
...
...
@@ -31,6 +31,40 @@ End Module
Await
TestMissingAsync
(
markup
)
End
Function
<
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsRemoveUnnecessaryCast
)
>
Public
Async
Function
TestDontRemoveCastSimpleArgument1
()
As
Task
Dim
markup
=
<
File
>
Option Strict On
Imports
System.Drawing
Module
M
Sub
Main
()
' test PredefinedCastExpressionSyntax and WalkDownParentheses
Dim
x
As
New
Point
(
[|
CInt
((
System
.
Math
.
Floor
(
1.1
)))
|]
,
1
)
End
Sub
End
Module
</
File
>
Await
TestMissingAsync
(
markup
)
End
Function
<
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsRemoveUnnecessaryCast
)
>
Public
Async
Function
TestDontRemoveCastSimpleArgument2
()
As
Task
Dim
markup
=
<
File
>
Option Strict On
Imports
System.Drawing
Module
M
Sub
Main
()
' test CastExpressionSyntax
Dim
y
As
New
Point
(
[|
CType
(
1.1
,
Integer
)
|]
,
1
)
End
Sub
End
Module
</
File
>
Await
TestMissingAsync
(
markup
)
End
Function
<
WorkItem
(
545148
,
"http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545148"
)
>
<
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsRemoveUnnecessaryCast
)
>
Public
Async
Function
TestParenthesizeToKeepParseTheSame1
()
As
Task
...
...
@@ -38,7 +72,7 @@ End Module
<
File
>
Imports
System.Collections
Imports
System.Linq
Module
Program
Sub
Main
Dim
a
=
CType
(
[|
CObj
(
From
x
In
""
Select
x
)
|]
,
IEnumerable
)
...
...
@@ -50,7 +84,7 @@ End Module
<
File
>
Imports
System.Collections
Imports
System.Linq
Module
Program
Sub
Main
Dim
a
=
CType
((
From
x
In
""
Select
x
),
IEnumerable
)
...
...
@@ -1121,7 +1155,7 @@ Module Program
Dim
a
=
{
[|
CLng
(
Nothing
)
|]
}
Goo
(
a
)
End
Sub
Sub
Goo
(
a
()
As
Long
)
End
Sub
End
Module
...
...
@@ -1899,7 +1933,7 @@ Imports System
Interface
I
Property
A
As
Action
End
Interface
Class
C
Implements
I
Property
A
As
Action
=
[|
CType
(
Sub
()
If
True
Then
,
Action
)
|]
Implements
I
.
A
...
...
@@ -1912,7 +1946,7 @@ Imports System
Interface
I
Property
A
As
Action
End
Interface
Class
C
Implements
I
Property
A
As
Action
=
(
Sub
()
If
True
Then
)
Implements
I
.
A
...
...
@@ -1991,7 +2025,7 @@ Imports System
Module
Program
Sub
Main
()
Dim
x
As
Action
=
Sub
()
Console
.
WriteLine
(
"Hello"
)
[|
CType
(
x
,
Action
)
|]
:
Console
.
WriteLine
()
[|
CType
(
x
,
Action
)
|]
:
Console
.
WriteLine
()
End
Sub
End
Module
...
...
@@ -2003,7 +2037,7 @@ Imports System
Module
Program
Sub
Main
()
Dim
x
As
Action
=
Sub
()
Console
.
WriteLine
(
"Hello"
)
x
()
:
Console
.
WriteLine
()
x
()
:
Console
.
WriteLine
()
End
Sub
End
Module
...
...
@@ -2167,7 +2201,7 @@ Class M
Shared
Sub
Main
()
[|
CType
(
New
M
(),
I1
).
Goo
()
|]
End
Sub
Public
Sub
Goo
()
Implements
I1
.
Goo
End
Sub
End
Class
...
...
@@ -2356,11 +2390,11 @@ Class X
End
Class
Class
Y
Inherits
X
Implements
IDisposable
Private
Sub
IDisposable_Dispose
()
Implements
IDisposable
.
Dispose
Console
.
WriteLine
(
"Y.Dispose"
)
End
Sub
Inherits
X
Implements
IDisposable
Private
Sub
IDisposable_Dispose
()
Implements
IDisposable
.
Dispose
Console
.
WriteLine
(
"Y.Dispose"
)
End
Sub
End
Class
</
File
>
...
...
@@ -2486,7 +2520,7 @@ End Class
Public
Async
Function
TestDontRemoveCastToInterfaceForSealedType5
()
As
Task
' Note: The cast below cannot be removed (even though C is sealed)
' because default values differ for optional parameters and
' hence the method is not considered an implementation.
' hence the method is not considered an implementation.
Dim
markup
=
<
File
>
...
...
src/Workspaces/SharedUtilitiesAndExtensions/Compiler/VisualBasic/Utilities/CastAnalyzer.vb
浏览文件 @
e92e5293
...
...
@@ -113,10 +113,16 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
Return
semanticModel
.
GetTypeInfo
(
otherExpression
).
Type
End
If
Dim
parentSimpleArgument
=
TryCast
(
parent
,
SimpleArgumentSyntax
)
If
TypeOf
parentSimpleArgument
?
.
Expression
Is
CastExpressionSyntax
OrElse
TypeOf
parentSimpleArgument
?
.
Expression
Is
PredefinedCastExpressionSyntax
Then
Return
semanticModel
.
GetTypeInfo
(
parentSimpleArgument
.
Expression
,
cancellationToken
).
Type
End
If
Return
expressionTypeInfo
.
ConvertedType
End
Function
Private
Shared
Function
GetSpeculatedExpressionToOuterTypeConversion
(
speculat
edExpression
As
ExpressionSyntax
,
speculationAnalyzer
As
SpeculationAnalyzer
,
cancellationToken
As
CancellationToken
,
<
Out
>
ByRef
speculatedExpressionOuterType
As
ITypeSymbol
)
As
Conversion
Private
Shared
Function
GetSpeculatedExpressionToOuterTypeConversion
(
speculat
ionAnalyzer
As
SpeculationAnalyzer
,
speculatedExpression
As
ExpressionSyntax
,
outerSpeculatedExpression
As
ExpressionSyntax
,
cancellationToken
As
CancellationToken
,
<
Out
>
ByRef
speculatedExpressionOuterType
As
ITypeSymbol
)
As
Conversion
Dim
innerSpeculatedExpression
=
speculatedExpression
.
WalkDownParentheses
()
Dim
typeInfo
=
speculationAnalyzer
.
SpeculativeSemanticModel
.
GetTypeInfo
(
innerSpeculatedExpression
,
cancellationToken
)
Dim
conv
=
speculationAnalyzer
.
SpeculativeSemanticModel
.
GetConversion
(
innerSpeculatedExpression
,
cancellationToken
)
...
...
@@ -129,7 +135,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
speculatedExpression
=
speculatedExpression
.
WalkUpParentheses
()
typeInfo
=
speculationAnalyzer
.
SpeculativeSemanticModel
.
GetTypeInfo
(
speculatedExpression
,
cancellationToken
)
speculatedExpressionOuterType
=
GetOuterCastType
(
speculatedExpression
,
typeInfo
,
speculationAnalyzer
.
SpeculativeSemanticModel
,
cancellationToken
)
If
speculatedExpressionOuterType
Is
Nothing
Then
If
speculatedExpressionOuterType
Is
Nothing
OrElse
outerSpeculatedExpression
.
IsParentKind
(
SyntaxKind
.
SimpleArgument
)
Then
Return
Nothing
End
If
...
...
@@ -236,7 +242,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
' Simple case: If the conversion from the inner expression to the cast type is identity,
' the cast can be removed.
Return
True
ElseIf
expressionToCastType
.
IsNarrowing
AndAlso
expressionToCastType
.
IsReference
ElseIf
expressionToCastType
.
IsNarrowing
AndAlso
expressionToCastType
.
IsReference
Then
' If the conversion from the inner expression to the cast type is narrowing reference conversion,
' the cast cannot be removed.
Return
False
...
...
@@ -256,7 +262,13 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
speculatedExpressionOuterType
=
outerType
expressionToOuterType
=
_semanticModel
.
ClassifyConversion
(
_castExpressionNode
.
WalkDownParentheses
(),
speculatedExpressionOuterType
)
Else
expressionToOuterType
=
GetSpeculatedExpressionToOuterTypeConversion
(
speculationAnalyzer
.
ReplacedExpression
,
speculationAnalyzer
,
_cancellationToken
,
speculatedExpressionOuterType
)
expressionToOuterType
=
GetSpeculatedExpressionToOuterTypeConversion
(
speculationAnalyzer
,
speculationAnalyzer
.
ReplacedExpression
,
outerSpeculatedExpression
,
_cancellationToken
,
speculatedExpressionOuterType
)
If
expressionToOuterType
=
Nothing
AndAlso
outerSpeculatedExpression
.
IsParentKind
(
SyntaxKind
.
SimpleArgument
)
Then
' If we are here we might be inside a SimpleArgument but it is
' not part of a ParamArray which is handled above.
speculatedExpressionOuterType
=
outerType
expressionToOuterType
=
_semanticModel
.
ClassifyConversion
(
_castExpressionNode
.
WalkDownParentheses
(),
speculatedExpressionOuterType
)
End
If
End
If
' CONSIDER: Anonymous function conversions cannot be compared from different semantic models as lambda symbol comparison requires syntax tree equality. Should this be a compiler bug?
...
...
@@ -266,7 +278,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
End
If
' If there is an user-defined conversion from the expression to the cast type or the cast
' to the outer type, we need to make sure that the same user-defined conversion will be
' to the outer type, we need to make sure that the same user-defined conversion will be
' called if the cast is removed.
If
castToOuterType
.
IsUserDefined
OrElse
expressionToCastType
.
IsUserDefined
Then
Return
(
HaveSameUserDefinedConversion
(
expressionToCastType
,
expressionToOuterType
)
OrElse
...
...
@@ -287,7 +299,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
Dim
expressionToCastTypeIsWideningRefOrDefault
As
Boolean
=
expressionToCastType
.
IsWidening
AndAlso
(
expressionToCastType
.
IsReference
OrElse
expressionToCastType
.
IsDefault
)
Dim
expressionToOuterTypeIsWideningRefOrDefault
As
Boolean
=
expressionToOuterType
.
IsWidening
AndAlso
(
expressionToOuterType
.
IsReference
OrElse
expressionToOuterType
.
IsDefault
)
If
(
expressionToCastTypeIsWideningRefOrDefault
AndAlso
expressionToOuterTypeIsWideningRefOrDefault
)
Then
If
expressionToCastTypeIsWideningRefOrDefault
AndAlso
expressionToOuterTypeIsWideningRefOrDefault
Then
If
expressionToCastType
.
IsDefault
Then
Return
Not
CastRemovalChangesDefaultValue
(
castType
,
outerType
)
End
If
...
...
@@ -305,7 +317,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
End
If
If
Not
castToOuterType
.
IsValueType
AndAlso
castToOuterType
=
expressionToOuterType
Then
If
(
castToOuterType
.
IsNullableValueType
)
Then
If
castToOuterType
.
IsNullableValueType
Then
Return
expressionToOuterType
.
IsWidening
AndAlso
DirectCast
(
castExpressionType
.
OriginalDefinition
,
ITypeSymbol
).
SpecialType
=
SpecialType
.
System_Nullable_T
ElseIf
expressionToCastType
.
IsWidening
AndAlso
expressionToCastType
.
IsNumeric
AndAlso
Not
castToOuterType
.
IsIdentity
Then
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录