VB -> C#: Incorrect conversion of `If` immediately preceded by `Else`
See original GitHub issueSeems like a pretty serious bug, as it may screw up control flow significantly and lead to completely unexpected results.
VB.Net input code
If OUTER Then
If FOO1 Then Bar1() : Exit Sub
Else 'NOT OUTER
If FOO2 Then
Bar2()
Exit Sub
End If
End If 'OUTER
Erroneous output
if (OUTER)
if (FOO1) {
Bar1();
return;
} else if (FOO2) {
Bar2();
return;
}
Expected output
if (OUTER) {
if (FOO1) {
Bar1();
return;
}
} else { //NOT OUTER
if (FOO2) {
Bar2();
return;
}
}
Details
- Product in use: VS extension
- Version in use: latest - 8.4.5.0
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
Conditionals with if, else, and Booleans
The condition is a Boolean expression: an expression that evaluates to either true or false . Boolean values are another type of data...
Read more >Logical and Bitwise Operators - Visual Basic
Floating-point values must be converted to integral types before bitwise operation can proceed.
Read more >Why do I get an error: 'else' without a previous 'if' even ...
You're getting this error because you have put an incorrect semicolon at the end of the line containing the 'if' statement. This confuses...
Read more >Statements in Visual Basic
When the code containing a declaration statement runs, Visual Basic reserves the memory required for the declared element.
Read more >Prepare Visual Basic for Conversion to C#
Many VB developers just take that value and assign it directly to another variable. This means that the VB runtime has to perform...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I did make an attempt at reproing within roslyn, but got bogged down by trying to find an existing type of test to fit this in with and ran out of time. Will try again when I have more time. For the workaround locally, I can’t manage to construct an option to override the one mentioned since so much is internal. We could of course stop using the document options altogether but I feel like that’s a degradation. Unless with fresh eyes anyone can figure out how to get at the option we need, we’ll likely need to use some reflection hackery unfortunately.
Debugging through, I can see that the issue is indeed introduced here: https://github.com/icsharpcode/CodeConverter/blob/068c7dd9ec06ec32d51794bfd2f0e0c40fb7114e/CodeConverter/Shared/DocumentExtensions.cs#L110-L112 This looks like a bug in roslyn. I’ll try to repro purely against that library and file/fix upstream if I can.
Even if the fix was made today, we couldn’t rely on everyone using a version of VS containing it for years, so it’s worth a fix/workaround given the severity. In the file I referenced you can see it does grab the options from the Document (which Visual Studio controls). Originally I’d hoped this would include editorconfig stuff, though I’ve seen no evidence of that ever working. You can see in that file I’ve overridden one of the VB options, and I will see if I can do the same for this case.