Browse Source

ExportAnalyzerAttribute: Simplify pattern matching and avoid multiple enumeration

pull/3243/head
Siegfried Pammer 1 year ago
parent
commit
fe879869c3
  1. 3
      ICSharpCode.ILSpyX/Analyzers/ExportAnalyzerAttribute.cs
  2. 4
      ILSpy.Tests/Analyzers/ExportAnalyzerAttributeTests.cs

3
ICSharpCode.ILSpyX/Analyzers/ExportAnalyzerAttribute.cs

@ -39,8 +39,7 @@ namespace ICSharpCode.ILSpyX.Analyzers
{ {
foreach (var type in typeof(ExportAnalyzerAttribute).Assembly.GetTypes()) foreach (var type in typeof(ExportAnalyzerAttribute).Assembly.GetTypes())
{ {
var exportAnalyzerAttribute = type.GetCustomAttribute(typeof(ExportAnalyzerAttribute), false) as ExportAnalyzerAttribute; if (type.GetCustomAttribute(typeof(ExportAnalyzerAttribute), false) is ExportAnalyzerAttribute exportAnalyzerAttribute)
if (exportAnalyzerAttribute is not null)
{ {
yield return (exportAnalyzerAttribute, type); yield return (exportAnalyzerAttribute, type);
} }

4
ILSpy.Tests/Analyzers/ExportAnalyzerAttributeTests.cs

@ -30,7 +30,9 @@ namespace ICSharpCode.ILSpy.Tests.Analyzers
[Test] [Test]
public void CollectAnalyzers() public void CollectAnalyzers()
{ {
var analyzerNames = ExportAnalyzerAttribute.GetAnnotatedAnalyzers().Select(analyzer => analyzer.AnalyzerType.Name); var analyzerNames = ExportAnalyzerAttribute.GetAnnotatedAnalyzers()
.Select(analyzer => analyzer.AnalyzerType.Name)
.ToArray();
Assert.That(analyzerNames.Contains("AttributeAppliedToAnalyzer")); Assert.That(analyzerNames.Contains("AttributeAppliedToAnalyzer"));
Assert.That(analyzerNames.Contains("EventImplementedByAnalyzer")); Assert.That(analyzerNames.Contains("EventImplementedByAnalyzer"));
Assert.That(analyzerNames.Contains("MethodUsedByAnalyzer")); Assert.That(analyzerNames.Contains("MethodUsedByAnalyzer"));

Loading…
Cancel
Save