Skip to content

Commit c8a82bd

Browse files
Only force identifier when referring to local symbol - fixes #601
1 parent 26dc64e commit c8a82bd

3 files changed

Lines changed: 70 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
2626
* Correct logic for conversion "objectWithOverloadedEquals Is Nothing" [#591](https://github.com/icsharpcode/CodeConverter/issues/591)
2727
* Coercing enum to a string now correctly uses its numeric value [#590](https://github.com/icsharpcode/CodeConverter/issues/590)
2828
* Correct conversion for equality of overloaded types [#594](https://github.com/icsharpcode/CodeConverter/issues/594)
29+
* Correct conversion when for loop variable is a class member [#601](https://github.com/icsharpcode/CodeConverter/issues/601)
2930

3031
### C# -> VB
3132

CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,9 +604,9 @@ public override async Task<SyntaxList<StatementSyntax>> VisitForEachBlock(VBSynt
604604
var variableType = varSymbol.GetSymbolType();
605605
var explicitCastWouldHaveNoEffect = variableType?.SpecialType == SpecialType.System_Object || _semanticModel.GetTypeInfo(stmt.Expression).ConvertedType.IsEnumerableOfExactType(variableType);
606606
type = CommonConversions.GetTypeSyntax(varSymbol.GetSymbolType(), explicitCastWouldHaveNoEffect);
607-
var v = await stmt.ControlVariable.AcceptAsync<IdentifierNameSyntax>(_expressionVisitor);
608-
if (_localsToInlineInLoop.Contains(varSymbol)) {
609-
id = v.Identifier;
607+
var v = await stmt.ControlVariable.AcceptAsync<ExpressionSyntax>(_expressionVisitor);
608+
if (_localsToInlineInLoop.Contains(varSymbol) && v is IdentifierNameSyntax vId) {
609+
id = vId.Identifier;
610610
} else {
611611
id = CommonConversions.CsEscapedIdentifier(GetUniqueVariableNameInScope(node, "current" + varSymbol.Name.ToPascalCase()));
612612
statements.Add(SyntaxFactory.ExpressionStatement(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, v, SyntaxFactory.IdentifierName(id))));

Tests/CSharp/StatementTests/LoopStatementTests.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,42 @@ private void TestMethod(int[] values)
219219
}");
220220
}
221221

222+
[Fact]
223+
public async Task ForEachStatementWithFieldVarUsedOuterDeclarationAsync()
224+
{
225+
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
226+
Dim val As Integer
227+
228+
Private Sub TestMethod(ByVal values As Integer())
229+
For Each val In values
230+
If val = 2 Then Continue For
231+
If val = 3 Then Exit For
232+
Next
233+
234+
Console.WriteLine(val)
235+
End Sub
236+
End Class", @"using System;
237+
238+
internal partial class TestClass
239+
{
240+
private int val;
241+
242+
private void TestMethod(int[] values)
243+
{
244+
foreach (var currentVal in values)
245+
{
246+
val = currentVal;
247+
if (val == 2)
248+
continue;
249+
if (val == 3)
250+
break;
251+
}
252+
253+
Console.WriteLine(val);
254+
}
255+
}");
256+
}
257+
222258
[Fact]
223259
public async Task ForEachStatementWithUnusedOuterDeclarationAsync()
224260
{
@@ -246,6 +282,36 @@ private void TestMethod(int[] values)
246282
}");
247283
}
248284

285+
[Fact]
286+
public async Task ForEachStatementWithFieldVarUnusedOuterDeclarationAsync()
287+
{
288+
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
289+
Dim val As Integer
290+
Private Sub TestMethod(ByVal values As Integer())
291+
For Each val In values
292+
If val = 2 Then Continue For
293+
If val = 3 Then Exit For
294+
Next
295+
End Sub
296+
End Class", @"
297+
internal partial class TestClass
298+
{
299+
private int val;
300+
301+
private void TestMethod(int[] values)
302+
{
303+
foreach (var currentVal in values)
304+
{
305+
val = currentVal;
306+
if (val == 2)
307+
continue;
308+
if (val == 3)
309+
break;
310+
}
311+
}
312+
}");
313+
}
314+
249315
[Fact]
250316
public async Task ForEachStatementWithUnusedNestedDeclarationAsync()
251317
{

0 commit comments

Comments
 (0)