提交 9b9f11b2 编写于 作者: C CyrusNajmabadi

Use-throw-expression should not be offered when the 'if-block' has additional statements in it.

上级 bc9be6c0
......@@ -386,6 +386,48 @@ public C(int? x)
_x = x;
}
}");
}
[WorkItem(19377, "https://github.com/dotnet/roslyn/issues/19377")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseThrowExpression)]
public async Task TestNotWithMultipleStatementsInIf1()
{
await TestMissingInRegularAndScriptAsync(
@"using System;
class C
{
void M(string s)
{
if (s == null)
{
Console.WriteLine();
[|throw|] new ArgumentNullException(nameof(s));
}
_s = s;
}
}");
}
[WorkItem(19377, "https://github.com/dotnet/roslyn/issues/19377")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseThrowExpression)]
public async Task TestNotWithMultipleStatementsInIf2()
{
await TestMissingInRegularAndScriptAsync(
@"using System;
class C
{
void M(string s)
{
if (s == null)
{
[|throw|] new ArgumentNullException(nameof(s));
Console.WriteLine();
}
_s = s;
}
}");
}
}
......
......@@ -116,6 +116,12 @@ private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol
return;
}
if (!IsOnlyStatementOfIf(ifOperation, throwOperation))
{
// The if-statement can only have a single throw-statement in it.
return;
}
var containingBlock = GetOperation(
semanticModel, ifOperation.Syntax.Parent, cancellationToken) as IBlockStatement;
if (containingBlock == null)
......@@ -189,6 +195,18 @@ private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol
}
}
private bool IsOnlyStatementOfIf(IIfStatement ifOperation, IThrowStatement throwStatement)
{
if (ifOperation.IfTrueStatement.Syntax == throwStatement.Syntax)
{
return true;
}
return ifOperation.IfTrueStatement is IBlockStatement block &&
block.Statements.Length == 1 &&
block.Statements[0].Syntax == throwStatement.Syntax;
}
protected abstract ISyntaxFactsService GetSyntaxFactsService();
protected abstract ISemanticFactsService GetSemanticFactsService();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册