提交 6c30c446 编写于 作者: C Cyrus Najmabadi

Still do expensive check if method is non-generic.

上级 21e06eb1
......@@ -1963,7 +1963,7 @@ unsafe static void M()
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(23116, "https://github.com/dotnet/roslyn/issues/23116")]
public async Task DoSuggestForDeclarationExpressionIfItWouldNotChangeOverloadResolution()
public async Task DoSuggestForDeclarationExpressionIfItWouldNotChangeOverloadResolution2()
{
await TestInRegularAndScriptAsync(@"
class Program
......@@ -1974,7 +1974,8 @@ static int Main(string[] args)
return value;
}
public static bool TryGetValue<T>(string key, out T value) => false;
public static bool TryGetValue(string key, out int value) => false;
public static bool TryGetValue(string key, out bool value, int x) => false;
}", @"
class Program
{
......@@ -1984,15 +1985,16 @@ static int Main(string[] args)
return value;
}
public static bool TryGetValue<T>(string key, out T value) => false;
public static bool TryGetValue(string key, out int value) => false;
public static bool TryGetValue(string key, out bool value, int x) => false;
}", options: ImplicitTypeEverywhere());
}
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(23116, "https://github.com/dotnet/roslyn/issues/23116")]
public async Task DoSuggestForDeclarationExpressionIfItWouldNotChangeOverloadResolution2()
public async Task DoNotSuggestForDeclarationExpressionIfItWouldChangeOverloadResolution()
{
await TestInRegularAndScriptAsync(@"
await TestMissingInRegularAndScriptAsync(@"
class Program
{
static int Main(string[] args)
......@@ -2001,39 +2003,81 @@ static int Main(string[] args)
return value;
}
public static bool TryGetValue(string key, out int value) => false;
public static bool TryGetValue(string key, out bool value, int x) => false;
public static bool TryGetValue(string key, out object value) => false;
public static bool TryGetValue<T>(string key, out T value) => false;
}", new TestParameters(options: ImplicitTypeEverywhere()));
}
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(23116, "https://github.com/dotnet/roslyn/issues/23116")]
public async Task DoNotSuggestIfChangesGenericTypeInference()
{
await TestMissingInRegularAndScriptAsync(@"
class Program
{
static int Main(string[] args)
{
TryGetValue(""key"", out [|int|] value);
return value;
}
public static bool TryGetValue<T>(string key, out T value) => false;
}", new TestParameters(options: ImplicitTypeEverywhere()));
}
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(23116, "https://github.com/dotnet/roslyn/issues/23116")]
public async Task SuggestIfDoesNotChangeGenericTypeInference1()
{
await TestInRegularAndScriptAsync(@"
class Program
{
static int Main(string[] args)
{
TryGetValue<int>(""key"", out [|int|] value);
return value;
}
public static bool TryGetValue<T>(string key, out T value) => false;
}", @"
class Program
{
static int Main(string[] args)
{
TryGetValue(""key"", out var value);
TryGetValue<int>(""key"", out var value);
return value;
}
public static bool TryGetValue(string key, out int value) => false;
public static bool TryGetValue(string key, out bool value, int x) => false;
public static bool TryGetValue<T>(string key, out T value) => false;
}", options: ImplicitTypeEverywhere());
}
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsUseExplicitType)]
[WorkItem(23116, "https://github.com/dotnet/roslyn/issues/23116")]
public async Task DoNotSuggestForDeclarationExpressionIfItWouldChangeOverloadResolution()
public async Task SuggestIfDoesNotChangeGenericTypeInference2()
{
await TestMissingInRegularAndScriptAsync(@"
await TestInRegularAndScriptAsync(@"
class Program
{
static int Main(string[] args)
{
TryGetValue(""key"", out [|int|] value);
TryGetValue(0, out [|int|] value);
return value;
}
public static bool TryGetValue(string key, out object value) => false;
public static bool TryGetValue<T>(T key, out T value) => false;
}", @"
class Program
{
static int Main(string[] args)
{
TryGetValue(0, out var value);
return value;
}
public static bool TryGetValue<T>(string key, out T value) => false;
}", new TestParameters(options: ImplicitTypeEverywhere()));
public static bool TryGetValue<T>(T key, out T value) => false;
}", options: ImplicitTypeEverywhere());
}
}
}
......@@ -173,9 +173,13 @@ protected override bool TryAnalyzeVariableDeclaration(TypeSyntax typeName, Seman
var memberGroup = semanticModel.GetMemberGroup(invocationExpression.Expression, cancellationToken);
if (memberGroup.Length == 1)
{
// Only one member found. Don't do the expensive check, this change
// should be ok.
return true;
// Only one member found. In most cases, converting the type to "var" will be
// ok. However, if the method is generic it may not be as inference may be
// involved.
if (memberGroup[0].GetTypeParameters().IsEmpty)
{
return true;
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册