Skip to content

Commit 14c7d34

Browse files
Conversion of parenthesized ref arguments no longer assigns back - fixes #1046
1 parent bc6b62a commit 14c7d34

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
99

1010

1111
### VB -> C#
12+
1213
* Remove square brackets from identifiers [#1043](https://github.com/icsharpcode/CodeConverter/issues/1043)
14+
* Conversion of parenthesized ref arguments no longer assigns back [#1046](https://github.com/icsharpcode/CodeConverter/issues/1046)
1315

1416
### C# -> VB
1517

CodeConverter/CSharp/ExpressionNodeVisitor.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1819,8 +1819,8 @@ private ArgumentSyntax CreateOptionalRefArg(IParameterSymbol p, RefKind refKind)
18191819
private RefConversion NeedsVariableForArgument(VBasic.Syntax.ArgumentSyntax node, RefKind refKind)
18201820
{
18211821
if (refKind == RefKind.None) return RefConversion.Inline;
1822-
if (!(node is VBSyntax.SimpleArgumentSyntax sas)) return RefConversion.PreAssigment;
1823-
var expression = sas.Expression.SkipIntoParens();
1822+
if (!(node is VBSyntax.SimpleArgumentSyntax sas) || sas is { Expression: VBSyntax.ParenthesizedExpressionSyntax }) return RefConversion.PreAssigment;
1823+
var expression = sas.Expression;
18241824

18251825
return GetRefConversion(expression);
18261826

Tests/CSharp/ExpressionTests/ByRefTests.cs

+38
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,44 @@ public void S([Optional] ref DateTime dt)
370370
}");
371371
}
372372

373+
[Fact]
374+
public async Task ParenthesizedArgShouldNotBeAssignedBackAsync()
375+
{
376+
await TestConversionVisualBasicToCSharpAsync(@"
377+
Public Class C
378+
Public Sub S()
379+
Dim i As Integer = 0
380+
Modify(i)
381+
System.Diagnostics.Debug.Assert(i = 1)
382+
Modify((i))
383+
System.Diagnostics.Debug.Assert(i = 1)
384+
End Sub
385+
386+
Sub Modify(ByRef i As Integer)
387+
i = i + 1
388+
End Sub
389+
End Class
390+
", @"using System.Diagnostics;
391+
392+
public partial class C
393+
{
394+
public void S()
395+
{
396+
int i = 0;
397+
Modify(ref i);
398+
Debug.Assert(i == 1);
399+
int argi = i;
400+
Modify(ref argi);
401+
Debug.Assert(i == 1);
402+
}
403+
404+
public void Modify(ref int i)
405+
{
406+
i = i + 1;
407+
}
408+
}");
409+
}
410+
373411
[Fact]
374412
public async Task OutOptionalArgumentAsync()
375413
{

0 commit comments

Comments
 (0)