Skip to content

Commit 7d399f6

Browse files
authored
Enable analyzer analyzers (#91733)
Use Microsoft.CodeAnalysis.Analyzer in our builds, to help validate our analyzers and source generators. A few projects were doing this explicitly; this does it for all.
1 parent 5a6d5ef commit 7d399f6

13 files changed

Lines changed: 302 additions & 16 deletions

File tree

eng/Analyzers.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<PackageReference Include="Microsoft.DotNet.CodeAnalysis" Version="$(MicrosoftDotNetCodeAnalysisVersion)" PrivateAssets="all" IsImplicitlyDefined="true" />
2222
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="$(MicrosoftCodeAnalysisNetAnalyzersVersion)" PrivateAssets="all" />
2323
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeStyle" Version="$(MicrosoftCodeAnalysisCSharpCodeStyleVersion)" PrivateAssets="all" />
24+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(MicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
2425
<PackageReference Include="StyleCop.Analyzers" Version="$(StyleCopAnalyzersVersion)" PrivateAssets="all" />
2526
</ItemGroup>
2627
<ItemGroup Condition="'$(IsTestProject)' == 'true'">

eng/CodeAnalysis.src.globalconfig

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ dotnet_diagnostic.CA1860.severity = warning
475475
dotnet_diagnostic.CA1861.severity = warning
476476

477477
# CA1862: Prefer using 'StringComparer'/'StringComparison' to perform case-insensitive string comparisons
478-
dotnet_diagnostic.CA1862.severity = info
478+
dotnet_diagnostic.CA1862.severity = suggestion
479479

480480
# CA1863: Use 'CompositeFormat'
481481
dotnet_diagnostic.CA1863.severity = suggestion
@@ -955,6 +955,147 @@ dotnet_diagnostic.IL3001.severity = warning
955955
# IL3002: Using member with RequiresAssemblyFilesAttribute can break functionality when embedded in a single-file app
956956
dotnet_diagnostic.IL3002.severity = warning
957957

958+
# RS1001: Missing diagnostic analyzer attribute
959+
dotnet_diagnostic.RS1001.severity = warning
960+
961+
# RS1002: Missing kind argument when registering an analyzer action
962+
dotnet_diagnostic.RS1002.severity = warning
963+
964+
# RS1003: Unsupported SymbolKind argument when registering a symbol analyzer action
965+
dotnet_diagnostic.RS1003.severity = warning
966+
967+
# RS1004: Recommend adding language support to diagnostic analyzer
968+
dotnet_diagnostic.RS1004.severity = warning
969+
970+
# RS1005: ReportDiagnostic invoked with an unsupported DiagnosticDescriptor
971+
dotnet_diagnostic.RS1005.severity = warning
972+
973+
# RS1006: Invalid type argument for DiagnosticAnalyzer's Register method
974+
dotnet_diagnostic.RS1006.severity = warning
975+
976+
# RS1007: Provide localizable arguments to diagnostic descriptor constructor
977+
dotnet_diagnostic.RS1007.severity = none
978+
979+
# RS1008: Avoid storing per-compilation data into the fields of a diagnostic analyzer
980+
dotnet_diagnostic.RS1008.severity = warning
981+
982+
# RS1009: Only internal implementations of this interface are allowed
983+
dotnet_diagnostic.RS1009.severity = error
984+
985+
# RS1010: Create code actions should have a unique EquivalenceKey for FixAll occurrences support
986+
dotnet_diagnostic.RS1010.severity = warning
987+
988+
# RS1011: Use code actions that have a unique EquivalenceKey for FixAll occurrences support
989+
dotnet_diagnostic.RS1011.severity = warning
990+
991+
# RS1012: Start action has no registered actions
992+
dotnet_diagnostic.RS1012.severity = warning
993+
994+
# RS1013: Start action has no registered non-end actions
995+
dotnet_diagnostic.RS1013.severity = warning
996+
997+
# RS1014: Do not ignore values returned by methods on immutable objects
998+
dotnet_diagnostic.RS1014.severity = warning
999+
1000+
# RS1015: Provide non-null 'helpLinkUri' value to diagnostic descriptor constructor
1001+
dotnet_diagnostic.RS1015.severity = none
1002+
1003+
# RS1016: Code fix providers should provide FixAll support
1004+
dotnet_diagnostic.RS1016.severity = suggestion
1005+
1006+
# RS1017: DiagnosticId for analyzers must be a non-null constant
1007+
dotnet_diagnostic.RS1017.severity = warning
1008+
1009+
# RS1018: DiagnosticId for analyzers must be in specified format
1010+
dotnet_diagnostic.RS1018.severity = warning
1011+
1012+
# RS1019: DiagnosticId must be unique across analyzers
1013+
dotnet_diagnostic.RS1019.severity = warning
1014+
1015+
# RS1020: Category for analyzers must be from the specified values
1016+
dotnet_diagnostic.RS1020.severity = none
1017+
1018+
# RS1021: Invalid entry in analyzer category and diagnostic ID range specification file
1019+
dotnet_diagnostic.RS1021.severity = warning
1020+
1021+
# RS1022: Do not use types from Workspaces assembly in an analyzer
1022+
dotnet_diagnostic.RS1022.severity = warning
1023+
1024+
# RS1023: Upgrade MSBuildWorkspace
1025+
dotnet_diagnostic.RS1023.severity = warning
1026+
1027+
# RS1024: Symbols should be compared for equality
1028+
dotnet_diagnostic.RS1024.severity = warning
1029+
1030+
# RS1025: Configure generated code analysis
1031+
dotnet_diagnostic.RS1025.severity = warning
1032+
1033+
# RS1026: Enable concurrent execution
1034+
dotnet_diagnostic.RS1026.severity = warning
1035+
1036+
# RS1027: Types marked with DiagnosticAnalyzerAttribute(s) should inherit from DiagnosticAnalyzer
1037+
dotnet_diagnostic.RS1027.severity = warning
1038+
1039+
# RS1028: Provide non-null 'customTags' value to diagnostic descriptor constructor
1040+
dotnet_diagnostic.RS1028.severity = none
1041+
1042+
# RS1029: Do not use reserved diagnostic IDs
1043+
dotnet_diagnostic.RS1029.severity = warning
1044+
1045+
# RS1030: Do not invoke Compilation.GetSemanticModel() method within a diagnostic analyzer
1046+
dotnet_diagnostic.RS1030.severity = warning
1047+
1048+
# RS1031: Define diagnostic title correctly
1049+
dotnet_diagnostic.RS1031.severity = warning
1050+
1051+
# RS1032: Define diagnostic message correctly
1052+
dotnet_diagnostic.RS1032.severity = warning
1053+
1054+
# RS1033: Define diagnostic description correctly
1055+
dotnet_diagnostic.RS1033.severity = warning
1056+
1057+
# RS1034: Prefer 'IsKind' for checking syntax kinds
1058+
dotnet_diagnostic.RS1034.severity = warning
1059+
1060+
# RS1035: Do not use APIs banned for analyzers
1061+
dotnet_diagnostic.RS1035.severity = error
1062+
1063+
# RS1036: Specify analyzer banned API enforcement setting
1064+
dotnet_diagnostic.RS1036.severity = warning
1065+
1066+
# RS1037: Add "CompilationEnd" custom tag to compilation end diagnostic descriptor
1067+
dotnet_diagnostic.RS1037.severity = warning
1068+
1069+
# RS1038: Compiler extensions should be implemented in assemblies with compiler-provided references
1070+
dotnet_diagnostic.RS1038.severity = suggestion
1071+
1072+
# RS2000: Add analyzer diagnostic IDs to analyzer release
1073+
dotnet_diagnostic.RS2000.severity = warning
1074+
1075+
# RS2001: Ensure up-to-date entry for analyzer diagnostic IDs are added to analyzer release
1076+
dotnet_diagnostic.RS2001.severity = warning
1077+
1078+
# RS2002: Do not add removed analyzer diagnostic IDs to unshipped analyzer release
1079+
dotnet_diagnostic.RS2002.severity = warning
1080+
1081+
# RS2003: Shipped diagnostic IDs that are no longer reported should have an entry in the 'Removed Rules' table in unshipped file
1082+
dotnet_diagnostic.RS2003.severity = warning
1083+
1084+
# RS2004: Diagnostic IDs marked as removed in analyzer release file should not be reported by analyzers
1085+
dotnet_diagnostic.RS2004.severity = warning
1086+
1087+
# RS2005: Remove duplicate entries for diagnostic ID in the same analyzer release
1088+
dotnet_diagnostic.RS2005.severity = warning
1089+
1090+
# RS2006: Remove duplicate entries for diagnostic ID between analyzer releases
1091+
dotnet_diagnostic.RS2006.severity = warning
1092+
1093+
# RS2007: Invalid entry in analyzer release file
1094+
dotnet_diagnostic.RS2007.severity = warning
1095+
1096+
# RS2008: Enable analyzer release tracking
1097+
dotnet_diagnostic.RS2008.severity = warning
1098+
9581099
# SA0001: XML comments
9591100
dotnet_diagnostic.SA0001.severity = none
9601101

eng/CodeAnalysis.test.globalconfig

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,147 @@ dotnet_diagnostic.IL3001.severity = none
951951
# IL3002: Using member with RequiresAssemblyFilesAttribute can break functionality when embedded in a single-file app
952952
dotnet_diagnostic.IL3002.severity = none
953953

954+
# RS1001: Missing diagnostic analyzer attribute
955+
dotnet_diagnostic.RS1001.severity = none
956+
957+
# RS1002: Missing kind argument when registering an analyzer action
958+
dotnet_diagnostic.RS1002.severity = none
959+
960+
# RS1003: Unsupported SymbolKind argument when registering a symbol analyzer action
961+
dotnet_diagnostic.RS1003.severity = none
962+
963+
# RS1004: Recommend adding language support to diagnostic analyzer
964+
dotnet_diagnostic.RS1004.severity = none
965+
966+
# RS1005: ReportDiagnostic invoked with an unsupported DiagnosticDescriptor
967+
dotnet_diagnostic.RS1005.severity = none
968+
969+
# RS1006: Invalid type argument for DiagnosticAnalyzer's Register method
970+
dotnet_diagnostic.RS1006.severity = none
971+
972+
# RS1007: Provide localizable arguments to diagnostic descriptor constructor
973+
dotnet_diagnostic.RS1007.severity = none
974+
975+
# RS1008: Avoid storing per-compilation data into the fields of a diagnostic analyzer
976+
dotnet_diagnostic.RS1008.severity = none
977+
978+
# RS1009: Only internal implementations of this interface are allowed
979+
dotnet_diagnostic.RS1009.severity = none
980+
981+
# RS1010: Create code actions should have a unique EquivalenceKey for FixAll occurrences support
982+
dotnet_diagnostic.RS1010.severity = none
983+
984+
# RS1011: Use code actions that have a unique EquivalenceKey for FixAll occurrences support
985+
dotnet_diagnostic.RS1011.severity = none
986+
987+
# RS1012: Start action has no registered actions
988+
dotnet_diagnostic.RS1012.severity = none
989+
990+
# RS1013: Start action has no registered non-end actions
991+
dotnet_diagnostic.RS1013.severity = none
992+
993+
# RS1014: Do not ignore values returned by methods on immutable objects
994+
dotnet_diagnostic.RS1014.severity = none
995+
996+
# RS1015: Provide non-null 'helpLinkUri' value to diagnostic descriptor constructor
997+
dotnet_diagnostic.RS1015.severity = none
998+
999+
# RS1016: Code fix providers should provide FixAll support
1000+
dotnet_diagnostic.RS1016.severity = suggestion
1001+
1002+
# RS1017: DiagnosticId for analyzers must be a non-null constant
1003+
dotnet_diagnostic.RS1017.severity = none
1004+
1005+
# RS1018: DiagnosticId for analyzers must be in specified format
1006+
dotnet_diagnostic.RS1018.severity = none
1007+
1008+
# RS1019: DiagnosticId must be unique across analyzers
1009+
dotnet_diagnostic.RS1019.severity = none
1010+
1011+
# RS1020: Category for analyzers must be from the specified values
1012+
dotnet_diagnostic.RS1020.severity = none
1013+
1014+
# RS1021: Invalid entry in analyzer category and diagnostic ID range specification file
1015+
dotnet_diagnostic.RS1021.severity = none
1016+
1017+
# RS1022: Do not use types from Workspaces assembly in an analyzer
1018+
dotnet_diagnostic.RS1022.severity = none
1019+
1020+
# RS1023: Upgrade MSBuildWorkspace
1021+
dotnet_diagnostic.RS1023.severity = none
1022+
1023+
# RS1024: Symbols should be compared for equality
1024+
dotnet_diagnostic.RS1024.severity = none
1025+
1026+
# RS1025: Configure generated code analysis
1027+
dotnet_diagnostic.RS1025.severity = none
1028+
1029+
# RS1026: Enable concurrent execution
1030+
dotnet_diagnostic.RS1026.severity = none
1031+
1032+
# RS1027: Types marked with DiagnosticAnalyzerAttribute(s) should inherit from DiagnosticAnalyzer
1033+
dotnet_diagnostic.RS1027.severity = none
1034+
1035+
# RS1028: Provide non-null 'customTags' value to diagnostic descriptor constructor
1036+
dotnet_diagnostic.RS1028.severity = none
1037+
1038+
# RS1029: Do not use reserved diagnostic IDs
1039+
dotnet_diagnostic.RS1029.severity = none
1040+
1041+
# RS1030: Do not invoke Compilation.GetSemanticModel() method within a diagnostic analyzer
1042+
dotnet_diagnostic.RS1030.severity = none
1043+
1044+
# RS1031: Define diagnostic title correctly
1045+
dotnet_diagnostic.RS1031.severity = none
1046+
1047+
# RS1032: Define diagnostic message correctly
1048+
dotnet_diagnostic.RS1032.severity = none
1049+
1050+
# RS1033: Define diagnostic description correctly
1051+
dotnet_diagnostic.RS1033.severity = none
1052+
1053+
# RS1034: Prefer 'IsKind' for checking syntax kinds
1054+
dotnet_diagnostic.RS1034.severity = none
1055+
1056+
# RS1035: Do not use APIs banned for analyzers
1057+
dotnet_diagnostic.RS1035.severity = none
1058+
1059+
# RS1036: Specify analyzer banned API enforcement setting
1060+
dotnet_diagnostic.RS1036.severity = none
1061+
1062+
# RS1037: Add "CompilationEnd" custom tag to compilation end diagnostic descriptor
1063+
dotnet_diagnostic.RS1037.severity = none
1064+
1065+
# RS1038: Compiler extensions should be implemented in assemblies with compiler-provided references
1066+
dotnet_diagnostic.RS1038.severity = suggestion
1067+
1068+
# RS2000: Add analyzer diagnostic IDs to analyzer release
1069+
dotnet_diagnostic.RS2000.severity = none
1070+
1071+
# RS2001: Ensure up-to-date entry for analyzer diagnostic IDs are added to analyzer release
1072+
dotnet_diagnostic.RS2001.severity = none
1073+
1074+
# RS2002: Do not add removed analyzer diagnostic IDs to unshipped analyzer release
1075+
dotnet_diagnostic.RS2002.severity = none
1076+
1077+
# RS2003: Shipped diagnostic IDs that are no longer reported should have an entry in the 'Removed Rules' table in unshipped file
1078+
dotnet_diagnostic.RS2003.severity = none
1079+
1080+
# RS2004: Diagnostic IDs marked as removed in analyzer release file should not be reported by analyzers
1081+
dotnet_diagnostic.RS2004.severity = none
1082+
1083+
# RS2005: Remove duplicate entries for diagnostic ID in the same analyzer release
1084+
dotnet_diagnostic.RS2005.severity = none
1085+
1086+
# RS2006: Remove duplicate entries for diagnostic ID between analyzer releases
1087+
dotnet_diagnostic.RS2006.severity = none
1088+
1089+
# RS2007: Invalid entry in analyzer release file
1090+
dotnet_diagnostic.RS2007.severity = none
1091+
1092+
# RS2008: Enable analyzer release tracking
1093+
dotnet_diagnostic.RS2008.severity = none
1094+
9541095
# SA0001: XML comments
9551096
dotnet_diagnostic.SA0001.severity = none
9561097

src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Roslyn3.11.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using Microsoft.CodeAnalysis.CSharp.Syntax;
88
using Microsoft.CodeAnalysis.Text;
99

10+
#pragma warning disable RS1035 // IIncrementalGenerator isn't available for the target configuration
11+
1012
[assembly: System.Resources.NeutralResourcesLanguage("en-us")]
1113

1214
namespace Microsoft.Extensions.Logging.Generators

src/libraries/Microsoft.Extensions.Options/gen/Emitter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ private StaticFieldInfo GetOrAddStaticValidationAttribute(ref Dictionary<string,
316316
attrInstantiationStatementLines.Add("}");
317317
}
318318

319-
var instantiationStatement = string.Join(Environment.NewLine, attrInstantiationStatementLines);
319+
var instantiationStatement = string.Join("\n", attrInstantiationStatementLines);
320320

321321
if (!staticValidationAttributesDict.TryGetValue(instantiationStatement, out var staticValidationAttributeInstance))
322322
{

src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSImportGenerator.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
<ItemGroup>
1717
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion_LatestVS)" PrivateAssets="all" />
18-
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(MicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
1918
</ItemGroup>
2019

2120
<ItemGroup>

src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
<ItemGroup>
1818
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion_LatestVS)" PrivateAssets="all" />
19-
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(MicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
2019
</ItemGroup>
2120

2221
<ItemGroup>

src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
<ItemGroup>
1717
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion_LatestVS)" PrivateAssets="all" />
18-
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(MicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
1918
</ItemGroup>
2019

2120
<ItemGroup>

src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Microsoft.Interop.SourceGeneration.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
<ItemGroup>
2828
<PackageReference Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion_LatestVS)" PrivateAssets="all" />
29-
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(MicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
3029
</ItemGroup>
3130

3231
</Project>

src/libraries/System.Text.Json/gen/JsonSourceGenerator.Roslyn3.11.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using Microsoft.CodeAnalysis.CSharp.Syntax;
1010
using Microsoft.CodeAnalysis.Text;
1111

12+
#pragma warning disable RS1035 // IIncrementalGenerator isn't available for the target configuration
13+
1214
namespace System.Text.Json.SourceGeneration
1315
{
1416
/// <summary>

0 commit comments

Comments
 (0)