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

Do not run 'remove unnecessary imports' on generated code #74762

Merged

Conversation

CyrusNajmabadi
Copy link
Member

Fixes #74761.

  1. this can be very expensive.
  2. it's unlikely the user can do anything about this. the code is generated, so it's not like they can remove the imports from teh generated code.

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner August 14, 2024 20:50
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Aug 14, 2024
@@ -71,8 +71,6 @@ private static ImmutableArray<DiagnosticDescriptor> GetDescriptors(LocalizableSt
protected abstract bool IsRegularCommentOrDocComment(SyntaxTrivia trivia);
protected abstract IUnnecessaryImportsProvider<TSyntaxNode> UnnecessaryImportsProvider { get; }

protected override GeneratedCodeAnalysisFlags GeneratedCodeAnalysisFlags => GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics;
Copy link
Member

@Youssef1313 Youssef1313 Aug 15, 2024

Choose a reason for hiding this comment

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

That way I think -generatedCodeClassificationIdDescriptor can be removed along with the check that selects the descriptor based on whether this is generated code or not

@@ -186,22 +186,9 @@ public async Task TestGeneratedCode()
var source = """
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add a [WorkItem] to the test to clarify, why no diagnostics there are expected

Copy link
Member

@Youssef1313 Youssef1313 left a comment

Choose a reason for hiding this comment

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

LGTM, but one thing to note. Do we know for sure that this is actually a performance issue? In some cases, the analyzer can be showing as expensive because it's calculating something that will happen anyways during the compilation (i.e, the cost is just moving around between the rest of the compilation process and the analyzer).

Copy link
Member

@sharwell sharwell left a comment

Choose a reason for hiding this comment

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

This is a red herring. Remove unnecessary imports has no actual performance impact. It incorrectly captures unavoidable performance overhead from a different part of the system, suggesting it can be removed by disabling this analyzer when in fact it cannot.

@CyrusNajmabadi
Copy link
Member Author

@sharwell what value does this analyzer have though? The user cannot change the usings added. we don't run other style analyzers on generated code, why do we run this one.

Remove unnecessary imports has no actual performance impact.

Then we shouldn't report that it does. It also makes the other analyzers look good by comparison.

I'd still like to take this though, if there's no use value provided.

@sharwell
Copy link
Member

I have found value in this feature while writing source generators.

@CyrusNajmabadi CyrusNajmabadi dismissed sharwell’s stale review August 16, 2024 23:12

I thnik we should take this.

@CyrusNajmabadi
Copy link
Member Author

The best code is no code. I don't see why we are special casing this style analyzer for generated code, and nothing else.

Copy link
Member

@akhera99 akhera99 left a comment

Choose a reason for hiding this comment

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

looks good, I agree that the test should have some indication that the behavior has changed

@CyrusNajmabadi CyrusNajmabadi merged commit 29e09e0 into dotnet:main Nov 14, 2024
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead VSCode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bad performance in CSharpRemoveUnnecessaryImportsDiagnosticAnalyzer with System.Text.Json source generator
6 participants