Repository navigation
feat(lint): forbid raw Serial.* in enforced example sketches; migrate AutoResearch - #2679
Conversation
… AutoResearch Add ExampleSerialChecker to flag raw Serial.print* / Serial.read* / Serial.begin in selected example sketches that should route through the fl:: wrappers. The fl:: wrappers carry the non-blocking HWCDC fixes shipped in #2669 (Serial.set TxTimeoutMs(0), guarded flush, host-presence skip). Bypassing them with raw Serial calls silently loses those guarantees. Enforcement is scoped to examples/AutoResearch/ for now -- tutorial examples (Blink, Cylon, DemoReel100, etc.) intentionally use raw Serial as the canonical Arduino-newcomer surface; broader rollout is a separate decision. Allowed (no fl:: equivalent, platform-specific hardware config): - Serial.setTxBufferSize, Serial.setTxTimeoutMs Opt-out: `// ok serial - <reason>` on the same line. Files: - ci/lint_cpp/example_serial_checker.py: new checker - ci/lint_cpp/run_all_checkers.py: register in checkers_by_scope["examples"] - ci/tests/test_example_serial_checker.py: 16 unit tests (all pass) - examples/AutoResearch/AutoResearch.ino: Serial.begin(115200) -> fl::serial_begin(115200) !Serial -> !fl::serial_ready() setTxBufferSize/setTxTimeoutMs annotated with `// ok serial - <reason>` Co-Authored-By: Claude Opus 4.7 <[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 as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThis PR adds a new CI checker that enforces the use of FastLED serial helpers in example sketches by forbidding raw ChangesSerial Usage Enforcement Checker
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
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 docstrings
🧪 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.
Actionable comments posted: 2
🧹 Nitpick comments (2)
ci/tests/test_example_serial_checker.py (1)
65-105: 💤 Low valueSolid coverage. Once the line-scanner gaps noted in
example_serial_checker.pyare addressed, consider adding cases for an inline block comment alongside code (Serial.println("x"); /* note */) and twoSerial.*calls on one line to lock in the expected violation counts.🤖 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 `@ci/tests/test_example_serial_checker.py` around lines 65 - 105, Add tests to cover the line-scanner gaps in example_serial_checker.py by: 1) adding a test (e.g., test_flags_serial_inline_block_comment) that creates content containing an inline block comment after a Serial call like 'Serial.println("x"); /* note */', calls checker.check_file_content(content), asserts the file is in checker.violations and that the violation message references println (and fl::println if applicable); and 2) adding a test (e.g., test_flags_two_serial_calls_one_line) that creates content with two Serial.* calls on the same line (for example 'Serial.println("a"); Serial.read();'), runs checker.check_file_content(content), and asserts the file is in checker.violations and that there are two violations recorded for that path. Ensure tests use the same _make_content helper and follow the existing assert patterns (checking content.path in checker.violations and verifying counts/messages).ci/lint_cpp/example_serial_checker.py (1)
108-135: ⚡ Quick winHarden
example_serial_checker.pyagainst false negatives in block-comment and multi-call lines.
- Lines containing both
/*and*/are fully skipped (continueat 114-116), soSerial.*appearing on the same line as an inline block comment can be ignored.SERIAL_METHOD_PATTERN.search(code_part)only matches the firstSerial.*per line (131), so additionalSerial.*calls on the same line could be missed.- Current
examples/AutoResearchcontains no such inline/* ... */+Serial.*cases and no multipleSerial.*calls on a single line, but this remains a silent-bypass risk for future edits.Consider stripping just the commented portion on
/* ... */open/close lines (instead of skipping the whole line) and usingfinditerto iterate over allSerial.*matches incode_part.🤖 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 `@ci/lint_cpp/example_serial_checker.py` around lines 108 - 135, The loop logic can miss Serial.* when block comments start and end on the same line and only captures the first match per line; modify the handling around in_multiline_comment and code_part so that when a line contains both "/*" and "*/" you strip only the commented substring (remove the "/*...*/" segment) instead of setting in_multiline_comment and continuing, and when tracking multi-line comment state remove or mask comment spans rather than unconditionally continuing; then replace SERIAL_METHOD_PATTERN.search(code_part) with SERIAL_METHOD_PATTERN.finditer(code_part) and iterate over each match to handle multiple Serial.* calls per line (use the matched groups as before, e.g., match.group(1) for the method) while preserving existing single-line '//' stripping logic.
🤖 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.
Inline comments:
In `@ci/lint_cpp/example_serial_checker.py`:
- Around line 94-95: The __init__ method is missing an explicit return type;
update the constructor declaration for the class in
ci/lint_cpp/example_serial_checker.py to include the explicit return annotation
(e.g. change def __init__(self): to def __init__(self) -> None:), leaving the
existing self.violations: dict[str, list[tuple[int, str]]] assignment unchanged
so the attribute typing remains intact.
- Around line 170-177: Hoist the imports currently inside main(): move the
`import sys` statement and the `MultiCheckerFileProcessor` and
`run_checker_standalone` imports from `ci.util.check_files` to module scope;
update the top-of-file imports to include `sys` and extend the existing `from
ci.util.check_files import ...` to list `MultiCheckerFileProcessor` and
`run_checker_standalone`, and remove those in-function imports so `main()`
simply references `sys`, `MultiCheckerFileProcessor`, and
`run_checker_standalone` directly.
---
Nitpick comments:
In `@ci/lint_cpp/example_serial_checker.py`:
- Around line 108-135: The loop logic can miss Serial.* when block comments
start and end on the same line and only captures the first match per line;
modify the handling around in_multiline_comment and code_part so that when a
line contains both "/*" and "*/" you strip only the commented substring (remove
the "/*...*/" segment) instead of setting in_multiline_comment and continuing,
and when tracking multi-line comment state remove or mask comment spans rather
than unconditionally continuing; then replace
SERIAL_METHOD_PATTERN.search(code_part) with
SERIAL_METHOD_PATTERN.finditer(code_part) and iterate over each match to handle
multiple Serial.* calls per line (use the matched groups as before, e.g.,
match.group(1) for the method) while preserving existing single-line '//'
stripping logic.
In `@ci/tests/test_example_serial_checker.py`:
- Around line 65-105: Add tests to cover the line-scanner gaps in
example_serial_checker.py by: 1) adding a test (e.g.,
test_flags_serial_inline_block_comment) that creates content containing an
inline block comment after a Serial call like 'Serial.println("x"); /* note */',
calls checker.check_file_content(content), asserts the file is in
checker.violations and that the violation message references println (and
fl::println if applicable); and 2) adding a test (e.g.,
test_flags_two_serial_calls_one_line) that creates content with two Serial.*
calls on the same line (for example 'Serial.println("a"); Serial.read();'), runs
checker.check_file_content(content), and asserts the file is in
checker.violations and that there are two violations recorded for that path.
Ensure tests use the same _make_content helper and follow the existing assert
patterns (checking content.path in checker.violations and verifying
counts/messages).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: f5f98fb3-6f39-4e90-8a46-39ed072155df
📒 Files selected for processing (4)
ci/lint_cpp/example_serial_checker.pyci/lint_cpp/run_all_checkers.pyci/tests/test_example_serial_checker.pyexamples/AutoResearch/AutoResearch.ino
- Add explicit `-> None` return type annotation to `ExampleSerialChecker.__init__` - Hoist `sys` and `ci.util.check_files` imports from `main()` to module scope Co-Authored-By: Claude Opus 4.7 <[email protected]>
Summary
Add a new C++ linter (
ExampleSerialChecker) that flags rawSerial.print*/Serial.read*/Serial.begincalls in selected example sketches, and migrateAutoResearch.inoto thefl::wrappers so those sketches actually benefit from the non-blocking HWCDC fixes shipped in #2669.Why
PR #2669 added platform-aware behavior to
src/platforms/arduino/io_arduino.hpp:Serial.setTxTimeoutMs(0)atbegin()time → HWCDC writes drop instead of stalling when no host is readingif (!Serial) return;inprint()/println()→ non-blocking when host absentflush()skipped on!Serial→ avoids arduino-esp32 #7554 hangThese fixes only take effect when the sketch goes through the
fl::API. A sketch that bypasses the wrappers by callingSerial.printdirectly silently loses every guarantee. AutoResearch — the canonical hardware-testing harness — was bypassing them despite the rest of the sketch already usingfl::println/fl::flush(viaAutoResearchRemote.cpp'sprintJsonRaw).What changes
ci/lint_cpp/example_serial_checker.pySerial.{print,println,printf,write,read,available,peek,readStringUntil,flush,begin}in enforced paths; allowssetTxBufferSize/setTxTimeoutMs(platform-specific, nofl::equivalent); honors// ok serial - <reason>opt-out.ci/lint_cpp/run_all_checkers.pycheckers_by_scope["examples"].ci/tests/test_example_serial_checker.pyexamples/AutoResearch/AutoResearch.inoSerial.begin(115200)→fl::serial_begin(115200);!Serial→!fl::serial_ready();setTxBufferSize/setTxTimeoutMsannotated with// ok serial - <reason>.Scope decision: AutoResearch only (for now)
Surveyed
examples/— 53 files use rawSerial.print*. Most are tutorial sketches (Blink, Cylon, DemoReel100, Audio*) where rawSerialis the canonical Arduino-newcomer surface and migration would confuse the audience. Enforcement starts atexamples/AutoResearch/only.Broader rollout is a separate decision — when ready, add the path prefix to
ENFORCED_PATH_PREFIXESinci/lint_cpp/example_serial_checker.pyand migrate as needed.Test plan
uv run python -m pytest ci/tests/test_example_serial_checker.py -v→ 16/16 passinguv run python ci/lint_cpp/example_serial_checker.py examples/AutoResearch/AutoResearch.ino→ no violationsuv run python ci/lint_cpp/example_serial_checker.py(full tree) → no violationsbash test --cpp→ 227/227 passing (cached, repo unchanged from previous run except docs/lint paths)bash compile esp32c6 --examples AutoResearchstill linksOpt-out example
Same-line only; suppression on a separate line does NOT silence the next line (covered by
test_suppression_only_on_same_line).🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Improvements
Tests
Chores