Files
codex/prs/bolinfest/study/PR-2864-study.md
2025-09-02 15:17:45 -07:00

2.8 KiB
Raw Blame History

DOs

  • Bold the core idea: Gate debug-only commands with #[cfg(debug_assertions)] everywhere theyre referenced.
// enum definition
enum SlashCommand {
    Diff,
    Mention,
    Status,
    Mcp,
    Quit,
    #[cfg(debug_assertions)]
    TestApproval,
}

// usage
impl SlashCommand {
    fn is_public(&self) -> bool {
        match self {
            SlashCommand::Diff
            | SlashCommand::Mention
            | SlashCommand::Status
            | SlashCommand::Mcp
            | SlashCommand::Quit => true,

            #[cfg(debug_assertions)]
            SlashCommand::TestApproval => true,
        }
    }
}
  • Bold the core idea: Keep user-facing commands (e.g., Diff) enabled in release builds.
// Good: `Diff` is always available; no cfg guard
SlashCommand::Diff
| SlashCommand::Mention
| SlashCommand::Status
| SlashCommand::Mcp
| SlashCommand::Quit => true,
  • Bold the core idea: Match cfg attributes on arms with the enums cfg to avoid missing-variant errors in release.
// Variant is debug-only…
#[cfg(debug_assertions)]
SlashCommand::TestApproval => true;
// …so there is no arm for it when not(debug_assertions)
  • Bold the core idea: If a variant exists in all builds but behavior differs, provide both arms so matches stay exhaustive.
#[cfg(debug_assertions)]
SlashCommand::TestApproval => true,
#[cfg(not(debug_assertions))]
SlashCommand::TestApproval => false,
  • Bold the core idea: Validate both debug and release to catch cfg mistakes early.
# Debug (default)
cargo build -p codex-tui
cargo test -p codex-tui

# Release (CI parity)
cargo build --release -p codex-tui

DONTs

  • Bold the core idea: Dont disable the wrong command; dont gate Diff to “fix” release—gate the test-only command.
// Bad: gating a user command
#[cfg(debug_assertions)]
SlashCommand::Diff => true;
  • Bold the core idea: Dont reference a debug-only variant without guarding it.
// Bad: breaks in release if `TestApproval` is not compiled in
SlashCommand::TestApproval => true;

// Good
#[cfg(debug_assertions)]
SlashCommand::TestApproval => true,
  • Bold the core idea: Dont leave variants unmatched under some cfg; ensure exhaustiveness in all configurations.
// Bad: `TestApproval` removed from enum in release, but arm remains
SlashCommand::TestApproval => true; // compile error in release
  • Bold the core idea: Dont duplicate the same variant across arms without cfg separation.
// Bad: conflicting arms (unreachable/ambiguous)
SlashCommand::Diff => true,
SlashCommand::Diff => false,

// Good: use cfg to separate build-specific behavior
#[cfg(debug_assertions)]
SlashCommand::TestApproval => true,
#[cfg(not(debug_assertions))]
SlashCommand::TestApproval => false,