Skip to content

Commit

Permalink
Conversion of parenthesized ref arguments no longer assigns back - fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamTheCoder committed Dec 20, 2023
1 parent bc6b62a commit 14c7d34
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)


### VB -> C#

* Remove square brackets from identifiers [#1043](https://github.com/icsharpcode/CodeConverter/issues/1043)
* Conversion of parenthesized ref arguments no longer assigns back [#1046](https://github.com/icsharpcode/CodeConverter/issues/1046)

### C# -> VB

Expand Down
4 changes: 2 additions & 2 deletions CodeConverter/CSharp/ExpressionNodeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1819,8 +1819,8 @@ private ArgumentSyntax CreateOptionalRefArg(IParameterSymbol p, RefKind refKind)
private RefConversion NeedsVariableForArgument(VBasic.Syntax.ArgumentSyntax node, RefKind refKind)
{
if (refKind == RefKind.None) return RefConversion.Inline;
if (!(node is VBSyntax.SimpleArgumentSyntax sas)) return RefConversion.PreAssigment;
var expression = sas.Expression.SkipIntoParens();
if (!(node is VBSyntax.SimpleArgumentSyntax sas) || sas is { Expression: VBSyntax.ParenthesizedExpressionSyntax }) return RefConversion.PreAssigment;
var expression = sas.Expression;

return GetRefConversion(expression);

Expand Down
38 changes: 38 additions & 0 deletions Tests/CSharp/ExpressionTests/ByRefTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,44 @@ public void S([Optional] ref DateTime dt)
}");
}

[Fact]
public async Task ParenthesizedArgShouldNotBeAssignedBackAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"
Public Class C
Public Sub S()
Dim i As Integer = 0
Modify(i)
System.Diagnostics.Debug.Assert(i = 1)
Modify((i))
System.Diagnostics.Debug.Assert(i = 1)
End Sub
Sub Modify(ByRef i As Integer)
i = i + 1
End Sub
End Class
", @"using System.Diagnostics;
public partial class C
{
public void S()
{
int i = 0;
Modify(ref i);
Debug.Assert(i == 1);
int argi = i;
Modify(ref argi);
Debug.Assert(i == 1);
}
public void Modify(ref int i)
{
i = i + 1;
}
}");
}

[Fact]
public async Task OutOptionalArgumentAsync()
{
Expand Down

0 comments on commit 14c7d34

Please sign in to comment.