Skip to content

Commit 0f8d5c9

Browse files
authored
Merge pull request #50031 from Youssef1313/patch-5
Ban DiagnosticDescriptor ctor in CodeStyle layer
2 parents e33749c + d33bc90 commit 0f8d5c9

File tree

7 files changed

+23
-4
lines changed

7 files changed

+23
-4
lines changed

src/Analyzers/Core/Analyzers/AbstractBuiltInCodeStyleDiagnosticAnalyzer_Core.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ protected static DiagnosticDescriptor CreateDescriptorWithId(
5454
bool isUnnecessary = false,
5555
bool isConfigurable = true,
5656
LocalizableString? description = null)
57+
#pragma warning disable RS0030 // Do not used banned APIs
5758
=> new(
5859
id, title, messageFormat,
5960
DiagnosticCategory.Style,
@@ -62,6 +63,7 @@ protected static DiagnosticDescriptor CreateDescriptorWithId(
6263
description: description,
6364
helpLinkUri: DiagnosticHelper.GetHelpLinkForDiagnosticId(id),
6465
customTags: DiagnosticCustomTags.Create(isUnnecessary, isConfigurable, enforceOnBuild));
66+
#pragma warning restore RS0030 // Do not used banned APIs
6567

6668
public sealed override void Initialize(AnalysisContext context)
6769
{

src/Analyzers/Core/Analyzers/AbstractCodeQualityDiagnosticAnalyzer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ protected static DiagnosticDescriptor CreateDescriptor(
5454
bool isEnabledByDefault = true,
5555
bool isConfigurable = true,
5656
LocalizableString description = null)
57+
#pragma warning disable RS0030 // Do not used banned APIs
5758
=> new(
5859
id, title, messageFormat,
5960
DiagnosticCategory.CodeQuality,
@@ -62,5 +63,6 @@ protected static DiagnosticDescriptor CreateDescriptor(
6263
description,
6364
helpLinkUri: DiagnosticHelper.GetHelpLinkForDiagnosticId(id),
6465
customTags: DiagnosticCustomTags.Create(isUnnecessary, isConfigurable, enforceOnBuild));
66+
#pragma warning disable RS0030 // Do not used banned APIs
6567
}
6668
}

src/Analyzers/Core/Analyzers/RemoveUnnecessaryImports/AbstractRemoveUnnecessaryImportsDiagnosticAnalyzer.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ internal abstract class AbstractRemoveUnnecessaryImportsDiagnosticAnalyzer
3333
// The NotConfigurable custom tag ensures that user can't turn this diagnostic into a warning / error via
3434
// ruleset editor or solution explorer. Setting messageFormat to empty string ensures that we won't display
3535
// this diagnostic in the preview pane header.
36+
#pragma warning disable RS0030 // Do not used banned APIs - We cannot use AbstractBuiltInCodeStyleDiagnosticAnalyzer nor AbstractCodeQualityDiagnosticAnalyzer.
37+
// This analyzer is run against generated code while the abstract base classes mentioned doesn't.
3638
private static readonly DiagnosticDescriptor s_fixableIdDescriptor =
3739
new(DiagnosticFixableId,
3840
title: "", messageFormat: "", category: "",
3941
defaultSeverity: DiagnosticSeverity.Hidden,
4042
isEnabledByDefault: true,
4143
customTags: DiagnosticCustomTags.NotConfigurable);
44+
#pragma warning restore RS0030 // Do not used banned APIs
4245

4346
protected abstract LocalizableString GetTitleAndMessageFormatForClassificationIdDescriptor();
4447
protected abstract ImmutableArray<SyntaxNode> MergeImports(ImmutableArray<SyntaxNode> unnecessaryImports);
@@ -56,13 +59,15 @@ private void EnsureClassificationIdDescriptors()
5659
{
5760
var titleAndMessageFormat = GetTitleAndMessageFormatForClassificationIdDescriptor();
5861

62+
#pragma warning disable RS0030 // Do not used banned APIs
5963
_unnecessaryClassificationIdDescriptor =
6064
new DiagnosticDescriptor(IDEDiagnosticIds.RemoveUnnecessaryImportsDiagnosticId,
6165
titleAndMessageFormat,
6266
titleAndMessageFormat,
6367
DiagnosticCategory.Style,
6468
DiagnosticSeverity.Hidden,
6569
isEnabledByDefault: true,
70+
helpLinkUri: DiagnosticHelper.GetHelpLinkForDiagnosticId(IDEDiagnosticIds.RemoveUnnecessaryImportsDiagnosticId),
6671
customTags: DiagnosticCustomTags.Unnecessary.Concat(EnforceOnBuildValues.RemoveUnnecessaryImports.ToCustomTag()).ToArray());
6772

6873
_classificationIdDescriptor =
@@ -72,6 +77,7 @@ private void EnsureClassificationIdDescriptors()
7277
DiagnosticCategory.Style,
7378
DiagnosticSeverity.Hidden,
7479
isEnabledByDefault: true,
80+
helpLinkUri: DiagnosticHelper.GetHelpLinkForDiagnosticId(IDEDiagnosticIds.RemoveUnnecessaryImportsDiagnosticId),
7581
customTags: EnforceOnBuildValues.RemoveUnnecessaryImports.ToCustomTag());
7682

7783
_unnecessaryGeneratedCodeClassificationIdDescriptor =
@@ -81,6 +87,7 @@ private void EnsureClassificationIdDescriptors()
8187
DiagnosticCategory.Style,
8288
DiagnosticSeverity.Hidden,
8389
isEnabledByDefault: true,
90+
helpLinkUri: DiagnosticHelper.GetHelpLinkForDiagnosticId(IDEDiagnosticIds.RemoveUnnecessaryImportsDiagnosticId),
8491
customTags: DiagnosticCustomTags.UnnecessaryAndNotConfigurable);
8592

