Skip to content

Commit

Permalink
Merge pull request #24071 from mavasani/NameOfFix
Browse files Browse the repository at this point in the history
Handle nameof expressions in UnboundIdentifiersDiagnosticAnalyzerBase
  • Loading branch information
mavasani authored Jan 18, 2019
2 parents fc965b5 + f25103a commit 6b7e1fc
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,22 @@ class Program
{
public async Task<IReadOnlyCollection<ProjectConfiguration>>
}
}");
}

[WorkItem(23667, "https://github.com/dotnet/roslyn/issues/23667")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddImport)]
public async Task TestMissingDiagnosticForNameOf()
{
await TestDiagnosticMissingAsync(
@"using System;
class C
{
Action action = () => {
var x = [|nameof|](System);
#warning xxx
};
}");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2606,5 +2606,21 @@ Class C
Dim s As Action = Sub()
Dim a = New Test()")
End Function

<WorkItem(23667, "https://github.com/dotnet/roslyn/issues/23667")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddImport)>
Public Async Function TestMissingDiagnosticForNameOf() As Task
Await TestDiagnosticMissingAsync(
"Imports System

Class Class1
Sub M()
Dim a As Action = Sub()
Dim x = [|NameOf|](System)
Dim x2
End Function
End Sub
Extension")
End Function
End Class
End Namespace
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Immutable;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Diagnostics.AddImport;
Expand Down Expand Up @@ -63,5 +64,7 @@ protected override bool ConstructorDoesNotExist(SyntaxNode node, SymbolInfo info

return false;
}

protected override bool IsNameOf(SyntaxNode node) => node.Parent is InvocationExpressionSyntax invocation && invocation.IsNameOfInvocation();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal abstract class UnboundIdentifiersDiagnosticAnalyzerBase<TLanguageKindEn
protected abstract DiagnosticDescriptor DiagnosticDescriptor2 { get; }
protected abstract ImmutableArray<TLanguageKindEnum> SyntaxKindsOfInterest { get; }
protected abstract bool ConstructorDoesNotExist(SyntaxNode node, SymbolInfo info, SemanticModel semanticModel);
protected abstract bool IsNameOf(SyntaxNode node);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(DiagnosticDescriptor, DiagnosticDescriptor2);
public bool OpenFileOnly(Workspace workspace) => false;
Expand Down Expand Up @@ -73,6 +74,12 @@ private void ReportUnboundIdentifierNames(SyntaxNodeAnalysisContext context, Syn
var info = context.SemanticModel.GetSymbolInfo(typeName);
if (info.Symbol == null && info.CandidateSymbols.Length == 0)
{
// GetSymbolInfo returns no symbols for "nameof" expression, so handle it specially.
if (IsNameOf(typeName))
{
continue;
}

context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptor, typeName.GetLocation(), typeName.ToString()));
}
else if (ConstructorDoesNotExist(typeName, info, context.SemanticModel))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Diagnostics

Return False
End Function

Protected Overrides Function IsNameOf(node As SyntaxNode) As Boolean
Return node.Kind() = SyntaxKind.NameOfKeyword
End Function
End Class
End Namespace

0 comments on commit 6b7e1fc

Please sign in to comment.