Repository navigation
feat(lint): split integer constants out of SingletonElisionChecker into PreferConstexprChecker (#3483) - #3790
Merged
Conversation
…to PreferConstexprChecker (#3483) SingletonElisionChecker reported two unrelated populations under one message: int PRIORITY_PARLIO = 3; // wants `constexpr` QueueHandle_t s_esp_request_queue; // wants `fl::Singleton<T>` The advice differs, so the rules should. "Wrap it in fl::Singleton<T>" is strictly worse advice for an integer constant: the singleton hides storage behind a function-local static the linker *may* drop, while `constexpr` means the compiler inlines the value and there is no storage to drop. Both rules now consume one shared scanner (`scan_namespace_scope_decls`) and partition its output through one predicate, so no declaration can be claimed by both or by neither. The scanner is the ~200 lines of brace-depth, comment-stripping and opt-out-marker tracking that was previously inline in SingletonElisionChecker; a second copy would have drifted. Two false-positive classes had to be excluded, both found by running the rule against the tree rather than by reasoning: - **Mutable state that merely starts at a literal.** `bool sInitialized = false;` matches "integral type bound to an integer literal" exactly like a real constant. The first cut flagged 13 of these -- SysTick flags, driver init flags, saved pin-mux values. Advising `constexpr` there does not compile, and it simultaneously stole the declaration from SingletonElisionChecker where mutable state belongs. Now excluded by scanning for assignment, compound assignment and inc/dec. - **State whose writes live in another translation unit.** `u8 gTimeErrorAccum256ths = 0;` is the only in-file mention of itself; every write happens in `clockless_blocking.h`, which also declares it `extern`. A per-file checker cannot see that, so the rule also reads the sibling header and treats an `extern` declaration or an assignment there as proof of shared mutable state. Net effect on the tree: SingletonElisionChecker still reports 33 across 9 files -- unchanged -- and PreferConstexprChecker reports 0. That zero is the honest result, and worth stating plainly: the issue predicted ~30 hits, but every example it cited (`int PRIORITY_*` in channel_manager_esp32, `const int kBassStart` in equalizer, `PRIORITY_SPI_HW_8` in spi_hw_manager_rp) has already been converted to `constexpr` since it was filed. Those files now carry 15, 7 and 3 `constexpr` declarations respectively. The population the issue describes no longer exists, so the rule's value here is as a guard against new ones plus removing the wrong-advice conflation -- not as a backlog of work. Because there are no in-tree instances, the tests are the entire evidence that the rule fires. They pin the motivating shape, hex/binary/suffixed literals, the two false-positive classes above, comparison-vs-assignment, whole-identifier matching, and -- directly -- that exactly one rule claims any given declaration. Registered warn-only on both the Rust and Python sides (kept in sync; ci/tests/test_warn_only_checkers_sync.py asserts they match). Co-Authored-By: Claude <[email protected]>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (7)
📝 WalkthroughWalkthroughThe PR adds ChangesPrefer constexpr linting
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant LintCLI
participant PreferConstexprChecker
participant scan_namespace_scope_decls
participant SourceFiles
LintCLI->>PreferConstexprChecker: create and run checker
PreferConstexprChecker->>scan_namespace_scope_decls: scan namespace-scope declarations
scan_namespace_scope_decls->>SourceFiles: read source files and sibling headers
SourceFiles-->>scan_namespace_scope_decls: declarations and assignments
scan_namespace_scope_decls-->>PreferConstexprChecker: structured declarations
PreferConstexprChecker-->>LintCLI: constexpr diagnostics
Possibly related PRs
✨ 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3483.
The conflation
SingletonElisionCheckerreported two unrelated populations under one message:The advice differs, so the rules should. "Wrap it in
fl::Singleton<T>" is strictly worse advice for an integer constant: the singleton hides storage behind a function-local static the linker may drop, whileconstexprmeans the compiler inlines the value and there is no storage to drop at all.Approach
Both rules now consume one shared scanner (
scan_namespace_scope_decls) and partition its output through one predicate, so no declaration can be claimed by both or by neither. That scanner is the ~200 lines of brace-depth tracking, comment stripping and opt-out-marker handling that used to be inline inSingletonElisionChecker— a second copy would have drifted.Two false-positive classes, both found by running it rather than reasoning
1. Mutable state that merely starts at a literal.
bool sInitialized = false;matches "integral type bound to an integer literal" exactly like a real constant does. My first cut flagged 13 of these — SysTick flags, driver init flags, saved pin-mux values.constexpr bool sInitialized = false;does not compile the moment anything assigns to it, and it simultaneously stole the declaration fromSingletonElisionChecker, where mutable state belongs. Now excluded by scanning for assignment, compound assignment and inc/dec.2. State whose writes live in another translation unit.
u8 gTimeErrorAccum256ths = 0;is the only mention of itself in its own file — every write happens inclockless_blocking.h, which also declares itextern. A per-file checker cannot see that. The rule now also reads the sibling header and treats anexterndeclaration or an assignment there as proof of shared mutable state.Result on the tree, stated plainly
SingletonElisionChecker: 33 across 9 files — unchanged.PreferConstexprChecker: 0.That zero deserves explanation rather than burial. The issue predicted ~30 hits, but every example it cited has already been converted to
constexprsince it was filed:channel_manager_esp32.cpp.hpp— 14 ×int PRIORITY_*constexpr, no bare intsequalizer.cpp.hpp— 6 ×const int kBass*constexprspi_hw_manager_rp.cpp.hpp—int PRIORITY_SPI_HW_8constexprSo the population the issue describes no longer exists. The rule's value here is (a) removing the wrong-advice conflation and (b) guarding against new instances — not a backlog of work. #3488, which is gated on this split, will therefore not shrink from it.
Tests are the evidence
Since there are no in-tree instances, the tests are the only proof the rule fires. They pin: the motivating shape; hex/binary/suffixed literals; both false-positive classes above; comparison-vs-assignment; whole-identifier matching; and — directly — that exactly one rule claims any given declaration.
One pre-existing test needed updating:
singleton_elision_honors_allow_global_markerusedstatic int sCounter = 0;, which after the split belongs to the new rule. Rather than swap the sample, it now asserts the partition itself (singleton_hits + constexpr_hits == 1), which is the stronger property.Registered warn-only on both Rust and Python sides;
ci/tests/test_warn_only_checkers_sync.pypasses.Verification
soldr cargo test: 52 pass.bash test --cppgreen.ci/tests: 682 passed.Pre-existing, not touched
rust_lint_source_files_stay_under_one_thousand_linesfails onunity_build.rs(1205 lines) — identical on unmodified master.bash lintgates on a gitignored local scratch file (tmp.sh, matched by.gitignore:87) containingpio run -v. Same failure on master; CI never sees the file. That the C++ linter scans gitignored paths at all looks worth a separate look.🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
constexpr.Bug Fixes
Tests
Chores