VB -> C#: Local variables should be pulled before labels #910
Open
Description
VB.Net input code
Public Shared Sub Main()
Label:
Dim number As Integer
number += 1
If number = 1 Then GoTo Label
Console.WriteLine(number) ' will print 2
End Sub
Erroneous output
public static void Main()
{
Label:
; // that's ugly but still compiles
var number = default(int);
number += 1;
if (number == 1)
goto Label;
Console.WriteLine(number); // will never be called, infinite loop
}
Expected output
public static void Main()
{
var number = default(int);
Label:
number += 1;
if (number == 1)
goto Label;
Console.WriteLine(number);
}
Details
- Product in use: e.g. codeconverter.icsharpcode.net / VS extension / both
- Version in use: e.g. 5.6.3 or a commit hash 73d55b2
- Did you see it working in a previous version, which? no
- Any other relevant information to the issue, or your interest in contributing a fix.
Very rare but unfortunately possible to happen in large, legacy code bases.
Maybe every variable initialization should be moved to the beginning of the procedure/before all labels when there are goto statements?
Related to #897
Even if the scope of a variable is limited to a block, its lifetime is still that of the entire procedure. If you enter the block more than once during the procedure, each block variable retains its previous value. To avoid unexpected results in such a case, it is wise to initialize block variables at the beginning of the block.