未验证 提交 d2c7f369 编写于 作者: A Allison Chou 提交者: GitHub

Merge pull request #40681 from allisonchou/ExtractMethodUsingBug

Fix for extract method crashes when extracting an invalid using statement
......@@ -4250,6 +4250,53 @@ bool NewMethod()
await TestInRegularAndScriptAsync(input, expected, CodeActionIndex, options: Option(CSharpCodeStyleOptions.PreferStaticLocalFunction, CodeStyleOptions.FalseWithSilentEnforcement));
}
[WorkItem(40654, "https://github.com/dotnet/roslyn/issues/40654")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsExtractLocalFunction)]
public async Task TestOnInvalidUsingStatement_MultipleStatements()
{
var input = @"
class C
{
void M()
{
[|var v = 0;
using System;|]
}
}";
var expected = @"
class C
{
void M()
{
{|Rename:NewMethod|}();
static void NewMethod()
{
var v = 0;
using System;
}
}
}";
await TestInRegularAndScriptAsync(input, expected, CodeActionIndex);
}
[WorkItem(40654, "https://github.com/dotnet/roslyn/issues/40654")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsExtractLocalFunction)]
public async Task TestMissingOnUsingStatement()
{
await TestMissingInRegularAndScriptAsync(
@"class C
{
void M()
{
void L()
{
[|using System;|]
}
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsExtractLocalFunction)]
public async Task TestMissingInLocalFunctionDeclaration_ExpressionBody()
{
......
......@@ -3751,7 +3751,7 @@ private static bool NewMethod()
await TestInRegularAndScriptAsync(input, expected);
}
[Fact, WorkItem(40209, "https://github.com/dotnet/roslyn/issues/40209"), Trait(Traits.Feature, Traits.Features.CodeActionsExtractLocalFunction)]
[Fact, WorkItem(40209, "https://github.com/dotnet/roslyn/issues/40209"), Trait(Traits.Feature, Traits.Features.CodeActionsExtractMethod)]
public async Task TestNaming_CamelCase_VerifyLocalFunctionSettingsDontApply()
{
var input = @"
......@@ -3798,7 +3798,7 @@ private static bool NewMethod()
await TestInRegularAndScriptAsync(input, expected);
}
[Fact, WorkItem(40209, "https://github.com/dotnet/roslyn/issues/40209"), Trait(Traits.Feature, Traits.Features.CodeActionsExtractLocalFunction)]
[Fact, WorkItem(40209, "https://github.com/dotnet/roslyn/issues/40209"), Trait(Traits.Feature, Traits.Features.CodeActionsExtractMethod)]
public async Task TestNaming_CamelCase_VerifyLocalFunctionSettingsDontApply_GetName()
{
var input = @"
......@@ -3842,5 +3842,49 @@ private static int GetA()
await TestInRegularAndScriptAsync(input, expected);
}
[WorkItem(40654, "https://github.com/dotnet/roslyn/issues/40654")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsExtractMethod)]
public async Task TestOnInvalidUsingStatement_MultipleStatements()
{
var input = @"
class C
{
void M()
{
[|var v = 0;
using System;|]
}
}";
var expected = @"
class C
{
void M()
{
{|Rename:NewMethod|}();
}
private static void NewMethod()
{
var v = 0;
using System;
}
}";
await TestInRegularAndScriptAsync(input, expected);
}
[WorkItem(40654, "https://github.com/dotnet/roslyn/issues/40654")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsExtractMethod)]
public async Task TestMissingOnInvalidUsingStatement()
{
await TestMissingInRegularAndScriptAsync(
@"class C
{
void M()
{
[|using System;|]
}
}");
}
}
}
......@@ -337,7 +337,7 @@ private OperationStatus CheckActiveStatements(IEnumerable<StatementSyntax> state
foreach (var statement in statements)
{
if (!(statement is LocalDeclarationStatementSyntax declarationStatement))
if (!(statement is LocalDeclarationStatementSyntax declarationStatement) || declarationStatement.Declaration.Variables.FullSpan.IsEmpty)
{
// if given statement is not decl statement.
yield return statement;
......
......@@ -486,6 +486,12 @@ private IList<VariableInfo> GetMethodParameters(ICollection<VariableInfo> variab
continue;
}
// If the variable doesn't have a name, it is invalid.
if (symbol.Name.IsEmpty())
{
continue;
}
if (!TryGetVariableStyle(
bestEffort, symbolMap, symbol, model, type,
captured, dataFlowIn, dataFlowOut, alwaysAssigned, variableDeclared,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册