提交 8648e6c5 编写于 作者: J Jonathon Marolf

Merge pull request #3274 from jmarolf/VBSimplifierBug

Correctly detect option strict on/off when determining if cast is red…
......@@ -87,5 +87,12 @@ NewLines("Option Strict On \n Module Program \n Sub Main(args As String()) \n Di
TestMissing(
NewLines("Option Strict On \n Class A \n End Class \n Class B \n End Class \n Module Program[||] \n Sub Main(args As String()) \n Dim x As A = New B() \n End Sub \n End Module"))
End Sub
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsInsertMissingCast)>
Public Sub TestOptionStrictOn()
Test(
NewLines("Option Strict On \n Module Module1 \n Sub Main() \n Dim red = ColorF.FromArgb(255, 255, 0, 0) \n Dim c As Color = [|red|] \n End Sub \n End Module \n Public Structure ColorF \n Public A, R, G, B As Single \n Public Shared Function FromArgb(a As Double, r As Double, g As Double, b As Double) As ColorF \n Return New ColorF With {.A = CSng(a), .R = CSng(r), .G = CSng(g), .B = CSng(b)} \n End Function \n Public Shared Widening Operator CType(x As Color) As ColorF \n Return ColorF.FromArgb(x.A / 255, x.R / 255, x.G / 255, x.B / 255) \n End Operator \n Public Shared Narrowing Operator CType(x As ColorF) As Color \n Return Color.FromArgb(CByte(x.A * 255), CByte(x.R * 255), CByte(x.G * 255), CByte(x.B * 255)) \n End Operator \n End Structure \n Public Structure Color \n Public A, R, G, B As Byte \n Public Shared Function FromArgb(a As Byte, r As Byte, g As Byte, b As Byte) As Color \n Return New Color With {.A = a, .R = r, .G = g, .B = b} \n End Function \n End Structure"),
NewLines("Option Strict On \n Module Module1 \n Sub Main() \n Dim red = ColorF.FromArgb(255, 255, 0, 0) \n Dim c As Color = CType(red, Color) \n End Sub \n End Module \n Public Structure ColorF \n Public A, R, G, B As Single \n Public Shared Function FromArgb(a As Double, r As Double, g As Double, b As Double) As ColorF \n Return New ColorF With {.A = CSng(a), .R = CSng(r), .G = CSng(g), .B = CSng(b)} \n End Function \n Public Shared Widening Operator CType(x As Color) As ColorF \n Return ColorF.FromArgb(x.A / 255, x.R / 255, x.G / 255, x.B / 255) \n End Operator \n Public Shared Narrowing Operator CType(x As ColorF) As Color \n Return Color.FromArgb(CByte(x.A * 255), CByte(x.R * 255), CByte(x.G * 255), CByte(x.B * 255)) \n End Operator \n End Structure \n Public Structure Color \n Public A, R, G, B As Byte \n Public Shared Function FromArgb(a As Byte, r As Byte, g As Byte, b As Byte) As Color \n Return New Color With {.A = a, .R = r, .G = g, .B = b} \n End Function \n End Structure"))
End Sub
End Class
End Namespace
......@@ -2363,7 +2363,7 @@ End Class
<WorkItem(544655)>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)>
Public Sub RemoveCastToICloneableForDelegate()
' Note: The cast below can be removed because delegates are implictly sealed.
' Note: The cast below can be removed because delegates are implicitly sealed.
Dim markup =
<File>
......@@ -2396,7 +2396,7 @@ End Class
<WorkItem(545926)>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)>
Public Sub RemoveCastToICloneableForArray()
' Note: The cast below can be removed because arrays are implictly sealed.
' Note: The cast below can be removed because arrays are implicitly sealed.
Dim markup =
<File>
......@@ -2427,7 +2427,7 @@ End Class
<WorkItem(529937)>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)>
Public Sub RemoveCastToICloneableForArray2()
' Note: The cast below can be removed because arrays are implictly sealed.
' Note: The cast below can be removed because arrays are implicitly sealed.
Dim markup =
<File>
......@@ -2456,7 +2456,7 @@ End Module
<WorkItem(529897)>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)>
Public Sub RemoveCastToIConvertibleForEnum()
' Note: The cast below can be removed because enums are implictly sealed.
' Note: The cast below can be removed because enums are implicitly sealed.
Dim markup =
<File>
......@@ -2691,6 +2691,84 @@ Class Program
End If
End Sub
End Class
</File>
TestMissing(markup)
End Sub
<WorkItem(3163, "https://github.com/dotnet/roslyn/issues/3163")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)>
Public Sub DoNotRemoveCastInUserDefinedNarrowingConverionStrictOn()
Dim markup =
<File>
Option Strict On
Module Module1
Sub Main()
Dim red = ColorF.FromArgb(255, 255, 0, 0)
Dim c As Color = [|CType(red, Color)|]
End Sub
End Module
Public Structure ColorF
Public A, R, G, B As Single
Public Shared Function FromArgb(a As Double, r As Double, g As Double, b As Double) As ColorF
Return New ColorF With {.A = CSng(a), .R = CSng(r), .G = CSng(g), .B = CSng(b)}
End Function
Public Shared Widening Operator CType(x As Color) As ColorF
Return ColorF.FromArgb(x.A / 255, x.R / 255, x.G / 255, x.B / 255)
End Operator
Public Shared Narrowing Operator CType(x As ColorF) As Color
Return Color.FromArgb(CByte(x.A * 255), CByte(x.R * 255), CByte(x.G * 255), CByte(x.B * 255))
End Operator
End Structure
Public Structure Color
Public A, R, G, B As Byte
Public Shared Function FromArgb(a As Byte, r As Byte, g As Byte, b As Byte) As Color
Return New Color With {.A = a, .R = r, .G = g, .B = b}
End Function
End Structure
</File>
TestMissing(markup)
End Sub
<WorkItem(3163, "https://github.com/dotnet/roslyn/issues/3163")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)>
Public Sub DoNotRemoveCastInUserDefinedNarrowingConverionStrictOff()
Dim markup =
<File>
Option Strict Off
Module Module1
Sub Main()
Dim red = ColorF.FromArgb(255, 255, 0, 0)
Dim c As Color = [|CType(red, Color)|]
End Sub
End Module
Public Structure ColorF
Public A, R, G, B As Single
Public Shared Function FromArgb(a As Double, r As Double, g As Double, b As Double) As ColorF
Return New ColorF With {.A = CSng(a), .R = CSng(r), .G = CSng(g), .B = CSng(b)}
End Function
Public Shared Widening Operator CType(x As Color) As ColorF
Return ColorF.FromArgb(x.A / 255, x.R / 255, x.G / 255, x.B / 255)
End Operator
Public Shared Narrowing Operator CType(x As ColorF) As Color
Return Color.FromArgb(CByte(x.A * 255), CByte(x.R * 255), CByte(x.G * 255), CByte(x.B * 255))
End Operator
End Structure
Public Structure Color
Public A, R, G, B As Byte
Public Shared Function FromArgb(a As Byte, r As Byte, g As Byte, b As Byte) As Color
Return New Color With {.A = a, .R = r, .G = g, .B = b}
End Function
End Structure
</File>
TestMissing(markup)
End Sub
......
......@@ -12,10 +12,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.InsertMissingCast
Inherits CodeFixProvider
Friend Const BC30512 As String = "BC30512" ' Option Strict On disallows implicit conversions from '{0}' to '{1}'.
Friend Const BC42016 As String = "BC42016" ' Implicit conversions from '{0}' to '{1}'.
Public NotOverridable Overrides ReadOnly Property FixableDiagnosticIds As ImmutableArray(Of String)
Get
Return ImmutableArray.Create(BC30512)
Return ImmutableArray.Create(BC30512, BC42016)
End Get
End Property
......
......@@ -253,7 +253,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
If castToOuterType.IsUserDefined OrElse expressionToCastType.IsUserDefined Then
Return (HaveSameUserDefinedConversion(expressionToCastType, expressionToOuterType) OrElse
HaveSameUserDefinedConversion(castToOuterType, expressionToOuterType)) AndAlso
UserDefinedConversionIsAllowed(_castNode, _semanticModel)
(UserDefinedConversionIsAllowed(_castNode, _semanticModel) AndAlso
Not expressionToCastType.IsNarrowing)
ElseIf expressionToOuterType.IsUserDefined Then
Return False
End If
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册