提交 5e66b24b 编写于 作者: M Manish Vasani

Handle nameof expressions in UnboundIdentifiersDiagnosticAnalyzerBase

Fixes #23667
上级 adbce761
......@@ -283,6 +283,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
};
}");
}
}
......
......@@ -42,13 +42,7 @@ public abstract partial class AbstractUserDiagnosticTest : AbstractCodeActionOrU
protected override async Task<ImmutableArray<Diagnostic>> GetDiagnosticsWorkerAsync(TestWorkspace workspace, TestParameters parameters)
{
var diagnosticsAndCodeFixes = await GetDiagnosticAndFixAsync(workspace, parameters);
if (diagnosticsAndCodeFixes == null)
{
return ImmutableArray<Diagnostic>.Empty;
}
return ImmutableArray.Create(diagnosticsAndCodeFixes.Item1);
return (await GetDiagnosticsAsync(workspace, parameters)).AsImmutable();
}
internal async Task<Tuple<Diagnostic, CodeFixCollection>> GetDiagnosticAndFixAsync(
......
......@@ -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
// 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;
......@@ -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();
}
}
......@@ -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;
......@@ -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))
......
......@@ -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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册