Make the analysis of generated code opt-in - #1425
Merged
Merged
Conversation
Analyzing generated code costs build time, and most projects never report anything in it, so the rules now skip it unless the MEZIANTOU_ANALYZER_GENERATED_CODE environment variable opts in with "1" or "true". The rules that need generated code to be correct, such as MA0053, and the ones that report in it by default, such as the Blazor rules, are unchanged, as the flags they pass to ConfigureAnalysisOfGeneratedCode are still the minimum. report_generated_code only decides what a rule reports among the code it analyzes, so it now needs the environment variable to report in generated code with a rule that skips it. Setting it to false still always works.
meziantou
enabled auto-merge (squash)
September 6, 2026 22:01
This was referenced Sep 6, 2026
Closed
This was referenced Sep 7, 2026
Closed
This was referenced Oct 4, 2026
Open
Bump Meziantou.Analyzer from 3.0.139 to 3.0.294
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#587
Open
Bump Meziantou.Analyzer from 3.0.139 to 3.0.294
Analogy-LogViewer/Analogy.AspNetCore.LogProvider#566
Open
Open
Open
Open
Open
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix #1424
Follow-up to #1423, which added
MEZIANTOU_ANALYZER_GENERATED_CODEas an opt-out. This flips it to an opt-in: the rules now skip generated code unless the variable is set.What changed
AnalysisContextExtensions.GetAdditionalFlagsreturnsAnalyze | ReportDiagnosticsonly when the variable is1ortrue(trimmed, case-insensitive), andNonefor anything else, including the variable being unset.Each rule's own flags remain the minimum, so nothing changes for the rules that pass more than
None:Nonereport_generated_codedecide)AnalyzeAnalyze | ReportDiagnosticsWhy
Analyzing generated code costs build time, and a project where nothing reports in generated code pays it for nothing, which is the vast majority of them: you cannot fix code you do not own. Making it opt-in means only the projects that ask for it pay the cost.
Notes for reviewers
Two behavior changes are worth knowing, both covered by tests:
report_generated_code = trueis no longer sufficient on its own for the 145 rules that skip generated code, as it only decides what a rule reports among the code it analyzes. It now needs the environment variable too. Setting it tofalsestill always works, as a rule can only report what it is allowed to report.[GeneratedCode]or[DebuggerNonUserCode]as generated. A symbol marked with one of those attributes in a hand written file is therefore skipped by default, where previously only the file of the diagnostic mattered.Also updated: the
BannedSymbols.txtmessage, the XML documentation ofConfigureAnalysisOfGeneratedCodeandGeneratedCodeReporting,docs/generated-code.md, the README section, and theGenerated codeguidance inAGENTS.md.Tests
GeneratedCodeAnalysisTestssimulates the variable with anOptedInflags constant, as the rules read it once per process. Added coverage for the two new defaults (a rule configured withreport_generated_codebut not opted in reports nothing, and a rule opted in without configuration reports nothing) and for both sides of the[GeneratedCode]case.AnalysisContextExtensionsTestswas flipped to opted-in/not-opted-in values.dotnet testpasses on the five Roslyn versions: 19808 tests, 0 failures.dotnet run --project src/DocumentationGeneratorexits 0 with no markdown change.