Skip to content

Mock generated-code nullable regression guards leave compiler warnings suppressed #7436

Closed

Description

@thomhurst

Summary

Mock generator nullable regression helpers can pass even when generated implementations contain nullable warnings. Generated source starts with #pragma warning disable, which compiler WarningsAsErrors cannot override. Restore relevant diagnostics in test compilations and prove enforcement with a deliberately invalid positive control.

Reproduction

Inspect src/TUnit.Mocks.SourceGenerator/MockGenerator.cs (implementation header) and tests/TUnit.Mocks.SourceGenerator.Tests/SnapshotTestBase.cs:221 on main d87516c. AssertGeneratedCodeHasNoNullableWarnings compiles generated trees without restoring the disabled warnings.

Proposal

Re-enable relevant nullable diagnostics in test-only generated trees before compiling and add a positive control proving that an injected nullable mismatch fails the guard. Preserve production generated output and existing reviewed snapshots unless a separate production defect requires change.

This is the nonblocking CodeRabbit follow-up on #7414; call-site CS8634 enforcement remains useful for that fix.

Acceptance criteria

  • In test-only generated syntax trees, restore nullable diagnostics after the generated warning suppression; leave production output unchanged.
  • Cover the nullable-event regression diagnostics CS8604 and CS8612–CS8615 as well as the existing nullable guards. Project-level WarningsAsErrors alone cannot override generated #pragma warning disable.
  • Add a deliberately invalid generated nullable-event signature (or equivalent targeted positive control) and prove the guard fails; verify the valid nullable-event regressions pass.
  • Run the affected mock source-generator tests and review any intentional snapshot changes.

The generated-code enforcement requirement from #7432 is consolidated here. PR #7414 removes the unsupported pragmas and adds project-level warning promotion, but does not restore diagnostics disabled inside generated files.

Activity

  1. thomhurst commented on Nov 3, 2026

    @thomhurst
    OwnerAuthor

    Audited against main d87516cb9c: MockGenerator.cs:415 still disables all generated warnings, and SnapshotTestBase.RunGeneratorWithCompilationDiagnostics compiles those trees unchanged. Added explicit acceptance criteria for the remaining nullable-event enforcement from #7432, including a failing positive control. This remains separate work from #7414.

  2. added
    bugSomething isn't working
    P3Low: cleanup, minor enhancement, or rare edge case
    on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Low: cleanup, minor enhancement, or rare edge casebugSomething isn't workingin-progressWork is in progress

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions