未验证 提交 d3131e53 编写于 作者: D David Wengier 提交者: GitHub

Don't offer to remove as cast from using statements or declarations (#48110)

上级 06c4312d
......@@ -114,6 +114,20 @@ private void SyntaxNodeAction(SyntaxNodeAnalysisContext syntaxContext)
return;
}
// Don't convert if the as is part of a using statement
// eg using (var x = y as MyObject) { }
if (localStatement is UsingStatementSyntax)
{
return;
}
// Don't convert if the as is part of a local declaration with a using keyword
// eg using var x = y as MyObject;
if (localStatement is LocalDeclarationStatementSyntax localDecl && localDecl.UsingKeyword != default)
{
return;
}
var typeNode = asExpression.Right;
var asType = semanticModel.GetTypeInfo(typeNode, cancellationToken).Type;
if (asType.IsNullable())
......
......@@ -1599,6 +1599,42 @@ class Program
}
return dictionary;
}
}");
}
[WorkItem(45596, "https://github.com/dotnet/roslyn/issues/45596")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
public async Task TestMissingInUsingDeclaration()
{
await TestMissingInRegularAndScriptAsync(
@"class C
{
void M()
{
using [|var|] x = o as IDisposable;
if (x != null)
{
}
}
}");
}
[WorkItem(45596, "https://github.com/dotnet/roslyn/issues/45596")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTypeCheck)]
public async Task TestMissingInUsingStatement()
{
await TestMissingInRegularAndScriptAsync(
@"class C
{
void M()
{
using ([|var|] x = o as IDisposable)
{
if (x != null)
{
}
}
}
}");
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册