Repository navigation
docs(cpp): add 8 code-gen rules from 2-week CodeRabbit survey (closes #2717) - #2718
Conversation
Closes #2717. Adds 8 new rules to agents/docs/cpp-standards.md capturing the recurring themes CodeRabbit flagged across 94 PRs (2026-05-20 → 2026-06-03, 59 with CodeRabbit activity, ~95 substantive findings). All 8 are preventable at code-generation time with a written rule. Rules added (in order, append-only at end of file): 1. Singleton-Stored Configuration: Own by Value, Never by Borrowed Pointer — prevents use-after-scope when callers pass a stack temporary to a `set_X(const T*)` that stashes the pointer in process state. Pairs with the Public Settings Pattern (PR #2715 — where the setter lives; this rule — how it stores the value). 2. In-Place Profile Mutation Bumps a Version — cache-invalidation contract for `set_X(T* obj, ...)` patterns. Without a version bump and key, derived-value caches keyed on `(obj_ptr, ...)` return stale data when the same pointer is mutated across frames. 3. Contract Change Fanout — broadens the existing "API Unit Change" rule (§595-604) from `_ms` / `_us` suffixes to any bound, enum range, or struct shape change. PRs must touch producer + validator + tests + docs + debug-metrics in one diff. 4. `fl::span` for Callback Typedefs and Small Fixed-Size Arrays — extends the existing span rule with two shapes that kept slipping through: `fl::function<...(const u8*, size_t)>` typedefs and `const float xy[2]` fixed-size arrays. 5. ISR-Shared State: `fl::atomic` or Critical Section, Never Bare `volatile` — uses `fl::atomic<T>` (not `std::atomic`, per the project-wide "prefer fl:: over std::" rule) with explicit memory order, OR `portENTER_CRITICAL_ISR` guarded access. Plain `volatile` is not sufficient on dual-core MCUs. 6. Unsigned Narrowing Cast Must Clamp — sibling to the existing Signed Integer Overflow rule. `static_cast<u8>(x)` wraps silently when x > 255 — defined behavior, wrong behavior. The signed-UB rule explicitly does NOT cover this case. 7. Test Mirrors Must Call Production Symbols, Not Redeclare Them — local lambda re-implementations in test files silently pass when the production code regresses. Test must `#include` and call the production symbol. 8. ASCII-Only in Source Files — emoji and Unicode math glyphs (`⚠️ `, `×`, `−`, `ρ`, `∆`) in C++ comments and Python source confuse toolchains and obscure compile errors. Markdown (`*.md`) files exempt. Rule dropped from the original recommendation: mirror the KeyboardInterrupt pattern into `agents/docs/python-standards.md` — on inspection the rule is already present there at lines 17-46. The recurring violations come from agents reading other docs first; that is a discovery problem, not a documentation gap. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
📝 WalkthroughWalkthroughAdds eight C++ coding-standard rules to documentation addressing ownership, cache contracts, contract-change fanout, span usage, ISR/shared-state, unsigned narrowing casts, test mirroring, and ASCII-only sources; also adds conditional FreeRTOS includes to an ESP32 LCD SPI driver header. ChangesC++ Standards Documentation
ESP32 driver include
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
agents/docs/cpp-standards.md (2)
273-276: ⚡ Quick winDocument scoped exception for deferred callback-shape migrations.
This rule is good as the default, but it should explicitly allow documented temporary exceptions for legacy layers that require full-layer atomic migration (to avoid one-off typedef churn and inconsistent handler APIs).
Based on learnings: In
src/fl/stl/cstdio.h, raw pointer+length handler signatures are intentionally retained until a full-layer sweep.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@agents/docs/cpp-standards.md` around lines 273 - 276, Update the cpp-standards.md rule `#1` to explicitly allow a documented, scoped exception for legacy layers that cannot migrate typedefs atomically: add a short paragraph after the rule stating that temporary exceptions are permitted when a full-layer sweep is required, and require the exception to be recorded in the codebase (e.g., a comment pointing to the migration plan) and to reference the legacy location (for example src/fl/stl/cstdio.h) where raw pointer+length handlers are intentionally retained; mention the requirement that such exceptions be timeboxed and revisited during the next layer migration to avoid permanent divergence.
193-219: ⚡ Quick winClarify nullable-reset exception for singleton setters.
The hard “MUST take
const T&” wording conflicts with established copy-by-value setters that acceptconst T*only to supportnullptrreset semantics (while still never storing the pointer). Please add a narrow exception so this rule enforces ownership without banning valid reset APIs.Suggested wording tweak
-**Core Principle**: Any public `fl::set_*` API that stores a configuration profile / settings object in process-wide (or static / namespace-scope) state MUST take the value `const T&` and **copy** it. +**Core Principle**: Any public `fl::set_*` API that stores a configuration profile / settings object in process-wide (or static / namespace-scope) state MUST store by value (copy/move) and never retain caller-owned pointers. +Setter parameters should prefer `const T&` (or `T&&`), except when `nullptr` is explicitly used as a reset signal; in that case, pointer-taking APIs are allowed only if they immediately copy on non-null and never store the pointer.Based on learnings: “Read cpp-standards.md for writing/editing C++ code or creating API wrapper types.”
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@agents/docs/cpp-standards.md` around lines 193 - 219, Update the rule text for fl::set_* setters to add a narrow exception allowing APIs that accept a const T* only to express nullable-reset semantics, but require that such setters never store the raw pointer and instead copy/move the pointed-to value into the library-owned storage (or clear it when nullptr); in practice change the Rule 1 wording and the Check Process to state that a const T* parameter is acceptable only if the implementation of the setter (e.g. set_rgbww_colorimetric_profile) copies the pointed-to value into a value-typed namespace/static slot or resets that owned slot on nullptr, and flag as HIGH only when both the parameter and the storage are raw const T* (i.e., the pointer itself is stored).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@agents/docs/cpp-standards.md`:
- Around line 273-276: Update the cpp-standards.md rule `#1` to explicitly allow a
documented, scoped exception for legacy layers that cannot migrate typedefs
atomically: add a short paragraph after the rule stating that temporary
exceptions are permitted when a full-layer sweep is required, and require the
exception to be recorded in the codebase (e.g., a comment pointing to the
migration plan) and to reference the legacy location (for example
src/fl/stl/cstdio.h) where raw pointer+length handlers are intentionally
retained; mention the requirement that such exceptions be timeboxed and
revisited during the next layer migration to avoid permanent divergence.
- Around line 193-219: Update the rule text for fl::set_* setters to add a
narrow exception allowing APIs that accept a const T* only to express
nullable-reset semantics, but require that such setters never store the raw
pointer and instead copy/move the pointed-to value into the library-owned
storage (or clear it when nullptr); in practice change the Rule 1 wording and
the Check Process to state that a const T* parameter is acceptable only if the
implementation of the setter (e.g. set_rgbww_colorimetric_profile) copies the
pointed-to value into a value-typed namespace/static slot or resets that owned
slot on nullptr, and flag as HIGH only when both the parameter and the storage
are raw const T* (i.e., the pointer itself is stored).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: dcdc517f-5f31-45bf-acdc-9da831aaba1e
📒 Files selected for processing (1)
agents/docs/cpp-standards.md
…agma keep Same pre-existing IWYU violation as on PR #2715 — this branch was cut from master before that fix landed, so the linter flags it again. Same one-line fix: `// IWYU pragma: keep` on each freertos system header. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Two "Quick win" nitpicks from CodeRabbit on the survey-rules PR: 1. **Rule 1 (Singleton-Stored Configuration)** — softens the "MUST take const T&" wording to allow `const T*` parameters when the pointer is used as a nullable-reset signal (nullptr → revert to default). The existing `set_rgbww_colorimetric_profile(const T*)` is the in-repo example of this idiom; it copies on non-null and resets on null, which is correct even though the parameter shape is `T*`. The HIGH-severity flag now fires only when BOTH parameter and storage are `const T*` (the actual use-after-scope shape). 2. **Rule 4 (`fl::span` for callback typedefs)** — adds a scoped exception for deferred legacy migrations. A `(ptr, size)` typedef may be retained temporarily when migrating all callers in one PR isn't tractable, provided the typedef site carries a comment pointing at the tracking issue and the exception is timeboxed. Cited example: `src/fl/stl/cstdio.h`. Without this carve-out the rule would push engineers toward unprincipled mixed APIs. Both rules remain "use value semantics / spans by default" — the exceptions are explicit, documented, and bounded. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
Addressed both CodeRabbit nitpicks in commit 18f7426: Rule 1 (Singleton-Stored Configuration) — added a nullable-reset exception. Rule 4 ( Both defaults remain "value semantics / spans"; the exceptions are explicit, documented, and bounded. |
… form #2718 also touched the FreeRTOS-include block in lcd_spi/channel_driver_lcd_clockless.cpp.hpp and picked the inline `// IWYU pragma: keep` form (cleaner than the `begin_keep`/`end_keep` wrapper this branch had added as a drive-by). Taking master's resolution; both forms satisfy the same lint check and master is the canonical style.
Closes #2717.
Summary
Surveyed CodeRabbit's findings across 94 PRs updated 2026-05-20 → 2026-06-03 (59 with CodeRabbit activity, ~95 substantive findings examined). Adds 8 new rules to
agents/docs/cpp-standards.mdcapturing the recurring themes that are preventable at code-generation time by a written rule.See issue #2717 for the full survey methodology, raw statistics, and the dropped recommendations.
Rules added (in order, append-only)
fl::spanfor Callback Typedefs and Small Fixed-Size Arraysfl::atomicor Critical Section, Never BarevolatileKey design notes
fl::atomic<T>, NOTstd::atomic<T>. FastLED disallowsstd::types except for a narrow exception list (std::move,std::forward, low-level metaprogramming traits) — AVR / older embedded toolchains don't ship a usable<atomic>. Verifiedfl::atomic_u32exists insrc/fl/stl/shared_ptr.h:29._ms,_us,_bytes); the new rule covers any bound / shape change.T[N]fixed-size arrays.python-standards.md. On inspection it's already at lines 17-46 — the recurring violations are a discovery problem (agents reading other docs first), not a documentation gap.Test plan
bash lintclean (all four pipelines).Related PRs
No textual conflict with #2715 / #2716 — they edit different files (
code_review.md,architecture-reviewer-agent.md,.coderabbit.yaml,src/FastLED.h,ci/lint_cpp/) and a different section ofcpp-standards.md(Public Settings Pattern is appended above Naming Conventions; this PR appends below). Whichever lands second will merge cleanly.🤖 Generated with Claude Code
Summary by CodeRabbit