Add IDE analyzer for unnecessary pragma suppressions#44848
Conversation
Fixes dotnet#44177 Addresses part of dotnet#44178 NOTE: This analyzer can only be executed from IDE, not in command line builds due to the following requirements/restrictions: 1. Detecting unnecessary suppressions requires knowing all suppressed diagnostics in a file, so we can identify the pragma suppressions that are not used. 2. Detecting unnecessary analyzer diagnostic suppressions requires all reported (suppressed) diagnostics for relevant analyzers. As regular analyzers cannot have access to other analyzers or their diagnostics, this is a special IDE-only analyzer. This analyzer requires special CompilationWithAnalyzers context information to compute its diagnostics, hence is specially handled and invoked in the IDE diagnostic service.
e230dcb to
053d5ac
Compare
|
Can you show a picture of this in actino? |
...RemoveUnnecessarySuppressions/CSharpRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.cs
Show resolved
Hide resolved
...RemoveUnnecessarySuppressions/CSharpRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.cs
Show resolved
Hide resolved
| protected override int CompilerErrorCodeDigitCount => 4; | ||
| protected override ISyntaxFacts SyntaxFacts => CSharpSyntaxFacts.Instance; | ||
| protected override (Assembly assembly, string typeName) GetCompilerDiagnosticAnalyzerInfo() | ||
| => (typeof(SyntaxKind).Assembly, CompilerDiagnosticAnalyzerNames.CSharpCompilerAnalyzerTypeName); |
There was a problem hiding this comment.
slightly worried to see Assembly here... will hold off judgemnt to see how this is used.
...Fixes/RemoveUnnecessarySuppressions/RemoveUnnecessaryAttributeSuppressionsCodeFixProvider.cs
Show resolved
Hide resolved
Sure, updated the PR description. |
...eUnnecessarySuppressions/VisualBasicRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.vb
Outdated
Show resolved
Hide resolved
...eUnnecessarySuppressions/VisualBasicRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.vb
Outdated
Show resolved
Hide resolved
...ditorFeatures/CSharpTest/Diagnostics/Suppression/RemoveUnnecessaryPragmaSuppressionsTests.cs
Show resolved
Hide resolved
...moveUnnecessarySuppressions/AbstractRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
...moveUnnecessarySuppressions/AbstractRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
...moveUnnecessarySuppressions/AbstractRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.cs
Show resolved
Hide resolved
...moveUnnecessarySuppressions/AbstractRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
...moveUnnecessarySuppressions/AbstractRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.cs
Show resolved
Hide resolved
...moveUnnecessarySuppressions/AbstractRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.cs
Show resolved
Hide resolved
...moveUnnecessarySuppressions/AbstractRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.cs
Show resolved
Hide resolved
...moveUnnecessarySuppressions/AbstractRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.cs
Show resolved
Hide resolved
...odeFixes/RemoveUnnecessarySuppressions/RemoveUnnecessaryPragmaSuppressionsCodeFixProvider.cs
Show resolved
Hide resolved
...moveUnnecessarySuppressions/AbstractRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
...moveUnnecessarySuppressions/AbstractRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
...moveUnnecessarySuppressions/AbstractRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
...moveUnnecessarySuppressions/AbstractRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
…mavasani/roslyn into RemoveUnncessaryCompilerPragmas
|
@tmat @sharwell Can one of you please take a look at IDE engine changes? @CyrusNajmabadi gave his approval offline for the analyzer/fixer changes, but would prefer one of you to review the remainder of changes. Thanks! |
| } | ||
|
|
||
| [Theory, CombinatorialData] | ||
| internal async Task TestPragmaSuppressionsAnalyzer(BackgroundAnalysisScope analysisScope) |
There was a problem hiding this comment.
Test for IDE diagnostic incremental analyzer changes.
| return await _compilationWithAnalyzers.GetAnalyzerSemanticDiagnosticsAsync(model, adjustedSpan, ImmutableArray.Create(analyzer), cancellationToken).ConfigureAwait(false); | ||
| } | ||
|
|
||
| // We specially handle IPragmaSuppressionsAnalyzer by passing in the 'CompilationWithAnalyzers' |
There was a problem hiding this comment.
IDE diagnostic incremental analyzer change for open file analysis.
|
|
||
| public static ImmutableDictionary<DiagnosticAnalyzer, DiagnosticAnalysisResultBuilder> ToResultBuilderMap( | ||
| this AnalysisResult analysisResult, | ||
| ImmutableArray<Diagnostic> additionalPragmaSuppressionDiagnostics, |
There was a problem hiding this comment.
This file has changes to IDE diagnostic analyzer execution for OOP execution.
| bool includeSuppressedDiagnostics; | ||
| if (diagnosticIds.Contains(IDEDiagnosticIds.RemoveUnnecessarySuppressionDiagnosticId)) | ||
| { | ||
| diagnosticIdsForDiagnosticProvider = null; |
There was a problem hiding this comment.
It feels weird to have setting this to null create this implicit behavior. Can we refactor this code to make this a less implicit side-effect in the codebase?
There was a problem hiding this comment.
Yeah, but using null diagnosticId is used at multiple places in the IDE diagnostic service in features layer, so I kept it consistent.
I think we need a cleanup across the services to replace null diagnostic IDs with some better representation.
|
Thank you @jmarolf! |
…ressions Fixes dotnet#44178 Builds on top of dotnet#44848, which added support to detect unnecessary pragma suppressions.
Fixes #44177
Addresses part of #44178
NOTE: This analyzer can only be executed from IDE, not in command line builds due to the following requirements/restrictions: