Skip to content

Commit 1d7ac53

Browse files
authored
Merge pull request #45139 from jrmoreno1/dev3438
#3438, use standard Type Conversion Functions when possible
2 parents 4d0c2c0 + 60d0083 commit 1d7ac53

File tree

4 files changed

+298
-11
lines changed

4 files changed

+298
-11
lines changed

src/EditorFeatures/Test2/Expansion/ExtensionMethodExpansionRewriteTests.vb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Imports System.Runtime.CompilerServices
8888
Public Class Program
8989
Public Sub Main(args As String())
9090
Dim p As Program = Nothing
91-
Dim ss = Global.ProgramExtensions.goo((CType((p), Global.Program)), (CType((""), System.String)))
91+
Dim ss = Global.ProgramExtensions.goo((CType((p), Global.Program)), (CStr((""))))
9292
End Sub
9393
End Class
9494

@@ -182,7 +182,7 @@ Imports System.Runtime.CompilerServices
182182
Public Class Program
183183
Public Sub Main(args As String())
184184
Dim p As Program = Nothing
185-
Dim ss = Global.ProgramExtensions.goo((CType((Global.ProgramExtensions.goo((CType((p), Global.Program)), (CType((""), System.String)))), Global.Program)), (CType((""), System.String)))
185+
Dim ss = Global.ProgramExtensions.goo((CType((Global.ProgramExtensions.goo((CType((p), Global.Program)), (CStr((""))))), Global.Program)), (CStr((""))))
186186
End Sub
187187
End Class
188188

@@ -229,7 +229,7 @@ Imports System.Runtime.CompilerServices
229229
Public Class Program
230230
Public Sub Main(args As String())
231231
Dim p As Program = Nothing
232-
Dim ss = Global.ProgramExtensions.goo((CType((Global.ProgramExtensions.goo((CType((p), Global.Program)), (CType((""), System.String)), (CType((""), System.String)), (CType((""), System.String)))), Global.Program)), (CType((""), System.String)), (CType((""), System.String)), (CType((""), System.String)))
232+
Dim ss = Global.ProgramExtensions.goo((CType((Global.ProgramExtensions.goo((CType((p), Global.Program)), (CStr((""))), (CStr((""))), (CStr((""))))), Global.Program)), (CStr((""))), (CStr((""))), (CStr((""))))
233233
End Sub
234234
End Class
235235

src/EditorFeatures/VisualBasicTest/Diagnostics/AddExplicitCast/AddExplicitCastTests.vb

Lines changed: 290 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,26 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.Diagnostics.AddExp
2121
End Function
2222

