Skip to content

Commit

Permalink
Recommend 'this' inside local functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv committed Jun 10, 2018
1 parent 66a6aac commit 3a06d09
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,79 @@ await VerifyKeywordAsync(
static int Goo($$");
}

[Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
[WorkItem(27028, "https://github.com/dotnet/roslyn/issues/27028")]
public async Task TestInLocalFunction()
{
await VerifyKeywordAsync(
@"class C
{
int Method()
{
void local()
{
$$
}
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
[WorkItem(27028, "https://github.com/dotnet/roslyn/issues/27028")]
public async Task TestInNestedLocalFunction()
{
await VerifyKeywordAsync(
@"class C
{
int Method()
{
void local()
{
void nested()
{
$$
}
}
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
[WorkItem(27028, "https://github.com/dotnet/roslyn/issues/27028")]
public async Task TestInLocalFunctionInStaticMethod()
{
await VerifyAbsenceAsync(
@"class C {
static int Method()
{
void local()
{
$$
}
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
[WorkItem(27028, "https://github.com/dotnet/roslyn/issues/27028")]
public async Task TestInNestedLocalFunctionInStaticMethod()
{
await VerifyAbsenceAsync(
@"class C
{
static int Method()
{
void local()
{
void nested()
{
$$
}
}
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.KeywordRecommending)]
public async Task TestAfterAttribute()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,13 @@ public static bool IsInstanceContext(this SyntaxTree syntaxTree, SyntaxToken tar
#endif

var enclosingSymbol = semanticModel.GetEnclosingSymbol(targetToken.SpanStart, cancellationToken);

while (enclosingSymbol is IMethodSymbol method && method.MethodKind == MethodKind.LocalFunction)
{
// It is allowed to reference the instance (`this`) within a local function, as long as the containing method allows it
enclosingSymbol = method.ContainingSymbol;
}

return !enclosingSymbol.IsStatic;
}

Expand Down

0 comments on commit 3a06d09

Please sign in to comment.