-
Notifications
You must be signed in to change notification settings - Fork 4k
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 support to detect unnecessary inline SuppressMessageAttribute suppressions #45765
Add support to detect unnecessary inline SuppressMessageAttribute suppressions #45765
Conversation
…clared top level and method level members from a tree
…ce is split into two parts, similar to `ISyntaxFacts` and `ISyntaxFactsService`: 1. `ISemanticFacts`, which is purely semantic query API, is now accessible from shared analyzer and code fixes layer. 2. `ISemanticFactsService`, which is semantic query + code transformation API as well as an `ILanguageService`, is only accessible from shared code fixes layer. It stays as being accessible via GetLanguageService API.
…ressions Fixes dotnet#44178 Builds on top of dotnet#44848, which added support to detect unnecessary pragma suppressions.
// Compute all the reported compiler and analyzer diagnostics for diagnostic IDs corresponding to pragmas in the tree. | ||
var (diagnostics, unhandledIds) = await GetReportedDiagnosticsForIdsAsync( | ||
idsToAnalyze, root, semanticModel, compilationWithAnalyzers, | ||
getSupportedDiagnostics, getIsCompilationEndAnalyzer, compilerDiagnosticIds, cancellationToken).ConfigureAwait(false); | ||
|
||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
// Iterate through reported diagnostics which are suppressed in source through pragmas and mark the corresponding pragmas as used. | ||
await ProcessReportedDiagnosticsAsync(diagnostics, tree, compilationWithAnalyzers, idToPragmasMap, | ||
pragmasToIsUsedMap, idToSuppressMessageAttributesMap, suppressMessageAttributesToIsUsedMap, cancellationToken).ConfigureAwait(false); | ||
|
||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
// Remove entries for unhandled diagnostic ids. | ||
foreach (var id in unhandledIds) | ||
{ | ||
foreach (var (pragma, _) in idToPragmasMap[id]) | ||
{ | ||
pragmasToIsUsedMap.Remove(pragma); | ||
} | ||
|
||
if (idToSuppressMessageAttributesMap.TryGetValue(id, out var attributeNodes)) | ||
{ | ||
foreach (var attributeNode in attributeNodes) | ||
{ | ||
suppressMessageAttributesToIsUsedMap.Remove(attributeNode); | ||
} | ||
|
||
idToSuppressMessageAttributesMap.Remove(id); | ||
} | ||
} | ||
|
||
// Finally, report the unnecessary suppressions. | ||
var effectiveSeverity = severity.ToDiagnosticSeverity() ?? s_removeUnnecessarySuppressionDescriptor.DefaultSeverity; | ||
ReportUnnecessarySuppressions(pragmasToIsUsedMap, sortedPragmasWithIds, | ||
suppressMessageAttributesToIsUsedMap, reportDiagnostic, effectiveSeverity, compilationWithAnalyzers.Compilation); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of this is just existing code, which now shows up as new code as bunch of existing code was refactored into ProcessPragmaDirectives
.
Ping for reviews... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jmarolf! |
…osticAnalyzer Fixes dotnet#46047 dotnet#45765 enhance this analyzer to support detecting unnecessary inline SuppressMessageAttribute suppressions. This led to a regression when processing idToPragmasMap for unhandled IDs, which is fixed by this change.
Strongly recommended to review commit by commit
Fixes #44178
Builds on top of #44848, which added support to detect unnecessary pragma suppressions.
ISyntaxFacts.GetTopLevelAndMethodLevelMembers
to get all the declared top level and method level members from a treeISyntaxFacts
andISyntaxFactsService
:ISemanticFacts
, which is purely semantic query API, is now accessible from shared analyzer and code fixes layer.ISemanticFactsService
, which is semantic query + code transformation API as well as anILanguageService
, is only accessible from shared code fixes layer. It stays as being accessible via GetLanguageService API.