Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a helper diagnostic for enforcing IDE0005 (remove unnecessary usi… #58835

Merged
merged 11 commits into from
Apr 25, 2023
23 changes: 22 additions & 1 deletion src/Analyzers/Core/Analyzers/AnalyzersResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,27 @@
<data name="Simplify_LINQ_expression" xml:space="preserve">
<value>Simplify LINQ expression</value>
</data>
<data name="Set_MSBuild_Property_GenerateDocumentationFile_to_true" xml:space="preserve">
<value>Set MSBuild property 'GenerateDocumentationFile' to 'true'</value>
</data>
<data name="Set_MSBuild_Property_GenerateDocumentationFile_to_true_in_project_file_to_enable_IDE0005_Remove_unnecessary_usings_imports_on_build" xml:space="preserve">
<value>Set MSBuild property 'GenerateDocumentationFile' to 'true' in project file to enable IDE0005 (Remove unnecessary usings/imports) on build</value>
</data>
<data name="Add_the_following_PropertyGroup_to_your_MSBuild_project_file_to_enable_IDE0005_Remove_unnecessary_usings_imports_on_build" xml:space="preserve">
<value>Add the following PropertyGroup to your MSBuild project file to enable IDE0005 (Remove unnecessary usings/imports) on build:
&lt;PropertyGroup&gt;
&lt;!--
Make sure any documentation comments which are included in code get checked for syntax during the build, but do
not report warnings for missing comments.
CS1573: Parameter 'parameter' has no matching param tag in the XML comment for 'parameter' (but other parameters do)
CS1591: Missing XML comment for publicly visible type or member 'Type_or_Member'
CS1712: Type parameter 'type_parameter' has no matching typeparam tag in the XML comment on 'type_or_member' (but other type parameters do)
--&gt;
&lt;GenerateDocumentationFile&gt;True&lt;/GenerateDocumentationFile&gt;
&lt;NoWarn&gt;$(NoWarn),1573,1591,1712&lt;/NoWarn&gt;
&lt;/PropertyGroup&gt;
</value>
</data>
<data name="Fix_formatting" xml:space="preserve">
<value>Fix formatting</value>
</data>
Expand All @@ -376,4 +397,4 @@
<data name="Simplify_check" xml:space="preserve">
<value>Simplify check</value>
</data>
</root>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,30 @@ internal abstract class AbstractRemoveUnnecessaryImportsDiagnosticAnalyzer<TSynt
AbstractBuiltInUnnecessaryCodeStyleDiagnosticAnalyzer
where TSyntaxNode : SyntaxNode
{
// NOTE: This is a special helper diagnostic ID which is reported when the remove unnecesssary diagnostic ID (IDE0005) is
// ecalated to a warning or an error, but 'GenerateDocumentationFile' is false, which leads to IDE0005 not being reported
// on command line builds. See https://github.com/dotnet/roslyn/issues/41640 for more details.
internal const string EnableGenerateDocumentationFileId = "EnableGenerateDocumentationFile";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am in two minds whether this diagnostic should get an IDExxxx ID or a special ID like this one is just fine.


// The NotConfigurable custom tag ensures that user can't turn this diagnostic into a warning / error via
// ruleset editor or solution explorer. Setting messageFormat to empty string ensures that we won't display
// this diagnostic in the preview pane header.
private static readonly DiagnosticDescriptor s_fixableIdDescriptor = CreateDescriptorWithId(
RemoveUnnecessaryImportsConstants.DiagnosticFixableId, EnforceOnBuild.Never, "", "", isConfigurable: false);

#pragma warning disable RS0030 // Do not used banned APIs - Special diagnostic with 'Warning' default severity.
private static readonly DiagnosticDescriptor s_enableGenerateDocumentationFileIdDescriptor = new(
EnableGenerateDocumentationFileId,
title: AnalyzersResources.Set_MSBuild_Property_GenerateDocumentationFile_to_true,
messageFormat: AnalyzersResources.Set_MSBuild_Property_GenerateDocumentationFile_to_true_in_project_file_to_enable_IDE0005_Remove_unnecessary_usings_imports_on_build,
category: DiagnosticCategory.Style,
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true,
helpLinkUri: "https://github.com/dotnet/roslyn/issues/41640",
description: AnalyzersResources.Add_the_following_PropertyGroup_to_your_MSBuild_project_file_to_enable_IDE0005_Remove_unnecessary_usings_imports_on_build,
customTags: DiagnosticCustomTags.Microsoft.Concat(EnforceOnBuild.Never.ToCustomTag()).ToArray());
#pragma warning restore RS0030 // Do not used banned APIs

private readonly DiagnosticDescriptor _classificationIdDescriptor;
private readonly DiagnosticDescriptor _generatedCodeClassificationIdDescriptor;

Expand All @@ -39,7 +57,7 @@ private static ImmutableArray<DiagnosticDescriptor> GetDescriptors(LocalizableSt
{
classificationIdDescriptor = CreateDescriptorWithId(IDEDiagnosticIds.RemoveUnnecessaryImportsDiagnosticId, EnforceOnBuildValues.RemoveUnnecessaryImports, titleAndMessage, isUnnecessary: true);
generatedCodeClassificationIdDescriptor = CreateDescriptorWithId(IDEDiagnosticIds.RemoveUnnecessaryImportsDiagnosticId + "_gen", EnforceOnBuild.Never, titleAndMessage, isUnnecessary: true, isConfigurable: false);
return ImmutableArray.Create(s_fixableIdDescriptor, classificationIdDescriptor, generatedCodeClassificationIdDescriptor);
return ImmutableArray.Create(s_fixableIdDescriptor, s_enableGenerateDocumentationFileIdDescriptor, classificationIdDescriptor, generatedCodeClassificationIdDescriptor);
}

protected abstract ISyntaxFacts SyntaxFacts { get; }
Expand Down Expand Up @@ -84,6 +102,20 @@ private void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
{
context.ReportDiagnostic(diagnostic);
}

// Due to https://github.com/dotnet/roslyn/issues/41640, enabling this analyzer (IDE0005) on build requires users
// to enable generation of XML documentation comments. We detect if generation of XML documentation comments
// is disabled for this tree and IDE0005 diagnostics are being reported with effective severity "Warning" or "Error".
// If so, we report a special diagnostic that recommends the users to set "GenerateDocumentationFile" to "true"
// in their project file to enable IDE0005 on build.
if (tree.Options.DocumentationMode == DocumentationMode.Parse)
mavasani marked this conversation as resolved.
Show resolved Hide resolved
{
var effectiveSeverity = descriptor.GetEffectiveSeverity(context.SemanticModel.Compilation.Options, tree, context.Options);
if (effectiveSeverity is ReportDiagnostic.Warn or ReportDiagnostic.Error)
{
context.ReportDiagnostic(Diagnostic.Create(s_enableGenerateDocumentationFileIdDescriptor, diagnostics.First().Location));
}
}
}
}

Expand Down
39 changes: 39 additions & 0 deletions src/Analyzers/Core/Analyzers/xlf/AnalyzersResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions src/Analyzers/Core/Analyzers/xlf/AnalyzersResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions src/Analyzers/Core/Analyzers/xlf/AnalyzersResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading