提交 54d9890c 编写于 作者: D David Poeschl

Remove work for SymbolKind.Local based on offline discussions

上级 5a0f4536
......@@ -1657,15 +1657,13 @@ public partial class C33 { }
}
[Fact, WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")]
public void TestLocalsAndParametersAnalyzer_InConstructor()
public void TestParametersAnalyzer_InConstructor()
{
string source = @"
public class C
{
public C(int a, int b)
{
int c = 0, d = 0;
System.Console.WriteLine(c + d);
}
}
";
......@@ -1673,24 +1671,20 @@ public C(int a, int b)
var compilation = CreateCompilationWithMscorlib45(new[] { tree });
compilation.VerifyDiagnostics();
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForLocalsAndParameters() };
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForParameters() };
compilation.VerifyAnalyzerDiagnostics(analyzers, null, null, true,
Diagnostic("Parameter_ID", "a").WithLocation(4, 18),
Diagnostic("Parameter_ID", "b").WithLocation(4, 25),
Diagnostic("Local_ID", "c").WithLocation(6, 13),
Diagnostic("Local_ID", "d").WithLocation(6, 20));
Diagnostic("Parameter_ID", "b").WithLocation(4, 25));
}
[Fact, WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")]
public void TestLocalsAndParametersAnalyzer_InRegularMethod()
public void TestParametersAnalyzer_InRegularMethod()
{
string source = @"
public class C
{
void M1(string a, string b)
{
C c = null;
System.Console.WriteLine(c);
}
}
";
......@@ -1698,22 +1692,22 @@ void M1(string a, string b)
var compilation = CreateCompilationWithMscorlib45(new[] { tree });
compilation.VerifyDiagnostics();
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForLocalsAndParameters() };
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForParameters() };
compilation.VerifyAnalyzerDiagnostics(analyzers, null, null, true,
Diagnostic("Parameter_ID", "a").WithLocation(4, 20),
Diagnostic("Parameter_ID", "b").WithLocation(4, 30),
Diagnostic("Local_ID", "c").WithLocation(6, 11));
Diagnostic("Parameter_ID", "b").WithLocation(4, 30));
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/14061"), WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")]
public void TestLocalsAndParametersAnalyzer_Lambdas()
[Fact, WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")]
public void TestParametersAnalyzer_InIndexers()
{
string source = @"
public class C
{
void M2()
public int this[int index]
{
System.Func<int, int, int> x = (int a, int b) => b;
get { return 0; }
set { }
}
}
";
......@@ -1721,62 +1715,61 @@ void M2()
var compilation = CreateCompilationWithMscorlib45(new[] { tree });
compilation.VerifyDiagnostics();
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForLocalsAndParameters() };
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForParameters() };
compilation.VerifyAnalyzerDiagnostics(analyzers, null, null, true,
Diagnostic("Local_ID", "x").WithLocation(6, 36),
Diagnostic("Parameter_ID", "a").WithLocation(6, 45),
Diagnostic("Parameter_ID", "b").WithLocation(6, 52));
Diagnostic("Parameter_ID", "index").WithLocation(4, 25));
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/14061"), WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")]
public void TestLocalsAndParametersAnalyzer_InAnonymousMethods()
public void TestParametersAnalyzer_Lambdas()
{
string source = @"
public class C
{
void M3()
void M2()
{
M4(delegate (int x, int y) { });
System.Func<int, int, int> x = (int a, int b) => b;
}
void M4(System.Action<int, int> a) { }
}
";
var tree = CSharpSyntaxTree.ParseText(source, path: "Source.cs");
var compilation = CreateCompilationWithMscorlib45(new[] { tree });
compilation.VerifyDiagnostics();
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForLocalsAndParameters() };
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForParameters() };
compilation.VerifyAnalyzerDiagnostics(analyzers, null, null, true,
Diagnostic("Parameter_ID", "a").WithLocation(9, 37),
Diagnostic("Parameter_ID", "x").WithLocation(6, 26),
Diagnostic("Parameter_ID", "y").WithLocation(6, 33));
Diagnostic("Local_ID", "x").WithLocation(6, 36),
Diagnostic("Parameter_ID", "a").WithLocation(6, 45),
Diagnostic("Parameter_ID", "b").WithLocation(6, 52));
}
[Fact, WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")]
public void TestLocalsAndParametersAnalyzer_InIndexers()
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/14061"), WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")]
public void TestParametersAnalyzer_InAnonymousMethods()
{
string source = @"
public class C
{
public int this[int index]
void M3()
{
get { return 0; }
set { }
M4(delegate (int x, int y) { });
}
void M4(System.Action<int, int> a) { }
}
";
var tree = CSharpSyntaxTree.ParseText(source, path: "Source.cs");
var compilation = CreateCompilationWithMscorlib45(new[] { tree });
compilation.VerifyDiagnostics();
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForLocalsAndParameters() };
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForParameters() };
compilation.VerifyAnalyzerDiagnostics(analyzers, null, null, true,
Diagnostic("Parameter_ID", "index").WithLocation(4, 25));
Diagnostic("Parameter_ID", "a").WithLocation(9, 37),
Diagnostic("Parameter_ID", "x").WithLocation(6, 26),
Diagnostic("Parameter_ID", "y").WithLocation(6, 33));
}
[Fact, WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")]
public void TestLocalsAndParametersAnalyzer_InDelegateTypes()
public void TestParametersAnalyzer_InDelegateTypes()
{
string source = @"
public class C
......@@ -1788,14 +1781,14 @@ public class C
var compilation = CreateCompilationWithMscorlib45(new[] { tree });
compilation.VerifyDiagnostics();
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForLocalsAndParameters() };
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForParameters() };
compilation.VerifyAnalyzerDiagnostics(analyzers, null, null, true,
Diagnostic("Parameter_ID", "x").WithLocation(4, 25),
Diagnostic("Parameter_ID", "y").WithLocation(4, 35));
}
[Fact, WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")]
public void TestLocalsAndParametersAnalyzer_InOperators()
public void TestParametersAnalyzer_InOperators()
{
string source = @"
public class C
......@@ -1807,13 +1800,13 @@ public class C
var compilation = CreateCompilationWithMscorlib45(new[] { tree });
compilation.VerifyDiagnostics();
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForLocalsAndParameters() };
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForParameters() };
compilation.VerifyAnalyzerDiagnostics(analyzers, null, null, true,
Diagnostic("Parameter_ID", "c").WithLocation(4, 44));
}
[Fact, WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")]
public void TestLocalsAndParametersAnalyzer_InExplicitInterfaceImplementations()
public void TestParametersAnalyzer_InExplicitInterfaceImplementations()
{
string source = @"
interface I
......@@ -1830,7 +1823,7 @@ public class C : I
var compilation = CreateCompilationWithMscorlib45(new[] { tree });
compilation.VerifyDiagnostics();
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForLocalsAndParameters() };
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForParameters() };
compilation.VerifyAnalyzerDiagnostics(analyzers, null, null, true,
Diagnostic("Parameter_ID", "c").WithLocation(9, 18),
Diagnostic("Parameter_ID", "d").WithLocation(9, 25),
......@@ -1839,7 +1832,7 @@ public class C : I
}
[Fact, WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")]
public void TestLocalsAndParametersAnalyzer_InExtensionMethods()
public void TestParametersAnalyzer_InExtensionMethods()
{
string source = @"
public static class C
......@@ -1851,14 +1844,14 @@ public static class C
var compilation = CreateCompilationWithMscorlib45(new[] { tree });
compilation.VerifyDiagnostics();
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForLocalsAndParameters() };
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForParameters() };
compilation.VerifyAnalyzerDiagnostics(analyzers, null, null, true,
Diagnostic("Parameter_ID", "x").WithLocation(4, 28),
Diagnostic("Parameter_ID", "y").WithLocation(4, 35));
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/14061"), WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")]
public void TestLocalsAndParametersAnalyzer_InLocalFunctions()
public void TestParametersAnalyzer_InLocalFunctions()
{
string source = @"
public class C
......@@ -1877,7 +1870,7 @@ void M2(int a, int b)
var compilation = CreateCompilationWithMscorlib45(new[] { tree });
compilation.VerifyDiagnostics();
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForLocalsAndParameters() };
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerForParameters() };
compilation.VerifyAnalyzerDiagnostics(analyzers, null, null, true,
Diagnostic("Parameter_ID", "a").WithLocation(4, 18), // ctor
Diagnostic("Parameter_ID", "b").WithLocation(4, 25),
......
......@@ -1200,8 +1200,6 @@ public struct OperationAnalysisContext
/// </summary>
public CancellationToken CancellationToken => _cancellationToken;
internal Func<Diagnostic, bool> IsSupportedDiagnostic => _isSupportedDiagnostic;
public OperationAnalysisContext(IOperation operation, ISymbol containingSymbol, Compilation compilation, AnalyzerOptions options, Action<Diagnostic> reportDiagnostic, Func<Diagnostic, bool> isSupportedDiagnostic, CancellationToken cancellationToken)
{
_operation = operation;
......
......@@ -609,27 +609,10 @@ public void RegisterSymbolAction(DiagnosticAnalyzer analyzer, Action<SymbolAnaly
this.GetOrCreateAnalyzerActions(analyzer).AddSymbolAction(analyzerAction);
_symbolActions = _symbolActions.Add(analyzerAction);
// The SymbolAnalyzerAction does not handle SymbolKind.Local or SymbolKind.Parameter
// because the compiler does not make CompilationEvents for them. As a workaround,
// handle them specially by registering further OperationActions (for
// VariableDeclarations) or SymbolActions (for Methods) and utilize the results of
// those to construct the necessary SymbolAnalysisContexts.
if (symbolKinds.Contains(SymbolKind.Local))
{
RegisterOperationAction(analyzer, opContext =>
{
var varDecl = (IVariableDeclaration)opContext.Operation;
action(new SymbolAnalysisContext(
varDecl.Variable,
opContext.Compilation,
opContext.Options,
opContext.ReportDiagnostic,
opContext.IsSupportedDiagnostic,
opContext.CancellationToken));
},
ImmutableArray.Create(OperationKind.VariableDeclaration));
}
// The SymbolAnalyzerAction does not handle SymbolKind.Parameter because the compiler
// does not make CompilationEvents for them. As a workaround, handle them specially by
// registering further SymbolActions (for Methods) and utilize the results to construct
// the necessary SymbolAnalysisContexts.
if (symbolKinds.Contains(SymbolKind.Parameter))
{
......
......@@ -982,7 +982,7 @@ End Class
End Sub
<Fact, WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")>
Public Sub TestLocalsAndParametersAnalyzer_InRegularMethods()
Public Sub TestParametersAnalyzer_InRegularMethods()
Dim source = <compilation>
<file name="c.vb">
<![CDATA[
......@@ -996,13 +996,13 @@ End Class
Dim comp = CreateCompilationWithMscorlibAndVBRuntime(source)
comp.VerifyDiagnostics()
comp.VerifyAnalyzerDiagnostics({New AnalyzerForLocalsAndParameters}, Nothing, Nothing, False,
comp.VerifyAnalyzerDiagnostics({New AnalyzerForParameters}, Nothing, Nothing, False,
Diagnostic("Parameter_ID", "a").WithLocation(2, 18),
Diagnostic("Parameter_ID", "b").WithLocation(2, 32))
End Sub
<Fact, WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")>
Public Sub TestLocalsAndParametersAnalyzer_InConstructors()
Public Sub TestParametersAnalyzer_InConstructors()
Dim source = <compilation>
<file name="c.vb">
<![CDATA[
......@@ -1016,13 +1016,13 @@ End Class
Dim comp = CreateCompilationWithMscorlibAndVBRuntime(source)
comp.VerifyDiagnostics()
comp.VerifyAnalyzerDiagnostics({New AnalyzerForLocalsAndParameters}, Nothing, Nothing, False,
comp.VerifyAnalyzerDiagnostics({New AnalyzerForParameters}, Nothing, Nothing, False,
Diagnostic("Parameter_ID", "a").WithLocation(2, 20),
Diagnostic("Parameter_ID", "b").WithLocation(2, 34))
End Sub
<Fact, WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")>
Public Sub TestLocalsAndParametersAnalyzer_InIndexers()
Public Sub TestParametersAnalyzer_InIndexers()
Dim source = <compilation>
<file name="c.vb">
<![CDATA[
......@@ -1041,14 +1041,14 @@ End Class
Dim comp = CreateCompilationWithMscorlibAndVBRuntime(source)
comp.VerifyDiagnostics()
comp.VerifyAnalyzerDiagnostics({New AnalyzerForLocalsAndParameters}, Nothing, Nothing, False,
comp.VerifyAnalyzerDiagnostics({New AnalyzerForParameters}, Nothing, Nothing, False,
Diagnostic("Parameter_ID", "a").WithLocation(2, 34),
Diagnostic("Parameter_ID", "b").WithLocation(2, 48),
Diagnostic("Parameter_ID", "Value").WithLocation(6, 19))
End Sub
<Fact(Skip:="https://github.com/dotnet/roslyn/issues/14062"), WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")>
Public Sub TestLocalsAndParametersAnalyzer_InDelegateTypes()
Public Sub TestParametersAnalyzer_InDelegateTypes()
Dim source = <compilation>
<file name="c.vb">
<![CDATA[
......@@ -1061,13 +1061,13 @@ End Class
Dim comp = CreateCompilationWithMscorlibAndVBRuntime(source)
comp.VerifyDiagnostics()
comp.VerifyAnalyzerDiagnostics({New AnalyzerForLocalsAndParameters}, Nothing, Nothing, False,
comp.VerifyAnalyzerDiagnostics({New AnalyzerForParameters}, Nothing, Nothing, False,
Diagnostic("Parameter_ID", "a").WithLocation(2, 34),
Diagnostic("Parameter_ID", "b").WithLocation(2, 48))
End Sub
<Fact, WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")>
Public Sub TestLocalsAndParametersAnalyzer_InOperators()
Public Sub TestParametersAnalyzer_InOperators()
Dim source = <compilation>
<file name="c.vb">
<![CDATA[
......@@ -1082,13 +1082,13 @@ End Class
Dim comp = CreateCompilationWithMscorlibAndVBRuntime(source)
comp.VerifyDiagnostics()
comp.VerifyAnalyzerDiagnostics({New AnalyzerForLocalsAndParameters}, Nothing, Nothing, False,
comp.VerifyAnalyzerDiagnostics({New AnalyzerForParameters}, Nothing, Nothing, False,
Diagnostic("Parameter_ID", "h1").WithLocation(2, 36),
Diagnostic("Parameter_ID", "h2").WithLocation(2, 51))
End Sub
<Fact, WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")>
Public Sub TestLocalsAndParametersAnalyzer_InInterfaceImplementations()
Public Sub TestParametersAnalyzer_InInterfaceImplementations()
Dim source = <compilation>
<file name="c.vb">
<![CDATA[
......@@ -1107,7 +1107,7 @@ End Class
Dim comp = CreateCompilationWithMscorlibAndVBRuntime(source)
comp.VerifyDiagnostics()
comp.VerifyAnalyzerDiagnostics({New AnalyzerForLocalsAndParameters}, Nothing, Nothing, False,
comp.VerifyAnalyzerDiagnostics({New AnalyzerForParameters}, Nothing, Nothing, False,
Diagnostic("Parameter_ID", "a").WithLocation(2, 11),
Diagnostic("Parameter_ID", "b").WithLocation(2, 25),
Diagnostic("Parameter_ID", "a").WithLocation(7, 18),
......@@ -1115,7 +1115,7 @@ End Class
End Sub
<Fact, WorkItem(8753, "https://github.com/dotnet/roslyn/issues/8753")>
Public Sub TestLocalsAndParametersAnalyzer_InParameterizedProperties()
Public Sub TestParametersAnalyzer_InParameterizedProperties()
Dim source = <compilation>
<file name="c.vb">
<![CDATA[
......@@ -1132,7 +1132,7 @@ End Class
Dim comp = CreateCompilationWithMscorlibAndVBRuntime(source)
comp.VerifyDiagnostics()
comp.VerifyAnalyzerDiagnostics({New AnalyzerForLocalsAndParameters}, Nothing, Nothing, False,
comp.VerifyAnalyzerDiagnostics({New AnalyzerForParameters}, Nothing, Nothing, False,
Diagnostic("Parameter_ID", "a").WithLocation(2, 35),
Diagnostic("Parameter_ID", "b").WithLocation(2, 49))
End Sub
......
......@@ -1069,16 +1069,8 @@ private void OnCompilationStart(CompilationStartAnalysisContext context)
}
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public class AnalyzerForLocalsAndParameters : DiagnosticAnalyzer
public class AnalyzerForParameters : DiagnosticAnalyzer
{
public static readonly DiagnosticDescriptor LocalDescriptor = new DiagnosticDescriptor(
"Local_ID",
"Local_Title",
"Local_Message",
"Local_Category",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);
public static readonly DiagnosticDescriptor ParameterDescriptor = new DiagnosticDescriptor(
"Parameter_ID",
"Parameter_Title",
......@@ -1087,20 +1079,16 @@ public class AnalyzerForLocalsAndParameters : DiagnosticAnalyzer
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(LocalDescriptor, ParameterDescriptor);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(ParameterDescriptor);
public override void Initialize(AnalysisContext context)
{
context.RegisterSymbolAction(SymbolAction, SymbolKind.Parameter, SymbolKind.Local);
context.RegisterSymbolAction(SymbolAction, SymbolKind.Parameter);
}
private void SymbolAction(SymbolAnalysisContext context)
{
var diagnostic = context.Symbol.Kind == SymbolKind.Parameter
? Diagnostic.Create(ParameterDescriptor, context.Symbol.Locations[0])
: Diagnostic.Create(LocalDescriptor, context.Symbol.Locations[0]);
context.ReportDiagnostic(diagnostic);
context.ReportDiagnostic(Diagnostic.Create(ParameterDescriptor, context.Symbol.Locations[0]));
}
}
}
......
......@@ -77,7 +77,7 @@ public void VerifyAnalyzeSymbolCalledForAllSymbolKinds()
{
var expectedSymbolKinds = new[]
{
SymbolKind.Event, SymbolKind.Field, SymbolKind.Local, SymbolKind.Method, SymbolKind.NamedType, SymbolKind.Namespace, SymbolKind.Parameter, SymbolKind.Property
SymbolKind.Event, SymbolKind.Field, SymbolKind.Method, SymbolKind.NamedType, SymbolKind.Namespace, SymbolKind.Parameter, SymbolKind.Property
};
var actualSymbolKinds = _callLog.Where(a => FilterByAbstractName(a, "Symbol")).Where(e => e.SymbolKind.HasValue).Select(e => e.SymbolKind.Value).Distinct();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册