未验证 提交 0efde3ee 编写于 作者: A Andrew Hall 提交者: GitHub

Merge pull request #32759 from ryzngard/issue/29793_uselocal_dont_report

UseLocalFunction should not report diagnostics on invalid decleration code
......@@ -17,6 +17,8 @@ public partial class UseLocalFunctionTests : AbstractCSharpDiagnosticProviderBas
internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace)
=> (new CSharpUseLocalFunctionDiagnosticAnalyzer(), new CSharpUseLocalFunctionCodeFixProvider());
private static ParseOptions CSharp72ParseOptions = CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_2);
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseLocalFunction)]
public async Task TestMissingBeforeCSharp7()
{
......@@ -862,7 +864,10 @@ int fibonacci(int v)
return fibonacci(v - 1, v - 2);
}
}
}");
}",
// 7.1 is required for default literals, so 7.2 should be sufficient
// and is used in other tests
parseOptions: CSharp72ParseOptions);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseLocalFunction)]
......@@ -1364,7 +1369,9 @@ void M()
{
void lambda(in int p) => throw null;
}
}");
}",
// Run with 7.2 to get read-only references
parseOptions: CSharp72ParseOptions);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseLocalFunction)]
......@@ -3307,6 +3314,241 @@ void Method(Func<string> o)
void Method(CustomDelegate o)
{
}
}");
}
[WorkItem(29793, "https://github.com/dotnet/roslyn/issues/29793")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseLocalFunction)]
public async Task TestNotAvailableWithInvalidDeclaration()
{
await TestMissingAsync(
@"using System;
class C
{
void M()
{
Func<string> [||]f = () => null);
Method(f);
}
void Method(Func<string> o)
{
}
}
");
}
[WorkItem(29793, "https://github.com/dotnet/roslyn/issues/29793")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseLocalFunction)]
public async Task TestNotAvailableWithInvalidDeclaration2()
{
await TestMissingAsync(
@"using System;
class C
{
void M()
{
Func<string> [||]f) = () => null;
Method(f);
}
void Method(Func<string> o)
{
}
}
");
}
[WorkItem(29793, "https://github.com/dotnet/roslyn/issues/29793")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseLocalFunction)]
public async Task TestNotAvailableWithInvalidDeclaration3()
{
await TestMissingAsync(
@"using System;
class C
{
void M()
{
Func<string>) [||]f = () => null;
Method(f);
}
void Method(Func<string> o)
{
}
}
");
}
[WorkItem(29793, "https://github.com/dotnet/roslyn/issues/29793")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseLocalFunction)]
public async Task TestWithInvalidUnrelatedCode()
{
await TestInRegularAndScript1Async(
@"using System;
class C
{
void M()
{
Func<int, string> [||]f = _ => null;
Method(f);
}
void Method<T>(Func<T, string> o))
{
}
}",
@"using System;
class C
{
void M()
{
string f(int _) => null;
Method((Func<int, string>)f);
}
void Method<T>(Func<T, string> o))
{
}
}");
}
[WorkItem(29793, "https://github.com/dotnet/roslyn/issues/29793")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseLocalFunction)]
public async Task TestWithInvalidUnrelatedCode2()
{
await TestInRegularAndScript1Async(
@"using System;
class C
{
void M()
{
Func<int, string> [||]f = _ => null;
(Method(f);
}
void Method<T>(Func<T, string> o)
{
}
}",
@"using System;
class C
{
void M()
{
string f(int _) => null;
(Method((Func<int, string>)f);
}
void Method<T>(Func<T, string> o)
{
}
}");
}
[WorkItem(29793, "https://github.com/dotnet/roslyn/issues/29793")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseLocalFunction)]
public async Task TestWithObsoleteCode()
{
await TestInRegularAndScript1Async(
@"using System;
class C
{
void M()
{
Func<int, string> [||]f = _ => S();
Method(f);
}
[System.Obsolete]
string S()
{
return null;
}
void Method<T>(Func<T, string> o)
{
}
}",
@"using System;
class C
{
void M()
{
string f(int _) => S();
Method((Func<int, string>)f);
}
[System.Obsolete]
string S()
{
return null;
}
void Method<T>(Func<T, string> o)
{
}
}");
}
[WorkItem(29793, "https://github.com/dotnet/roslyn/issues/29793")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseLocalFunction)]
public async Task TestWithDeclarationWarning()
{
await TestInRegularAndScript1Async(
@"using System;
class C
{
void M()
{
#warning Declaration Warning
Func<int, string> [||]f = _ => null;
Method(f);
}
void Method<T>(Func<T, string> o)
{
}
}",
@"using System;
class C
{
void M()
{
#warning Declaration Warning
string f(int _) => null;
Method((Func<int, string>)f);
}
void Method<T>(Func<T, string> o)
{
}
}");
}
}
......
......@@ -106,6 +106,14 @@ private void SyntaxNodeAction(SyntaxNodeAnalysisContext syntaxContext, INamedTyp
return;
}
// If there are compiler error on the declaration we can't reliably
// tell that the refactoring will be accurate, so don't provide any
// code diagnostics
if (localDeclaration.GetDiagnostics().Any(d => d.Severity == DiagnosticSeverity.Error))
{
return;
}
var local = semanticModel.GetDeclaredSymbol(localDeclaration.Declaration.Variables[0], cancellationToken);
if (local == null)
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册