提交 6edcd367 编写于 作者: M Manish Vasani

Report AD0001 if an analyzer accidentally provides a null descriptor in its SupportedDiagnostics

Fixes #30453
上级 b1ab1b05
......@@ -889,6 +889,22 @@ public void TestReportingDiagnosticWithInvalidId()
.WithLocation(1, 1));
}
[Fact, WorkItem(30453, "https://github.com/dotnet/roslyn/issues/30453")]
public void TestAnalyzerWithNullDescriptor()
{
string source = @"";
var analyzers = new DiagnosticAnalyzer[] { new AnalyzerWithNullDescriptor() };
var analyzerFullName = "Microsoft.CodeAnalysis.CommonDiagnosticAnalyzers+AnalyzerWithNullDescriptor";
string message = new ArgumentException(string.Format(CodeAnalysisResources.SupportedDiagnosticsHasNullDescriptor, analyzerFullName), "SupportedDiagnostics").Message;
CreateCompilationWithMscorlib45(source)
.VerifyDiagnostics()
.VerifyAnalyzerDiagnostics(analyzers, null, null, logAnalyzerExceptionAsDiagnostics: true,
expected: Diagnostic("AD0001")
.WithArguments(analyzerFullName, "System.ArgumentException", message)
.WithLocation(1, 1));
}
[Fact, WorkItem(25748, "https://github.com/dotnet/roslyn/issues/25748")]
public void TestReportingDiagnosticWithCSharpCompilerId()
{
......
......@@ -1387,6 +1387,15 @@ internal class CodeAnalysisResources {
}
}
/// <summary>
/// Looks up a localized string similar to Analyzer &apos;{0}&apos; contains a null descriptor in its &apos;SupportedDiagnostics&apos;..
/// </summary>
internal static string SupportedDiagnosticsHasNullDescriptor {
get {
return ResourceManager.GetString("SupportedDiagnosticsHasNullDescriptor", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Windows PDB writer doesn&apos;t support SourceLink feature: &apos;{0}&apos;.
/// </summary>
......
......@@ -449,6 +449,9 @@
<data name="InvalidDiagnosticLocationReported" xml:space="preserve">
<value>Reported diagnostic '{0}' has a source location in file '{1}', which is not part of the compilation being analyzed.</value>
</data>
<data name="SupportedDiagnosticsHasNullDescriptor" xml:space="preserve">
<value>Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</value>
</data>
<data name="The_type_0_is_not_understood_by_the_serialization_binder" xml:space="preserve">
<value>The type '{0}' is not understood by the serialization binder.</value>
</data>
......
......@@ -260,6 +260,15 @@ public ImmutableArray<DiagnosticDescriptor> GetOrComputeDescriptors(DiagnosticAn
var supportedDiagnosticsLocal = analyzer.SupportedDiagnostics;
if (!supportedDiagnosticsLocal.IsDefaultOrEmpty)
{
foreach (var descriptor in supportedDiagnosticsLocal)
{
if (descriptor == null)
{
// Disallow null descriptors.
throw new ArgumentException(string.Format(CodeAnalysisResources.SupportedDiagnosticsHasNullDescriptor, analyzer.ToString()), nameof(DiagnosticAnalyzer.SupportedDiagnostics));
}
}
supportedDiagnostics = supportedDiagnosticsLocal;
}
},
......
......@@ -59,6 +59,11 @@
<target state="translated">Kopie modulu se nedá použít k vytvoření metadat sestavení.</target>
<note />
</trans-unit>
<trans-unit id="SupportedDiagnosticsHasNullDescriptor">
<source>Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</source>
<target state="new">Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</target>
<note />
</trans-unit>
<trans-unit id="Unresolved">
<source>Unresolved: </source>
<target state="translated">Nevyřešeno: </target>
......
......@@ -59,6 +59,11 @@
<target state="translated">Modulkopie kann nicht zum Erstellen von Assembly-Metadaten verwendet werden.</target>
<note />
</trans-unit>
<trans-unit id="SupportedDiagnosticsHasNullDescriptor">
<source>Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</source>
<target state="new">Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</target>
<note />
</trans-unit>
<trans-unit id="Unresolved">
<source>Unresolved: </source>
<target state="translated">Nicht aufgelöst: </target>
......
......@@ -59,6 +59,11 @@
<target state="translated">No se puede usar una copia de módulo para crear metadatos de ensamblado.</target>
<note />
</trans-unit>
<trans-unit id="SupportedDiagnosticsHasNullDescriptor">
<source>Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</source>
<target state="new">Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</target>
<note />
</trans-unit>
<trans-unit id="Unresolved">
<source>Unresolved: </source>
<target state="translated">Sin resolver: </target>
......
......@@ -59,6 +59,11 @@
<target state="translated">Impossible d'utiliser le module de copie pour créer des métadonnées d'assembly.</target>
<note />
</trans-unit>
<trans-unit id="SupportedDiagnosticsHasNullDescriptor">
<source>Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</source>
<target state="new">Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</target>
<note />
</trans-unit>
<trans-unit id="Unresolved">
<source>Unresolved: </source>
<target state="translated">Non résolu : </target>
......
......@@ -59,6 +59,11 @@
<target state="translated">Non è possibile usare la copia del modulo per creare i metadati di un assembly.</target>
<note />
</trans-unit>
<trans-unit id="SupportedDiagnosticsHasNullDescriptor">
<source>Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</source>
<target state="new">Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</target>
<note />
</trans-unit>
<trans-unit id="Unresolved">
<source>Unresolved: </source>
<target state="translated">Non risolto: </target>
......
......@@ -59,6 +59,11 @@
<target state="translated">モジュールのコピーは、アセンブリ メタデータの作成には使用できません。</target>
<note />
</trans-unit>
<trans-unit id="SupportedDiagnosticsHasNullDescriptor">
<source>Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</source>
<target state="new">Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</target>
<note />
</trans-unit>
<trans-unit id="Unresolved">
<source>Unresolved: </source>
<target state="translated">未解決: </target>
......
......@@ -59,6 +59,11 @@
<target state="translated">모듈 복사본은 어셈블리 메타데이터를 만드는 데 사용할 수 없습니다.</target>
<note />
</trans-unit>
<trans-unit id="SupportedDiagnosticsHasNullDescriptor">
<source>Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</source>
<target state="new">Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</target>
<note />
</trans-unit>
<trans-unit id="Unresolved">
<source>Unresolved: </source>
<target state="translated">확인되지 않음: </target>
......
......@@ -59,6 +59,11 @@
<target state="translated">Nie można użyć kopii modułu do utworzenia metadanych zestawu.</target>
<note />
</trans-unit>
<trans-unit id="SupportedDiagnosticsHasNullDescriptor">
<source>Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</source>
<target state="new">Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</target>
<note />
</trans-unit>
<trans-unit id="Unresolved">
<source>Unresolved: </source>
<target state="translated">Nierozpoznane: </target>
......
......@@ -59,6 +59,11 @@
<target state="translated">Cópia de módulo não pode ser usada para criar metadados do assembly.</target>
<note />
</trans-unit>
<trans-unit id="SupportedDiagnosticsHasNullDescriptor">
<source>Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</source>
<target state="new">Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</target>
<note />
</trans-unit>
<trans-unit id="Unresolved">
<source>Unresolved: </source>
<target state="translated">Não resolvido: </target>
......
......@@ -59,6 +59,11 @@
<target state="translated">Копию модуля нельзя использовать для создания метаданных сборки.</target>
<note />
</trans-unit>
<trans-unit id="SupportedDiagnosticsHasNullDescriptor">
<source>Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</source>
<target state="new">Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</target>
<note />
</trans-unit>
<trans-unit id="Unresolved">
<source>Unresolved: </source>
<target state="translated">Не разрешено:</target>
......
......@@ -59,6 +59,11 @@
<target state="translated">Derleme meta verileri oluşturmak için modül kopyası kullanılamaz.</target>
<note />
</trans-unit>
<trans-unit id="SupportedDiagnosticsHasNullDescriptor">
<source>Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</source>
<target state="new">Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</target>
<note />
</trans-unit>
<trans-unit id="Unresolved">
<source>Unresolved: </source>
<target state="translated">Çözümlenmemiş: </target>
......
......@@ -59,6 +59,11 @@
<target state="translated">模块复制不能用于创建程序集元数据。</target>
<note />
</trans-unit>
<trans-unit id="SupportedDiagnosticsHasNullDescriptor">
<source>Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</source>
<target state="new">Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</target>
<note />
</trans-unit>
<trans-unit id="Unresolved">
<source>Unresolved: </source>
<target state="translated">未解析:</target>
......
......@@ -59,6 +59,11 @@
<target state="translated">不可使用模組複本建立組件中繼資料。</target>
<note />
</trans-unit>
<trans-unit id="SupportedDiagnosticsHasNullDescriptor">
<source>Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</source>
<target state="new">Analyzer '{0}' contains a null descriptor in its 'SupportedDiagnostics'.</target>
<note />
</trans-unit>
<trans-unit id="Unresolved">
<source>Unresolved: </source>
<target state="translated">未解析: </target>
......
......@@ -463,6 +463,16 @@ public override void Initialize(AnalysisContext context)
}
}
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed class AnalyzerWithNullDescriptor : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create((DiagnosticDescriptor)null);
public override void Initialize(AnalysisContext context)
{
context.RegisterCompilationAction(_ => { });
}
}
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed class AnalyzerWithCSharpCompilerDiagnosticId : DiagnosticAnalyzer
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册