Skip to content

Commit

Permalink
Handle using var in redundant assignment removal (#75952)
Browse files Browse the repository at this point in the history
Co-authored-by: Omar Bonnet <[email protected]>
  • Loading branch information
obonn1 and Omar Bonnet authored Nov 18, 2024
1 parent f6f0035 commit 0a1a7c6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10072,4 +10072,33 @@ public void Reset() {
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
}.RunAsync();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/72829")]
public async Task RemoveRedundantAssignment_PreservesUsingVar()
{
await TestInRegularAndScriptAsync(
"""
class C
{
void M()
{
{|FixAllInDocument:int items = 0;|}
items = 42;
System.Console.WriteLine(items);
using var _ = System.IO.File.OpenRead("test.txt");
}
}
""",
"""
class C
{
void M()
{
int items = 42;
System.Console.WriteLine(items);
using var _ = System.IO.File.OpenRead("test.txt");
}
}
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public async Task<SyntaxNode> ReplaceAsync(
case LocalDeclarationStatementSyntax localDeclarationStatement:
if (localDeclarationStatement.Declaration.Variables.Any(IsDiscardDeclaration))
{
// Skip replacing discard declarations in "using var"
if (localDeclarationStatement.UsingKeyword != default)
{
continue;
}

RemoveDiscardHelper.ProcessDeclarationStatement(localDeclarationStatement, editor);
}

Expand Down

0 comments on commit 0a1a7c6

Please sign in to comment.