Skip to content

Add support for skipping analyzing banned API analysis in generated files#82713

Open
333fred wants to merge 4 commits intodotnet:mainfrom
333fred:banned-api-option
Open

Add support for skipping analyzing banned API analysis in generated files#82713
333fred wants to merge 4 commits intodotnet:mainfrom
333fred:banned-api-option

Conversation

@333fred
Copy link
Member

@333fred 333fred commented Mar 11, 2026

Adds a new .editorconfig/.globalconfig options for the banned API analyzer to skip analyzing generated code. Closes #82114.

Microsoft Reviewers: Open in CodeFlow

This comment was marked as resolved.

…iles

Adds a new `.editorconfig`/`.globalconfig` options for the banned API analyzer to skip analyzing generated code. Closes dotnet#82114.
@333fred 333fred force-pushed the banned-api-option branch from 209c313 to 807db56 Compare March 12, 2026 00:20
@333fred 333fred requested a review from Copilot March 12, 2026 00:21
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

@333fred 333fred requested a review from Copilot March 12, 2026 22:23
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

@333fred 333fred marked this pull request as ready for review March 16, 2026 18:17
@333fred 333fred requested a review from a team as a code owner March 16, 2026 18:17
@333fred
Copy link
Member Author

333fred commented Mar 16, 2026

@jjonescz for review

});

return !generatedCodeOptions.ExcludesGeneratedCode ||
!(isGeneratedCode ??
Copy link
Member

@jjonescz jjonescz Mar 17, 2026

Choose a reason for hiding this comment

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

It feels like we could return fast if isGeneratedCode is false (and perhaps the other checks we do in this ?? chain) and don't go into the complex GetOrAdd code path above in that case.

protected override IEnumerable<SyntaxNode> GetTypeSyntaxNodesFromBaseType(SyntaxNode syntaxNode) => ((BaseListSyntax)syntaxNode).Types.Select(t => (SyntaxNode)t.Type);

protected override bool IsRegularCommentOrDocumentationComment(SyntaxTrivia trivia)
=> trivia.Kind() is SyntaxKind.SingleLineCommentTrivia or SyntaxKind.MultiLineCommentTrivia or SyntaxKind.ShebangDirectiveTrivia or SyntaxKind.SingleLineDocumentationCommentTrivia or SyntaxKind.MultiLineDocumentationCommentTrivia;
Copy link
Member

Choose a reason for hiding this comment

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

Other callers like CSharpCompilation.CreateAnalyzerDriver only consider the first two kinds. Should we unify this logic?

context => VerifyAttributes(context.ReportDiagnostic, context.Symbol.GetAttributes(), context.CancellationToken),
context =>
{
VerifyAttributes(context.ReportDiagnostic, context.Symbol.GetAttributes(), context.IsGeneratedCode, context.CancellationToken);
Copy link
Member

Choose a reason for hiding this comment

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

What is context.IsGeneratedCode going to be if context.Symbol is partial and one part is in generated code and other is not? It looks like currently this symbol-level flag has precedence but shouldn't we prefer the tree-level flags instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

It will return false. My thinking was that this is fine, but as I try to type out a justification for that I'm failing convince myself now. I'll update it.

}
"""),
("Test0.cs", """
[{|#0:Banned|}]
Copy link
Member

Choose a reason for hiding this comment

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

Do we have a test when the attribute is in the generated file?

if (applicationSyntaxReference == null)
continue;

if (applicationSyntaxReference.SyntaxTree == null)
Copy link
Member

Choose a reason for hiding this comment

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

It doesn't look like SyntaxTree property is nullable. Is this null check necessary?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Analyzer config option to exclude generated files from BannedApiAnalyzer

4 participants