8693
_generatedCodeClassificationIdDescriptor =
@@ -90,7 +97,9 @@ private void EnsureClassificationIdDescriptors()
9097
DiagnosticCategory.Style,
9198
DiagnosticSeverity.Hidden,
9299
isEnabledByDefault: true,
100+
helpLinkUri: DiagnosticHelper.GetHelpLinkForDiagnosticId(IDEDiagnosticIds.RemoveUnnecessaryImportsDiagnosticId),
93101
customTags: DiagnosticCustomTags.NotConfigurable);
102+
#pragma warning restore RS0030 // Do not used banned APIs
94103
}
95104
}
96105

src/Analyzers/Core/Analyzers/ValidateFormatString/AbstractValidateFormatStringDiagnosticAnalyzer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ internal abstract class AbstractValidateFormatStringDiagnosticAnalyzer<TSyntaxKi
3333
AnalyzersResources.ResourceManager,
3434
typeof(AnalyzersResources));
3535

36+
#pragma warning disable RS0030 // Do not used banned APIs - We cannot use AbstractBuiltInCodeStyleDiagnosticAnalyzer nor AbstractCodeQualityDiagnosticAnalyzer.
37+
// This analyzer is run against generated code while the abstract base classes mentioned doesn't. The rule is also not documented.
38+
// There is even a current work to remove the rule completely in favor of CA2241.
3639
private static readonly DiagnosticDescriptor Rule = new(
3740
DiagnosticID,
3841
Title,
@@ -42,6 +45,7 @@ internal abstract class AbstractValidateFormatStringDiagnosticAnalyzer<TSyntaxKi
4245
isEnabledByDefault: true,
4346
description: Description,
4447
customTags: EnforceOnBuildValues.ValidateFormatString.ToCustomTag());
48+
#pragma warning restore RS0030 // Do not used banned APIs
4549

4650
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
4751
=> ImmutableArray.Create(Rule);

