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 support to detect unnecessary inline SuppressMessageAttribute suppressions #45765

Conversation

mavasani
Copy link
Contributor

@mavasani mavasani commented Jul 7, 2020

Strongly recommended to review commit by commit

Fixes #44178

Builds on top of #44848, which added support to detect unnecessary pragma suppressions.

image

  1. 4a91b03: Add ISyntaxFacts.GetTopLevelAndMethodLevelMembers to get all the declared top level and method level members from a tree
  2. 1d4a9ce: Split and move down ISemanticFactsService to shared layer. This commit has no semantic changes, only splitting the service and moving down existing code to lower shared layer. This service is now 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.
  3. 73fd51e: Enhance pragma suppressions analyzer support to detect unnecessary local SuppressMessageAttribute suppressions. (NOTE: I have not renamed the existing analyzer/fixer/test file names for ease of review. I will do the file renames as a follow-up PR or commit).

…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.
@mavasani mavasani added this to the 16.8.P1 milestone Jul 7, 2020
@mavasani mavasani requested review from CyrusNajmabadi, a team and jmarolf July 7, 2020 22:16
Comment on lines +177 to +212
// 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);
Copy link
Contributor Author

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.

@mavasani
Copy link
Contributor Author

Ping for reviews...

Copy link
Contributor

@jmarolf jmarolf left a comment

Choose a reason for hiding this comment

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

:shipit:

@mavasani
Copy link
Contributor Author

Thanks @jmarolf!

@mavasani mavasani merged commit b5edce4 into dotnet:master Jul 13, 2020
@ghost ghost modified the milestones: 16.8.P1, Next Jul 13, 2020
@mavasani mavasani deleted the UnncessaryLocalSuppressMessageAttributeSuppressions branch July 13, 2020 15:48
mavasani added a commit to mavasani/roslyn that referenced this pull request Jul 16, 2020
…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.
@JoeRobich JoeRobich modified the milestones: Next, 16.8.P1 Jul 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unnecessary pragma/SuppressMessage suppressions for analyzer diagnostics
3 participants