提交 79f00bc6 编写于 作者: D Dustin Campbell

Merge pull request #9625 from DustinCampbell/issue-9576

Fix null ref in C# expander when adding cast to lambda return statement
......@@ -3888,5 +3888,45 @@ static void N(int x, int y)
}";
await TestAsync(initial, expected, compareTokens: false);
}
[WorkItem(9576, "https://github.com/dotnet/roslyn/issues/9576")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTemporary)]
public async Task InlineIntoLambdaWithReturnStatementWithNoExpression()
{
const string initial = @"
using System;
class C
{
static void M(Action a) { }
static void N()
{
var [||]x = 42;
M(() =>
{
Console.WriteLine(x);
return;
});
}
}";
const string expected = @"
using System;
class C
{
static void M(Action a) { }
static void N()
{
M(() =>
{
Console.WriteLine(42);
return;
});
}
}";
await TestAsync(initial, expected, compareTokens: false);
}
}
}
......@@ -141,17 +141,19 @@ public override SyntaxNode VisitReturnStatement(ReturnStatementSyntax node)
if (newNode is ReturnStatementSyntax)
{
var newReturnStatement = (ReturnStatementSyntax)newNode;
var parentLambda = node.FirstAncestorOrSelf<LambdaExpressionSyntax>();
if (parentLambda != null)
if (newReturnStatement.Expression != null)
{
var returnType = (_semanticModel.GetSymbolInfo(parentLambda).Symbol as IMethodSymbol)?.ReturnType;
if (returnType != null)
var parentLambda = node.FirstAncestorOrSelf<LambdaExpressionSyntax>();
if (parentLambda != null)
{
ExpressionSyntax newExpressionWithCast;
if (TryCastTo(returnType, node.Expression, newReturnStatement.Expression, out newExpressionWithCast))
var returnType = (_semanticModel.GetSymbolInfo(parentLambda).Symbol as IMethodSymbol)?.ReturnType;
if (returnType != null)
{
newNode = newReturnStatement.WithExpression(newExpressionWithCast);
ExpressionSyntax newExpressionWithCast;
if (TryCastTo(returnType, node.Expression, newReturnStatement.Expression, out newExpressionWithCast))
{
newNode = newReturnStatement.WithExpression(newExpressionWithCast);
}
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册