src/CodeStyle/BannedSymbols.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ T:Microsoft.CodeAnalysis.Options.OptionSet; 'OptionSet' is not available in Code
44
T:Microsoft.CodeAnalysis.CodeStyle.CodeStyleOption`1; Use 'Microsoft.CodeAnalysis.CodeStyle.CodeStyleOption2' instead
55
T:Microsoft.CodeAnalysis.CodeStyle.CodeStyleOptions; Use 'Microsoft.CodeAnalysis.CodeStyle.CodeStyleOptions2' instead
66
T:Microsoft.CodeAnalysis.Options.OptionKey; Use 'Microsoft.CodeAnalysis.Options.OptionKey2' instead
7-
T:Microsoft.CodeAnalysis.CodeStyle.NotificationOption; Use 'Microsoft.CodeAnalysis.CodeStyle.NotificationOption2' instead
7+
T:Microsoft.CodeAnalysis.CodeStyle.NotificationOption; Use 'Microsoft.CodeAnalysis.CodeStyle.NotificationOption2' instead
8+
M:Microsoft.CodeAnalysis.DiagnosticDescriptor.#ctor(System.String,System.String,System.String,System.String,Microsoft.CodeAnalysis.DiagnosticSeverity,System.Boolean,System.String,System.String,System.String[]); Analyzers should extend 'AbstractBuiltInCodeStyleDiagnosticAnalyzer' or 'AbstractCodeQualityDiagnosticAnalyzer' instead
9+
M:Microsoft.CodeAnalysis.DiagnosticDescriptor.#ctor(System.String,Microsoft.CodeAnalysis.LocalizableString,Microsoft.CodeAnalysis.LocalizableString,System.String,Microsoft.CodeAnalysis.DiagnosticSeverity,System.Boolean,Microsoft.CodeAnalysis.LocalizableString,System.String,System.String[]); Analyzers should extend 'AbstractBuiltInCodeStyleDiagnosticAnalyzer' or 'AbstractCodeQualityDiagnosticAnalyzer' instead

src/EditorFeatures/CSharpTest/Squiggles/ErrorSquiggleProducerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void Test()
142142
var expectedToolTip = new ContainerElement(
143143
ContainerElementStyle.Wrapped,
144144
new ClassifiedTextElement(
145-
new ClassifiedTextRun(ClassificationTypeNames.Text, "IDE0005"),
145+
new ClassifiedTextRun(ClassificationTypeNames.Text, "IDE0005", QuickInfoHyperLink.TestAccessor.CreateNavigationAction(new Uri("https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005", UriKind.Absolute)), "https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005"),
146146
new ClassifiedTextRun(ClassificationTypeNames.Punctuation, ":"),
147147
new ClassifiedTextRun(ClassificationTypeNames.WhiteSpace, " "),
148148
new ClassifiedTextRun(ClassificationTypeNames.Text, CSharpAnalyzersResources.Using_directive_is_unnecessary)));
@@ -155,7 +155,7 @@ void Test()
155155
expectedToolTip = new ContainerElement(
156156
ContainerElementStyle.Wrapped,
157157
new ClassifiedTextElement(
158-
new ClassifiedTextRun(ClassificationTypeNames.Text, "IDE0005"),
158+
new ClassifiedTextRun(ClassificationTypeNames.Text, "IDE0005", QuickInfoHyperLink.TestAccessor.CreateNavigationAction(new Uri("https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005", UriKind.Absolute)), "https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005"),
159159
new ClassifiedTextRun(ClassificationTypeNames.Punctuation, ":"),
160160
new ClassifiedTextRun(ClassificationTypeNames.WhiteSpace, " "),
161161
new ClassifiedTextRun(ClassificationTypeNames.Text, CSharpAnalyzersResources.Using_directive_is_unnecessary)));

src/EditorFeatures/VisualBasicTest/Squiggles/ErrorSquiggleProducerTests.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ End Class"
132132
Dim expectedToolTip = New ContainerElement(
133133
ContainerElementStyle.Wrapped,
134134
New ClassifiedTextElement(
135-
New ClassifiedTextRun(ClassificationTypeNames.Text, "IDE0005"),
135+
New ClassifiedTextRun(ClassificationTypeNames.Text, "IDE0005", QuickInfoHyperLink.TestAccessor.CreateNavigationAction(new Uri("https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005", UriKind.Absolute)), "https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005"),
136136
New ClassifiedTextRun(ClassificationTypeNames.Punctuation, ":"),
137137
New ClassifiedTextRun(ClassificationTypeNames.WhiteSpace, " "),
138138
New ClassifiedTextRun(ClassificationTypeNames.Text, VisualBasicAnalyzersResources.Imports_statement_is_unnecessary)))

0 commit comments

Comments
 (0)