Skip to content

Commit

Permalink
Merge pull request #58835 from mavasani/UnusedUsings_WarningError
Browse files Browse the repository at this point in the history
Add a helper diagnostic for enforcing IDE0005 (remove unnecessary usi…
  • Loading branch information
mavasani authored Apr 25, 2023
2 parents 248e851 + adcb1ad commit 005a17a
Show file tree
Hide file tree
Showing 17 changed files with 644 additions and 8 deletions.
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";

// 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 All @@ -54,13 +72,13 @@ private static ImmutableArray<DiagnosticDescriptor> GetDescriptors(LocalizableSt
protected override void InitializeWorker(AnalysisContext context)
{
context.RegisterSemanticModelAction(AnalyzeSemanticModel);
context.RegisterCompilationAction(AnalyzeCompilation);
}

private void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
{
var tree = context.SemanticModel.SyntaxTree;
var cancellationToken = context.CancellationToken;
var language = context.SemanticModel.Language;

var unnecessaryImports = UnnecessaryImportsProvider.GetUnnecessaryImports(context.SemanticModel, cancellationToken);
if (unnecessaryImports.Any())
Expand All @@ -87,6 +105,26 @@ private void AnalyzeSemanticModel(SemanticModelAnalysisContext context)
}
}

private void AnalyzeCompilation(CompilationAnalysisContext context)
{
// 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.

var compilation = context.Compilation;
var tree = compilation.SyntaxTrees.FirstOrDefault(tree => !GeneratedCodeUtilities.IsGeneratedCode(tree, IsRegularCommentOrDocComment, context.CancellationToken));
if (tree is null || tree.Options.DocumentationMode != DocumentationMode.None)
return;

var effectiveSeverity = _classificationIdDescriptor.GetEffectiveSeverity(compilation.Options, tree, context.Options);
if (effectiveSeverity is ReportDiagnostic.Warn or ReportDiagnostic.Error)
{
context.ReportDiagnostic(Diagnostic.Create(s_enableGenerateDocumentationFileIdDescriptor, Location.None));
}
}

private IEnumerable<TextSpan> GetContiguousSpans(ImmutableArray<SyntaxNode> nodes)
{
var syntaxFacts = this.SyntaxFacts;
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

0 comments on commit 005a17a

Please sign in to comment.