未验证 提交 e92e5293 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #45102 from paul1956/fixCastAnalyzerIsUnnecessary

FIx Redundant Cast incorrect for SimpleArguments #44516
......@@ -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>
......
......@@ -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(speculatedExpression As ExpressionSyntax, speculationAnalyzer As SpeculationAnalyzer, cancellationToken As CancellationToken, <Out> ByRef speculatedExpressionOuterType As ITypeSymbol) As Conversion
Private Shared Function GetSpeculatedExpressionToOuterTypeConversion(speculationAnalyzer 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.
先完成此消息的编辑!
想要评论请 注册