Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Inline temporary variable adds unnecessary cast #70423

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,14 @@ ExpressionSyntax CreateExpressionToInline()

return SyntaxFactory.ArrayCreationExpression(arrayType, arrayInitializer);
}
else if (isVar && expression is ObjectCreationExpressionSyntax or ArrayCreationExpressionSyntax or CastExpressionSyntax)
else if (isVar && expression is
ObjectCreationExpressionSyntax or
ArrayCreationExpressionSyntax or
CastExpressionSyntax or
InvocationExpressionSyntax)
{
// if we have `var x = new Y();` there's no need to do any casting as the type is indicated
// directly in the existing code. The same holds for `new Y[]` or `(Y)...`
// directly in the existing code. The same holds for `new Y[]` or `(Y)...` or `Y(...)`
return expression;
}
else
Expand Down
27 changes: 27 additions & 0 deletions src/Features/CSharpTest/InlineTemporary/InlineTemporaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5884,4 +5884,31 @@ public string M()

await TestInRegularAndScriptAsync(code, expected);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/69869")]
public async Task InlineTemporaryNoNeededVariable()
{
await TestInRegularAndScriptAsync(
"""
using System;
class A
{
void M(string[] args)
{
var [||]a = Math.Round(1.1D);
var b = a;
}
}
""",
"""
using System;
class A
{
void M(string[] args)
{
var b = Math.Round(1.1D);
}
}
""");
}
}
Loading