Skip to content

Commit

Permalink
dotnet#3438, insert missing cast to use predefined Type Conversion Fu…
Browse files Browse the repository at this point in the history
…nctions when available
  • Loading branch information
jrmoreno1 committed Jun 13, 2020
1 parent c65542e commit 6c0ee6f
Show file tree
Hide file tree
Showing 2 changed files with 269 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,26 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.Diagnostics.AddExp
End Function

<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
Public Async Function TestPredefinedAssignment() As Task
Public Async Function TestPredefinedAssignmentCBool() As Task
Await TestInRegularAndScriptAsync(
"Option Strict On
Module M1
Sub Main()
Dim i As Integer = 0
Dim b As Boolean = [|i|]
End Sub
End Module",
"Option Strict On
Module M1
Sub Main()
Dim i As Integer = 0
Dim b As Boolean = CBool(i)
End Sub
End Module")
End Function

<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
Public Async Function TestPredefinedAssignmentCByte() As Task
Await TestInRegularAndScriptAsync(
"Option Strict On
Module M1
Expand All @@ -41,6 +60,253 @@ Module M1
End Module")
End Function

<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
Public Async Function TestPredefinedAssignmentCChar() As Task
Await TestInRegularAndScriptAsync(
"Option Strict On
Module M1
Sub Main()
Dim s As String = 0.ToString
Dim ch As Char = [|s|]
End Sub
End Module",
"Option Strict On
Module M1
Sub Main()
Dim s As String = 0.ToString
Dim ch As Char = CChar(s)
End Sub
End Module")
End Function

<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
Public Async Function TestPredefinedAssignmentCDate() As Task
Await TestInRegularAndScriptAsync(
"Option Strict On
Module M1
Sub Main()
Dim s As String = #2006-06-13#.ToString
Dim dt As Date = [|s|]
End Sub
End Module",
"Option Strict On
Module M1
Sub Main()
Dim s As String = #2006-06-13#.ToString
Dim dt As Date = CDate(s)
End Sub
End Module")
End Function

<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
Public Async Function TestPredefinedAssignmentCDbl() As Task
Await TestInRegularAndScriptAsync(
"Option Strict On
Module M1
Sub Main()
Dim s As String = 1.0R.ToString
Dim db As Double = [|s|]
End Sub
End Module",
"Option Strict On
Module M1
Sub Main()
Dim s As String = 1.0R.ToString
Dim db As Double = CDbl(s)
End Sub
End Module")
End Function

<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
Public Async Function TestPredefinedAssignmentCDec() As Task
Await TestInRegularAndScriptAsync(
"Option Strict On
Module M1
Sub Main()
Dim db As Double = 1.0R
Dim dc As Decimal = [|db|]
End Sub
End Module",
"Option Strict On
Module M1
Sub Main()
Dim db As Double = 1.0R
Dim dc As Decimal = CDec(db)
End Sub
End Module")
End Function

<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
Public Async Function TestPredefinedAssignmentCInt() As Task
Await TestInRegularAndScriptAsync(
"Option Strict On
Module M1
Sub Main()
Dim db As Double = 1.0R
Dim i As Integer = [|db|]
End Sub
End Module",
"Option Strict On
Module M1
Sub Main()
Dim db As Double = 1.0R
Dim i As Integer = CInt(db)
End Sub
End Module")
End Function

<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
Public Async Function TestPredefinedAssignmentCLng() As Task
Await TestInRegularAndScriptAsync(
"Option Strict On
Module M1
Sub Main()
Dim db As Double = 1.0R
Dim lg As Long = [|db|]
End Sub
End Module",
"Option Strict On
Module M1
Sub Main()
Dim db As Double = 1.0R
Dim lg As Long = CLng(db)
End Sub
End Module")
End Function

<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
Public Async Function TestPredefinedAssignmentCSByte() As Task
Await TestInRegularAndScriptAsync(
"Option Strict On
Module M1
Sub Main()
Dim dc As Decimal = -14.02D
Dim sb As SByte = [|dc|]
End Sub
End Module",
"Option Strict On
Module M1
Sub Main()
Dim dc As Decimal = -14.02D
Dim sb As SByte = CSByte(dc)
End Sub
End Module")
End Function

<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
Public Async Function TestPredefinedAssignmentCShort() As Task
Await TestInRegularAndScriptAsync(
"Option Strict On
Module M1
Sub Main()
Dim i As Integer = 2
Dim sh As Short = [|i|]
End Sub
End Module",
"Option Strict On
Module M1
Sub Main()
Dim i As Integer = 2
Dim sh As Short = CShort(i)
End Sub
End Module")
End Function

<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
Public Async Function TestPredefinedAssignmentCSng() As Task
Await TestInRegularAndScriptAsync(
"Option Strict On
Module M1
Sub Main()
Dim db As Double = 1.0R
Dim sn As Single = [|db|]
End Sub
End Module",
"Option Strict On
Module M1
Sub Main()
Dim db As Double = 1.0R
Dim sn As Single = CSng(db)
End Sub
End Module")
End Function

<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
Public Async Function TestPredefinedAssignmentCStr() As Task
Await TestInRegularAndScriptAsync(
"Option Strict On
Module M1
Sub Main()
Dim i As integer = 1
Dim s As String = [|i|]
End Sub
End Module",
"Option Strict On
Module M1
Sub Main()
Dim i As integer = 1
Dim s As String = CStr(i)
End Sub
End Module")
End Function

<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
Public Async Function TestPredefinedAssignmentCUInt() As Task
Await TestInRegularAndScriptAsync(
"Option Strict On
Module M1
Sub Main()
Dim i As Integer = -1
Dim ui As UInteger = [|i|]
End Sub
End Module",
"Option Strict On
Module M1
Sub Main()
Dim i As Integer = -1
Dim ui As UInteger = CUInt(i)
End Sub
End Module")
End Function

<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
Public Async Function TestPredefinedAssignmentCULng() As Task
Await TestInRegularAndScriptAsync(
"Option Strict On
Module M1
Sub Main()
Dim l As Long = -1
Dim ul As ULong =[|l|]
End Sub
End Module",
"Option Strict On
Module M1
Sub Main()
Dim l As Long = -1
Dim ul As ULong = CULng(l)
End Sub
End Module")
End Function

<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
Public Async Function TestPredefinedAssignmentCUShort() As Task
Await TestInRegularAndScriptAsync(
"Option Strict On
Module M1
Sub Main()
Dim i As Integer = -2
Dim us As UShort = [|i|]
End Sub
End Module",
"Option Strict On
Module M1
Sub Main()
Dim i As Integer = -2
Dim us As UShort = CUShort(i)
End Sub
End Module")
End Function

<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
Public Async Function TestAssignment() As Task
Await TestInRegularAndScriptAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
Return SyntaxKind.CULngKeyword
Case specialType.System_UInt16
Return SyntaxKind.CUShortKeyword
Case SpecialType.System_String
Return SyntaxKind.CStrKeyword
Case Else
Return SyntaxKind.None
End Select
Expand Down

0 comments on commit 6c0ee6f

Please sign in to comment.