diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/AnalyzerReleases.Unshipped.md b/src/Microsoft.CodeAnalysis.Analyzers/Core/AnalyzerReleases.Unshipped.md index 8b15c08ff9..247d6bd077 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Core/AnalyzerReleases.Unshipped.md +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/AnalyzerReleases.Unshipped.md @@ -7,3 +7,4 @@ Rule ID | Category | Severity | Notes RS1038 | MicrosoftCodeAnalysisCorrectness | Warning | CompilerExtensionStrictApiAnalyzer RS1041 | MicrosoftCodeAnalysisCorrectness | Warning | CompilerExtensionTargetFrameworkAnalyzer, [Documentation](https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1041.md) RS1042 | MicrosoftCodeAnalysisCompatibility | Error | ImplementationIsObsoleteAnalyzer +RS1043 | MicrosoftCodeAnalysisCorrectness | Error | DoNotUseFileTypesForAnalyzersOrGenerators diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/CodeAnalysisDiagnosticsResources.resx b/src/Microsoft.CodeAnalysis.Analyzers/Core/CodeAnalysisDiagnosticsResources.resx index e9174ddb36..6d12c43305 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Core/CodeAnalysisDiagnosticsResources.resx +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/CodeAnalysisDiagnosticsResources.resx @@ -605,4 +605,13 @@ Implementations of this interface are not allowed + + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + + + Type '{0}' should not be marked with 'file' + + + Do not use file types for implementing analyzers, generators, and code fixers + diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/DiagnosticIds.cs b/src/Microsoft.CodeAnalysis.Analyzers/Core/DiagnosticIds.cs index daf31d6769..d583cfb963 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Core/DiagnosticIds.cs +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/DiagnosticIds.cs @@ -46,6 +46,7 @@ internal static class DiagnosticIds public const string SemanticModelGetDeclaredSymbolAlwaysReturnsNullForField = "RS1040"; public const string DoNotRegisterCompilerTypesWithBadTargetFrameworkRuleId = "RS1041"; public const string ImplementationIsObsoleteRuleId = "RS1042"; + public const string DoNotUseFileTypesForAnalyzersOrGenerators = "RS1043"; // Release tracking analyzer IDs public const string DeclareDiagnosticIdInAnalyzerReleaseRuleId = "RS2000"; diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/MetaAnalyzers/DoNotUseFileTypesForAnalyzersOrGenerators.cs b/src/Microsoft.CodeAnalysis.Analyzers/Core/MetaAnalyzers/DoNotUseFileTypesForAnalyzersOrGenerators.cs new file mode 100644 index 0000000000..ddd01f1a62 --- /dev/null +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/MetaAnalyzers/DoNotUseFileTypesForAnalyzersOrGenerators.cs @@ -0,0 +1,67 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. + +using System.Collections.Immutable; +using Analyzer.Utilities; +using Analyzer.Utilities.Extensions; +using Microsoft.CodeAnalysis.Diagnostics; + +namespace Microsoft.CodeAnalysis.Analyzers.MetaAnalyzers +{ + using static CodeAnalysisDiagnosticsResources; + + [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] + public sealed class DoNotUseFileTypesForAnalyzersOrGenerators : DiagnosticAnalyzer + { + private static readonly DiagnosticDescriptor Rule = new( + DiagnosticIds.DoNotUseFileTypesForAnalyzersOrGenerators, + CreateLocalizableResourceString(nameof(DoNotUseFileTypesForAnalyzersOrGeneratorsTitle)), + CreateLocalizableResourceString(nameof(DoNotUseFileTypesForAnalyzersOrGeneratorsMessage)), + DiagnosticCategory.MicrosoftCodeAnalysisCorrectness, + DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: CreateLocalizableResourceString(nameof(DoNotUseFileTypesForAnalyzersOrGeneratorsDescription))); + + public override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(Rule); + + public override void Initialize(AnalysisContext context) + { + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); + context.EnableConcurrentExecution(); + context.RegisterCompilationStartAction(context => + { + INamedTypeSymbol? diagnosticAnalyzer = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftCodeAnalysisDiagnosticsDiagnosticAnalyzer); + if (diagnosticAnalyzer == null) + { + // Does not contain any diagnostic analyzers. + return; + } + + // There may or may not be a code fix provider type and generator types, depending on what this assembly depends on. + INamedTypeSymbol? codeFixProvider = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftCodeAnalysisCodeFixesCodeFixProvider); + INamedTypeSymbol? isourceGenerator = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftCodeAnalysisISourceGenerator); + INamedTypeSymbol? iincrementalGenerator = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftCodeAnalysisIIncrementalGenerator); + + context.RegisterSymbolAction(symbolContext => AnalyzeSymbol(symbolContext, diagnosticAnalyzer, codeFixProvider, isourceGenerator, iincrementalGenerator), SymbolKind.NamedType); + }); + } + + private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol diagnosticAnalyzer, INamedTypeSymbol? codeFixProvider, INamedTypeSymbol? isourceGenerator, INamedTypeSymbol? iincrementalGenerator) + { + var namedTypeSymbol = (INamedTypeSymbol)context.Symbol; + + if (!namedTypeSymbol.IsFileLocal()) + { + return; + } + + if (namedTypeSymbol.Inherits(diagnosticAnalyzer) || + namedTypeSymbol.Inherits(codeFixProvider) || + namedTypeSymbol.Inherits(isourceGenerator) || + namedTypeSymbol.Inherits(iincrementalGenerator)) + { + var diagnostic = Diagnostic.Create(Rule, namedTypeSymbol.Locations[0], namedTypeSymbol.Name); + context.ReportDiagnostic(diagnostic); + } + } + } +} diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.cs.xlf b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.cs.xlf index 51891ff2b6..519f880069 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.cs.xlf +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.cs.xlf @@ -172,6 +172,21 @@ Nevolejte metodu Compilation.GetSemanticModel() v diagnostickém analyzátoru + + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + + + + Type '{0}' should not be marked with 'file' + Type '{0}' should not be marked with 'file' + + + + Do not use file types for implementing analyzers, generators, and code fixers + Do not use file types for implementing analyzers, generators, and code fixers + + DiagnosticId for analyzers should not use reserved IDs. Hodnoty DiagnosticId pro analyzátory by neměly používat rezervovaná ID. diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.de.xlf b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.de.xlf index 3b058c78e6..3048aba2da 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.de.xlf +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.de.xlf @@ -172,6 +172,21 @@ Compilation.GetSemanticModel()-Methode nicht innerhalb eines Diagnoseanalysetools aufrufen + + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + + + + Type '{0}' should not be marked with 'file' + Type '{0}' should not be marked with 'file' + + + + Do not use file types for implementing analyzers, generators, and code fixers + Do not use file types for implementing analyzers, generators, and code fixers + + DiagnosticId for analyzers should not use reserved IDs. Die DiagnosticId für Analysetools darf keine reservierten IDs verwenden. diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.es.xlf b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.es.xlf index dbb2c3ef96..7e30e87af9 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.es.xlf +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.es.xlf @@ -172,6 +172,21 @@ No invoque el método Compilation.GetSemanticModel() en un analizador de diagnóstico + + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + + + + Type '{0}' should not be marked with 'file' + Type '{0}' should not be marked with 'file' + + + + Do not use file types for implementing analyzers, generators, and code fixers + Do not use file types for implementing analyzers, generators, and code fixers + + DiagnosticId for analyzers should not use reserved IDs. El valor DiagnosticId para los analizadores no debe usar identificadores reservados. diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.fr.xlf b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.fr.xlf index 2f3d2745d6..322d3d2d50 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.fr.xlf +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.fr.xlf @@ -172,6 +172,21 @@ N'appelez pas la méthode Compilation.GetSemanticModel() dans un analyseur de diagnostic + + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + + + + Type '{0}' should not be marked with 'file' + Type '{0}' should not be marked with 'file' + + + + Do not use file types for implementing analyzers, generators, and code fixers + Do not use file types for implementing analyzers, generators, and code fixers + + DiagnosticId for analyzers should not use reserved IDs. DiagnosticId pour les analyseurs ne doit pas utiliser d'ID réservés. diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.it.xlf b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.it.xlf index 7e06d4df30..1db742bfc2 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.it.xlf +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.it.xlf @@ -172,6 +172,21 @@ Non richiamare il metodo Compilation.GetSemanticModel() in un analizzatore diagnostico + + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + + + + Type '{0}' should not be marked with 'file' + Type '{0}' should not be marked with 'file' + + + + Do not use file types for implementing analyzers, generators, and code fixers + Do not use file types for implementing analyzers, generators, and code fixers + + DiagnosticId for analyzers should not use reserved IDs. L'ID diagnostica per gli analizzatori non deve usare ID riservati. diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.ja.xlf b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.ja.xlf index 5874e6923d..3df62c7844 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.ja.xlf +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.ja.xlf @@ -172,6 +172,21 @@ 診断アナライザー内での Compilation.GetSemanticModel() メソッドの呼び出し禁止 + + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + + + + Type '{0}' should not be marked with 'file' + Type '{0}' should not be marked with 'file' + + + + Do not use file types for implementing analyzers, generators, and code fixers + Do not use file types for implementing analyzers, generators, and code fixers + + DiagnosticId for analyzers should not use reserved IDs. アナライザーの診断 ID には、予約済み ID を使用しないでください。 diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.ko.xlf b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.ko.xlf index a420161549..bc8eba243a 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.ko.xlf +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.ko.xlf @@ -172,6 +172,21 @@ 진단 분석기 내에서 Compilation.GetSemanticModel() 메서드를 호출하지 마세요. + + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + + + + Type '{0}' should not be marked with 'file' + Type '{0}' should not be marked with 'file' + + + + Do not use file types for implementing analyzers, generators, and code fixers + Do not use file types for implementing analyzers, generators, and code fixers + + DiagnosticId for analyzers should not use reserved IDs. 분석기의 DiagnosticId에는 예약된 ID를 사용해서는 안 됩니다. diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.pl.xlf b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.pl.xlf index 2fc901c653..177fd342a1 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.pl.xlf +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.pl.xlf @@ -172,6 +172,21 @@ Nie wywołuj metody Compilation.GetSemanticModel() w analizatorze diagnostycznym + + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + + + + Type '{0}' should not be marked with 'file' + Type '{0}' should not be marked with 'file' + + + + Do not use file types for implementing analyzers, generators, and code fixers + Do not use file types for implementing analyzers, generators, and code fixers + + DiagnosticId for analyzers should not use reserved IDs. W elemencie DiagnosticId dla analizatorów nie należy używać zastrzeżonych identyfikatorów. diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.pt-BR.xlf b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.pt-BR.xlf index dcfa9870b2..f6a1e56578 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.pt-BR.xlf +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.pt-BR.xlf @@ -172,6 +172,21 @@ Não invocar o método Compilation.GetSemanticModel() em um analisador de diagnóstico + + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + + + + Type '{0}' should not be marked with 'file' + Type '{0}' should not be marked with 'file' + + + + Do not use file types for implementing analyzers, generators, and code fixers + Do not use file types for implementing analyzers, generators, and code fixers + + DiagnosticId for analyzers should not use reserved IDs. A DiagnosticId dos analisadores não deve usar IDs reservadas. diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.ru.xlf b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.ru.xlf index 833a128ffa..f25f85529c 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.ru.xlf +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.ru.xlf @@ -172,6 +172,21 @@ Не вызывайте метод Compilation.GetSemanticModel() в диагностическом анализаторе + + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + + + + Type '{0}' should not be marked with 'file' + Type '{0}' should not be marked with 'file' + + + + Do not use file types for implementing analyzers, generators, and code fixers + Do not use file types for implementing analyzers, generators, and code fixers + + DiagnosticId for analyzers should not use reserved IDs. Идентификатор диагностики DiagnosticId для анализатора не должен включать зарезервированные идентификаторы. diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.tr.xlf b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.tr.xlf index 5e5f5c0f01..bc9fc04eb0 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.tr.xlf +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.tr.xlf @@ -172,6 +172,21 @@ Bir tanılama çözümleyicisi içinde Compilation.GetSemanticModel() yöntemini çağırma + + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + + + + Type '{0}' should not be marked with 'file' + Type '{0}' should not be marked with 'file' + + + + Do not use file types for implementing analyzers, generators, and code fixers + Do not use file types for implementing analyzers, generators, and code fixers + + DiagnosticId for analyzers should not use reserved IDs. Çözümleyiciler için DiagnosticId, ayrılmış kimlikler kullanmamalıdır. diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.zh-Hans.xlf b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.zh-Hans.xlf index 336e53ca66..3484cdd8a6 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.zh-Hans.xlf +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.zh-Hans.xlf @@ -172,6 +172,21 @@ 请勿在诊断分析器中调用 Compilation.GetSemanticModel() 方法 + + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + + + + Type '{0}' should not be marked with 'file' + Type '{0}' should not be marked with 'file' + + + + Do not use file types for implementing analyzers, generators, and code fixers + Do not use file types for implementing analyzers, generators, and code fixers + + DiagnosticId for analyzers should not use reserved IDs. 分析器的 DiagnosticId 不应使用保留 ID。 diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.zh-Hant.xlf b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.zh-Hant.xlf index c3986e2d28..019158fb86 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.zh-Hant.xlf +++ b/src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.zh-Hant.xlf @@ -172,6 +172,21 @@ 請不要在診斷分析器內叫用 Compilation.GetSemanticModel() 方法 + + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + + + + Type '{0}' should not be marked with 'file' + Type '{0}' should not be marked with 'file' + + + + Do not use file types for implementing analyzers, generators, and code fixers + Do not use file types for implementing analyzers, generators, and code fixers + + DiagnosticId for analyzers should not use reserved IDs. 分析器的診斷識別碼不應使用保留的識別碼。 diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Microsoft.CodeAnalysis.Analyzers.md b/src/Microsoft.CodeAnalysis.Analyzers/Microsoft.CodeAnalysis.Analyzers.md index 1974d81841..fd1bc1cec0 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Microsoft.CodeAnalysis.Analyzers.md +++ b/src/Microsoft.CodeAnalysis.Analyzers/Microsoft.CodeAnalysis.Analyzers.md @@ -520,6 +520,18 @@ The author of this interface has deprecated implementing this interface. |CodeFix|False| --- +## RS1043: Do not use file types for implementing analyzers, generators, and code fixers + +Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms. + +|Item|Value| +|-|-| +|Category|MicrosoftCodeAnalysisCorrectness| +|Enabled|True| +|Severity|Error| +|CodeFix|False| +--- + ## [RS2000](https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md): Add analyzer diagnostic IDs to analyzer release All supported analyzer diagnostic IDs should be part of an analyzer release. diff --git a/src/Microsoft.CodeAnalysis.Analyzers/Microsoft.CodeAnalysis.Analyzers.sarif b/src/Microsoft.CodeAnalysis.Analyzers/Microsoft.CodeAnalysis.Analyzers.sarif index b7a7bb6661..d8569ba439 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/Microsoft.CodeAnalysis.Analyzers.sarif +++ b/src/Microsoft.CodeAnalysis.Analyzers/Microsoft.CodeAnalysis.Analyzers.sarif @@ -528,6 +528,21 @@ ] } }, + "RS1043": { + "id": "RS1043", + "shortDescription": "Do not use file types for implementing analyzers, generators, and code fixers", + "fullDescription": "Using a 'file' type is not allowed for implementing analyzers, generators, or code fixers. This can break analyzer loading on some platforms.", + "defaultLevel": "error", + "properties": { + "category": "MicrosoftCodeAnalysisCorrectness", + "isEnabledByDefault": true, + "typeName": "DoNotUseFileTypesForAnalyzersOrGenerators", + "languages": [ + "C#", + "Visual Basic" + ] + } + }, "RS2000": { "id": "RS2000", "shortDescription": "Add analyzer diagnostic IDs to analyzer release", diff --git a/src/Microsoft.CodeAnalysis.Analyzers/RulesMissingDocumentation.md b/src/Microsoft.CodeAnalysis.Analyzers/RulesMissingDocumentation.md index 18e0e66b8d..7cf56d6168 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/RulesMissingDocumentation.md +++ b/src/Microsoft.CodeAnalysis.Analyzers/RulesMissingDocumentation.md @@ -40,3 +40,4 @@ RS1037 | | Add "CompilationEnd" custom tag to compilation end diagnostic descri RS1039 | | This call to 'SemanticModel.GetDeclaredSymbol()' will always return 'null' | RS1040 | | This call to 'SemanticModel.GetDeclaredSymbol()' will always return 'null' | RS1042 | | Implementations of this interface are not allowed | +RS1043 | | Do not use file types for implementing analyzers, generators, and code fixers | diff --git a/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/MetaAnalyzers/DoNotUseFileLocalTypesForAnalyzersOrGeneratorsTests.cs b/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/MetaAnalyzers/DoNotUseFileLocalTypesForAnalyzersOrGeneratorsTests.cs new file mode 100644 index 0000000000..b970affb72 --- /dev/null +++ b/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/MetaAnalyzers/DoNotUseFileLocalTypesForAnalyzersOrGeneratorsTests.cs @@ -0,0 +1,116 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. + +using System.Threading.Tasks; +using Microsoft.CodeAnalysis.Testing; +using Xunit; +using VerifyCS = Test.Utilities.CSharpCodeFixVerifier< + Microsoft.CodeAnalysis.Analyzers.MetaAnalyzers.DoNotUseFileTypesForAnalyzersOrGenerators, + Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>; + +namespace Microsoft.CodeAnalysis.CSharp.UnitTests.MetaAnalyzers +{ + public sealed partial class MetaAnalyzersTests + { + private const string CompilerReferenceVersion = "4.6.0"; + + [Fact] + public async Task FiresOnFileLocalType_CodeFixProvider() + { + var code = """ + using System.Collections.Immutable; + using System.Threading.Tasks; + using Microsoft.CodeAnalysis; + using Microsoft.CodeAnalysis.CodeFixes; + file class Type : Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider + { + public override ImmutableArray FixableDiagnosticIds => throw null!; + public override FixAllProvider GetFixAllProvider() => throw null!; + public override Task RegisterCodeFixesAsync(CodeFixContext context) => throw null!; + } + """; + await new VerifyCS.Test + { + TestCode = code, + LanguageVersion = LanguageVersion.CSharp11, + ExpectedDiagnostics = + { + // /0/Test0.cs(5,12): error RS1043: Type 'Type' should not be marked with 'file' + VerifyCS.Diagnostic().WithSpan(5, 12, 5, 16).WithArguments("Type"), + } + }.RunAsync(); + } + + [Fact] + public async Task FiresOnFileLocalType_DiagnosticAnalyzer() + { + var code = """ + using System.Collections.Immutable; + using Microsoft.CodeAnalysis; + using Microsoft.CodeAnalysis.Diagnostics; + file class Type : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer + { + public override ImmutableArray SupportedDiagnostics => throw null!; + public override void Initialize(AnalysisContext context) => throw null!; + + } + """; + await new VerifyCS.Test + { + TestCode = code, + LanguageVersion = LanguageVersion.CSharp11, + ExpectedDiagnostics = + { + // /0/Test0.cs(4,12): error RS1043: Type 'Type' should not be marked with 'file' + VerifyCS.Diagnostic().WithSpan(4, 12, 4, 16).WithArguments("Type"), + } + }.RunAsync(); + } + + [Fact] + public async Task FiresOnFileLocalType_ISourceGenerator() + { + var code = """ + using Microsoft.CodeAnalysis; + file class Type : Microsoft.CodeAnalysis.ISourceGenerator + { + public void Initialize(GeneratorInitializationContext context) => throw null!; + public void Execute(GeneratorExecutionContext context) => throw null!; + } + """; + await new VerifyCS.Test + { + TestCode = code, + LanguageVersion = LanguageVersion.CSharp11, + ReferenceAssemblies = ReferenceAssemblies.NetStandard.NetStandard20.AddPackages([new PackageIdentity("Microsoft.CodeAnalysis.Common", CompilerReferenceVersion)]), + ExpectedDiagnostics = + { + // /0/Test0.cs(2,12): error RS1043: Type 'Type' should not be marked with 'file' + VerifyCS.Diagnostic().WithSpan(2, 12, 2, 16).WithArguments("Type"), + }, + }.RunAsync(); + } + + [Fact] + public async Task FiresOnFileLocalType_IIncrementalGenerator() + { + var code = """ + using Microsoft.CodeAnalysis; + file class Type : Microsoft.CodeAnalysis.IIncrementalGenerator + { + public void Initialize(IncrementalGeneratorInitializationContext context) => throw null!; + } + """; + await new VerifyCS.Test + { + TestCode = code, + LanguageVersion = LanguageVersion.CSharp11, + ReferenceAssemblies = ReferenceAssemblies.NetStandard.NetStandard20.AddPackages([new PackageIdentity("Microsoft.CodeAnalysis.Common", CompilerReferenceVersion)]), + ExpectedDiagnostics = + { + // /0/Test0.cs(2,12): error RS1043: Type 'Type' should not be marked with 'file' + VerifyCS.Diagnostic().WithSpan(2, 12, 2, 16).WithArguments("Type"), + }, + }.RunAsync(); + } + } +} diff --git a/src/NetAnalyzers/RulesMissingDocumentation.md b/src/NetAnalyzers/RulesMissingDocumentation.md index 5e8204a3bc..b59a35228c 100644 --- a/src/NetAnalyzers/RulesMissingDocumentation.md +++ b/src/NetAnalyzers/RulesMissingDocumentation.md @@ -2,7 +2,5 @@ Rule ID | Missing Help Link | Title | --------|-------------------|-------| -CA2022 | | Avoid inexact read with 'Stream.Read' | CA2023 | | Invalid braces in message template | CA2024 | | Do not use 'StreamReader.EndOfStream' in async methods | -CA2265 | | Do not compare Span\ to 'null' or 'default' |