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

Don't offer Use Explicit Type for target typed new (#46469)

上级 552de9a2
......@@ -2793,5 +2793,21 @@ private void M(object sender, EventArgs e)
private readonly Action<object, EventArgs> f2;
}", parameters: new TestParameters(options: ImplicitTypeEverywhere()));
}
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseImplicitType)]
public async Task DoNotSuggestVarForTargetTypedNew()
{
await TestMissingInRegularAndScriptAsync(
@"using System;
class Program
{
void Method()
{
[|string|] p = new('c', 1);
}
}", new TestParameters(options: ImplicitTypeEverywhere()));
}
}
}
......@@ -286,6 +286,16 @@ private static bool IsSafeToSwitchToVarWithoutNeedingSpeculation(DeclarationExpr
return false;
}
// var cannot be used with target typed new
#if CODE_STYLE
if (expression.IsKind(Formatting.SyntaxKindEx.ImplicitObjectCreationExpression))
#else
if (expression.IsKind(SyntaxKind.ImplicitObjectCreationExpression))
#endif
{
return false;
}
// cannot use implicit typing on method group or on dynamic
var declaredType = semanticModel.GetTypeInfo(typeName.StripRefIfNeeded(), cancellationToken).Type;
if (declaredType != null && declaredType.TypeKind == TypeKind.Dynamic)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册