Skip to content

Add missing #pragma warning disable to generated code headers#210

Merged
Skymly merged 2 commits into
mainfrom
fix/pragma-warning-disable
Jul 4, 2026
Merged

Add missing #pragma warning disable to generated code headers#210
Skymly merged 2 commits into
mainfrom
fix/pragma-warning-disable

Conversation

@Skymly

@Skymly Skymly commented Jul 2, 2026

Copy link
Copy Markdown
Owner

Summary

  • Add #pragma warning disable CS1591,CS8019,CS0162,CS0612,CS0618 to the auto-generated header trivia in GeneratedCodeHelper.CreateAutoGeneratedHeader. This is emitted before the using directives and applies to all generated code.
  • The XML doc on GeneratedCodeHelper claimed to add #pragma warning disable but the implementation had none. Generated code can trigger warnings (CS1591 missing XML doc, CS8019 unused using, CS0162 unreachable code, CS0612/CS0618 obsolete) that become errors in consumer projects with TreatWarningsAsErrors=true.
  • 55 Verify snapshots updated (Verify strips leading trivia in its default scrubber, so the #pragma line is not visible in snapshots but is present in the actual generated source — verified via a temporary assertion test).

Suppressed warning codes

Code Reason
CS1591 Missing XML doc on generated internal/private members
CS8019 Unnecessary using directive (TFM-conditional usings)
CS0162 Unreachable code (conditional #if paths)
CS0612 Obsolete member (non-formatted)
CS0618 Obsolete member (formatted)

Related Issue

Closes #209

Solution module

  • SourceGenerators (DesignPatterns.SourceGenerators/)

Type of change

  • Bug fix

Test plan

  • dotnet build DesignPatterns.slnx -c Release — 0 errors, 0 warnings
  • dotnet test DesignPatterns.slnx -c Release --no-build — all tests pass
  • Temporary assertion test confirmed #pragma warning disable present in generated source text (test removed before commit)
  • 55 Verify snapshots updated

Breaking changes

  • None

@Skymly Skymly left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Review: PR #210 — Add #pragma warning disable to generated code headers

Clean implementation. Adds #pragma warning disable CS1591,CS8019,CS0162,CS0612,CS0618 to the auto-generated header, suppressing warnings that generated code may trigger but consumers shouldn't see.

What's good

  • PragmaWarningDisableCodes constant centralizes the warning codes — easy to maintain.
  • Warning code selection is reasonable:
    • CS1591 — missing XML doc comment (generated code doesn't have XML docs)
    • CS8019 — unnecessary using directive (generated code may include usings that aren't always needed)
    • CS0162 — unreachable code (generated code may have conditional branches)
    • CS0612 / CS0618 — obsolete member usage (generated code may reference APIs marked obsolete)
  • ParseLeadingTrivia correctly parses the pragma directive with newline.
  • All Verify snapshots updated to include the new pragma line.

Observation

  • The pragma is added to the header trivia (before #nullable enable), which means it's in scope for the entire generated file. This is correct — the pragma should cover all generated code.
  • Note: this PR and PR #208 both modify GeneratedCodeHelper.cs and the same verified.txt files. They will need to be merged sequentially with a rebase in between.

No blocking issues. Approve.

Skymly added 2 commits July 4, 2026 11:44
The GeneratedCodeHelper XML doc claimed to add #pragma warning disable
directives but the implementation had none. Generated code can trigger
warnings (CS1591 missing XML doc, CS8019 unused using, CS0162 unreachable
code, CS0612/CS0618 obsolete) that become errors in consumer projects
with TreatWarningsAsErrors=true.

Add #pragma warning disable CS1591,CS8019,CS0162,CS0612,CS0618 to the
auto-generated header trivia, emitted before the using directives.
This is independent of the #nullable enable placement (issue #207) and
applies to all generators using CreateAutoGeneratedHeader.

55 Verify snapshots updated.

Closes #209
…ing disable

Rebase onto main (which includes PR #208 #nullable enable placement fix)
required updating all Verify snapshots to reflect both changes:
- #nullable enable now appears before namespace (from #208)
- #pragma warning disable appears in header (from #210)
@Skymly Skymly force-pushed the fix/pragma-warning-disable branch from f135793 to 0db4336 Compare July 4, 2026 03:52
@Skymly Skymly merged commit 22a5a85 into main Jul 4, 2026
3 checks passed
@Skymly Skymly deleted the fix/pragma-warning-disable branch July 4, 2026 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add missing #pragma warning disable to generated code headers

1 participant