提交 3de8a196 编写于 作者: V Victor Zaytsev 提交者: Julien Couvreur

Fixed code style option 'prefer var' for delegate type. (#23718)

Merging on behalf of @zaytsev-victor. Thanks for the contribution!
上级 001a62df
......@@ -2124,5 +2124,90 @@ static int Main(string[] args)
public static bool TryGetValue<T>(T key, out T value) => false;
}", options: ImplicitTypeEverywhere());
}
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(23711, "https://github.com/dotnet/roslyn/issues/23711")]
public async Task SuggestVarForDelegateType()
{
await TestInRegularAndScriptAsync(@"
class Program
{
static void Main(string[] args)
{
[|GetHandler|] handler = Handler;
}
private static GetHandler Handler;
delegate object GetHandler();
}", @"
class Program
{
static void Main(string[] args)
{
var handler = Handler;
}
private static GetHandler Handler;
delegate object GetHandler();
}", options: ImplicitTypeEverywhere());
}
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(23711, "https://github.com/dotnet/roslyn/issues/23711")]
public async Task DoNotSuggestVarForDelegateType1()
{
await TestMissingInRegularAndScriptAsync(@"
class Program
{
static void Main(string[] args)
{
[|GetHandler|] handler = () => new object();
}
private static GetHandler Handler;
delegate object GetHandler();
}", new TestParameters(options: ImplicitTypeEverywhere()));
}
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(23711, "https://github.com/dotnet/roslyn/issues/23711")]
public async Task DoNotSuggestVarForDelegateType2()
{
await TestMissingInRegularAndScriptAsync(@"
class Program
{
static void Main(string[] args)
{
[|GetHandler|] handler = Foo;
}
private static GetHandler Handler;
private static object Foo() => new object();
delegate object GetHandler();
}", new TestParameters(options: ImplicitTypeEverywhere()));
}
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(23711, "https://github.com/dotnet/roslyn/issues/23711")]
public async Task DoNotSuggestVarForDelegateType3()
{
await TestMissingInRegularAndScriptAsync(@"
class Program
{
static void Main(string[] args)
{
[|GetHandler|] handler = delegate { return new object(); };
}
private static GetHandler Handler;
delegate object GetHandler();
}", new TestParameters(options: ImplicitTypeEverywhere()));
}
}
}
......@@ -232,10 +232,9 @@ protected override bool TryAnalyzeVariableDeclaration(TypeSyntax typeName, Seman
return false;
}
// cannot use implicit typing on method group, anonymous function or on dynamic
// cannot use implicit typing on method group or on dynamic
var declaredType = semanticModel.GetTypeInfo(typeName, cancellationToken).Type;
if (declaredType != null &&
(declaredType.TypeKind == TypeKind.Delegate || declaredType.TypeKind == TypeKind.Dynamic))
if (declaredType != null && declaredType.TypeKind == TypeKind.Dynamic)
{
return false;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册