2323
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
24-
Public Async Function TestPredefinedAssignment() As Task
24+
Public Async Function TestPredefinedAssignmentCBool() As Task
25+
Await TestInRegularAndScriptAsync(
26+
"Option Strict On
27+
Module M1
28+
Sub Main()
29+
Dim i As Integer = 0
30+
Dim b As Boolean = [|i|]
31+
End Sub
32+
End Module",
33+
"Option Strict On
34+
Module M1
35+
Sub Main()
36+
Dim i As Integer = 0
37+
Dim b As Boolean = CBool(i)
38+
End Sub
39+
End Module")
40+
End Function
41+
42+
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
43+
Public Async Function TestPredefinedAssignmentCByte() As Task
2544
Await TestInRegularAndScriptAsync(
2645
"Option Strict On
2746
Module M1
@@ -41,6 +60,272 @@ Module M1
4160
End Module")
4261
End Function
4362

63+
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
64+
Public Async Function TestPredefinedAssignmentCChar() As Task
65+
Await TestInRegularAndScriptAsync(
66+
"Option Strict On
67+
Module M1
68+
Sub Main()
69+
Dim s As String = 0.ToString()
70+
Dim ch As Char = [|s|]
71+
End Sub
72+
End Module",
73+
"Option Strict On
74+
Module M1
75+
Sub Main()
76+
Dim s As String = 0.ToString()
77+
Dim ch As Char = CChar(s)
78+
End Sub
79+
End Module")
80+
End Function
81+
82+
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
83+
Public Async Function TestPredefinedAssignmentCDate() As Task
84+
Await TestInRegularAndScriptAsync(
85+
"Option Strict On
86+
Module M1
87+
Sub Main()
88+
Dim s As String = #2006-06-13#.ToString()
89+
Dim dt As Date = [|s|]
90+
End Sub
91+
End Module",
92+
"Option Strict On
93+
Module M1
94+
Sub Main()
95+
Dim s As String = #2006-06-13#.ToString()
96+
Dim dt As Date = CDate(s)
97+
End Sub
98+
End Module")
99+
End Function
100+
101+
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
102+
Public Async Function TestPredefinedAssignmentCDbl() As Task
103+
Await TestInRegularAndScriptAsync(
104+
"Option Strict On
105+
Module M1
106+
Sub Main()
107+
Dim s As String = 1.0R.ToString()
108+
Dim db As Double = [|s|]
109+
End Sub
110+
End Module",
111+
"Option Strict On
112+
Module M1
113+
Sub Main()
114+
Dim s As String = 1.0R.ToString()
115+
Dim db As Double = CDbl(s)
116+
End Sub
117+
End Module")
118+
End Function
119+
120+
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
121+
Public Async Function TestPredefinedAssignmentCDec() As Task
122+
Await TestInRegularAndScriptAsync(
123+
"Option Strict On
124+
Module M1
125+
Sub Main()
126+
Dim db As Double = 1.0R
127+
Dim dc As Decimal = [|db|]
128+
End Sub
129+
End Module",
130+
"Option Strict On
131+
Module M1
132+
Sub Main()
133+
Dim db As Double = 1.0R
134+
Dim dc As Decimal = CDec(db)
135+
End Sub
136+
End Module")
137+
End Function
138+
139+
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
140+
Public Async Function TestPredefinedAssignmentCInt() As Task
141+
Await TestInRegularAndScriptAsync(
142+
"Option Strict On
143+
Module M1
144+
Sub Main()
145+
Dim db As Double = 1.0R
146+
Dim i As Integer = [|db|]
147+
End Sub
148+
End Module",
149+
"Option Strict On
150+
Module M1
151+
Sub Main()
152+
Dim db As Double = 1.0R
153+
Dim i As Integer = CInt(db)
154+
End Sub
155+
End Module")
156+
End Function
157+
158+
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
159+
Public Async Function TestPredefinedAssignmentCLng() As Task
160+
Await TestInRegularAndScriptAsync(
161+
"Option Strict On
162+
Module M1
163+
Sub Main()
164+
Dim db As Double = 1.0R
165+
Dim lg As Long = [|db|]
166+
End Sub
167+
End Module",
168+
"Option Strict On
169+
Module M1
170+
Sub Main()
171+
Dim db As Double = 1.0R
172+
Dim lg As Long = CLng(db)
173+
End Sub
174+
End Module")
175+
End Function
176+
177+
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
178+
Public Async Function TestPredefinedAssignmentCSByte() As Task
179+
Await TestInRegularAndScriptAsync(
180+
"Option Strict On
181+
Module M1
182+
Sub Main()
183+
Dim dc As Decimal = -14.02D
184+
Dim sb As SByte = [|dc|]
185+
End Sub
186+
End Module",
187+
"Option Strict On
188+
Module M1
189+
Sub Main()
190+
Dim dc As Decimal = -14.02D
191+
Dim sb As SByte = CSByte(dc)
192+
End Sub
193+
End Module")
194+
End Function
195+
196+
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
197+
Public Async Function TestPredefinedAssignmentCShort() As Task
198+
Await TestInRegularAndScriptAsync(
199+
"Option Strict On
200+
Module M1
201+
Sub Main()
202+
Dim i As Integer = 2
203+
Dim sh As Short = [|i|]
204+
End Sub
205+
End Module",
206+
"Option Strict On
207+
Module M1
208+
Sub Main()
209+
Dim i As Integer = 2
210+
Dim sh As Short = CShort(i)
211+
End Sub
212+
End Module")
213+
End Function
214+
215+
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
216+
Public Async Function TestPredefinedAssignmentCSng() As Task
217+
Await TestInRegularAndScriptAsync(
218+
"Option Strict On
219+
Module M1
220+
Sub Main()
221+
Dim db As Double = 1.0R
222+
Dim sn As Single = [|db|]
223+
End Sub
224+
End Module",
225+
"Option Strict On
226+
Module M1
227+
Sub Main()
228+
Dim db As Double = 1.0R
229+
Dim sn As Single = CSng(db)
230+
End Sub
231+
End Module")
232+
End Function
233+
234+
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
235+
Public Async Function TestPredefinedAssignmentCStr() As Task
236+
Await TestInRegularAndScriptAsync(
237+
"Option Strict On
238+
Module M1
239+
Sub Main()
240+
Dim i As Integer = 1
241+
Dim s As String = [|i|]
242+
End Sub
243+
End Module",
244+
"Option Strict On
245+
Module M1
246+
Sub Main()
247+
Dim i As Integer = 1
248+
Dim s As String = CStr(i)
249+
End Sub
250+
End Module")
251+
End Function
252+
253+
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
254+
Public Async Function TestPredefinedAssignmentObjectToStringCStr() As Task
255+
Await TestInRegularAndScriptAsync(
256+
"Option Strict On
257+
Module M1
258+
Sub Main()
259+
Dim o As Object = 1.ToString()
260+
Dim s As String = [|o|]
261+
End Sub
262+
End Module",
263+
"Option Strict On
264+
Module M1
265+
Sub Main()
266+
Dim o As Object = 1.ToString()
267+
Dim s As String = CStr(o)
268+
End Sub
269+
End Module")
270+
End Function
271+
272+
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
273+
Public Async Function TestPredefinedAssignmentCUInt() As Task
274+
Await TestInRegularAndScriptAsync(
275+
"Option Strict On
276+
Module M1
277+
Sub Main()
278+
Dim i As Integer = -1
279+
Dim ui As UInteger = [|i|]
280+
End Sub
281+
End Module",
282+
"Option Strict On
283+
Module M1
284+
Sub Main()
285+
Dim i As Integer = -1
286+
Dim ui As UInteger = CUInt(i)
287+
End Sub
288+
End Module")
289+
End Function
290+
291+
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
292+
Public Async Function TestPredefinedAssignmentCULng() As Task
293+
Await TestInRegularAndScriptAsync(
294+
"Option Strict On
295+
Module M1
296+
Sub Main()
297+
Dim l As Long = -1
298+
Dim ul As ULong =[|l|]
299+
End Sub
300+
End Module",
301+
"Option Strict On
302+
Module M1
303+
Sub Main()
304+
Dim l As Long = -1
305+
Dim ul As ULong = CULng(l)
306+
End Sub
307+
End Module")
308+
End Function
309+
310+
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
311+
Public Async Function TestPredefinedAssignmentCUShort() As Task
312+
Await TestInRegularAndScriptAsync(
313+
"Option Strict On
314+
Module M1
315+
Sub Main()
316+
Dim i As Integer = -2
317+
Dim us As UShort = [|i|]
318+
End Sub
319+
End Module",
320+
"Option Strict On
321+
Module M1
322+
Sub Main()
323+
Dim i As Integer = -2
324+
Dim us As UShort = CUShort(i)
325+
End Sub
326+
End Module")
327+
End Function
328+
44329
<Fact(), Trait(Traits.Feature, Traits.Features.CodeActionsAddExplicitCast)>
45330
Public Async Function TestAssignment() As Task
46331
Await TestInRegularAndScriptAsync(
@@ -2930,17 +3215,17 @@ Class Program
29303215

29313216
Private Sub M()
29323217
Dim b As Base = New Base()
2933-
Foo(s:="""", i:=1, d:=CType(b, {0}))
3218+
Foo(s:="""", i:=1, d:={0})
29343219
End Sub
29353220
End Class"
29363221

2937-
Await TestInRegularAndScriptAsync(initialMarkup, String.Format(expect_format, "String"), index:=0,
3222+
Await TestInRegularAndScriptAsync(initialMarkup, String.Format(expect_format, "CStr(b)"), index:=0,
29383223
title:=String.Format(FeaturesResources.Convert_type_to_0, "String"))
29393224

2940-
Await TestInRegularAndScriptAsync(initialMarkup, String.Format(expect_format, "Derived"), index:=1,
3225+
Await TestInRegularAndScriptAsync(initialMarkup, String.Format(expect_format, "CType(b, Derived)"), index:=1,
29413226
title:=String.Format(FeaturesResources.Convert_type_to_0, "Derived"))
29423227

2943-
Await TestInRegularAndScriptAsync(initialMarkup, String.Format(expect_format, "Derived2"), index:=2,
3228+
Await TestInRegularAndScriptAsync(initialMarkup, String.Format(expect_format, "CType(b, Derived2)"), index:=2,
29443229
title:=String.Format(FeaturesResources.Convert_type_to_0, "Derived2"))
29453230
End Function
29463231

src/EditorFeatures/VisualBasicTest/Diagnostics/AddExplicitCast/AddExplicitCastTests_FixAllTests.vb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,7 @@ Public Class Program1
12701270
Foo4(i:=1, j:="", CType(b1, Derived1))
12711271
Foo5("", 1, b1)
12721272
Foo5(d:=CType(b1, Derived2), i:=1, j:="", x:=1)
1273-
Foo5(CType(1, String), "", x:=1, d:=b1)
1273+
Foo5(CStr(1), "", x:=1, d:=b1)
12741274
Foo5(1, "", d:=b1, b2, b3, d1)
12751275
Foo5("", 1, d:=b1, b2, b3, d1)
12761276
Dim d2list = New Derived2() {}
@@ -1475,7 +1475,7 @@ Public Class Program1
14751475
Foo4(i:=1, j:="", CType(b1, Derived1))
14761476
Foo5("", 1, b1)
14771477
Foo5(d:=CType(b1, Derived2), i:=1, j:="", x:=1)
1478-
Foo5(CType(1, String), "", x:=1, d:=b1)
1478+
Foo5(CStr(1), "", x:=1, d:=b1)
14791479
Foo5(1, "", d:=b1, b2, b3, d1)
14801480
Foo5("", 1, d:=b1, b2, b3, d1)
14811481
Dim d2list = New Derived2() {}
@@ -1680,7 +1680,7 @@ Public Class Program1
16801680
Foo4(i:=1, j:="", CType(b1, Derived1))
16811681
Foo5("", 1, b1)
16821682
Foo5(d:=CType(b1, Derived2), i:=1, j:="", x:=1)
1683-
Foo5(CType(1, String), "", x:=1, d:=b1)
1683+
Foo5(CStr(1), "", x:=1, d:=b1)
16841684
Foo5(1, "", d:=b1, b2, b3, d1)
16851685
Foo5("", 1, d:=b1, b2, b3, d1)
16861686
Dim d2list = New Derived2() {}

src/Workspaces/SharedUtilitiesAndExtensions/Workspace/VisualBasic/Extensions/ITypeSymbolExtensions.vb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions
4242
Return SyntaxKind.CSngKeyword
4343
Case specialType.System_Int16
4444
Return SyntaxKind.CShortKeyword
45+
Case SpecialType.System_String
46+
Return SyntaxKind.CStrKeyword
4547
Case specialType.System_UInt32
4648
Return SyntaxKind.CUIntKeyword
4749
Case specialType.System_UInt64

0 commit comments

Comments
 (0)