Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle nameof expressions in UnboundIdentifiersDiagnosticAnalyzerBase #24071

Merged
merged 2 commits into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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