Skip to content

lint: split integer-constant globals into PreferConstexprChecker #3483

Closed

Description

@zackees

Part of #3481 (Tier 0).

Task

The scan (ci/scan_singleton_elision.py) reports ~30 hits that are NOT true Singleton candidates — they are integer / const-int compile-time constants that should be constexpr (compiler will inline, no storage emitted). Bucketing them under the Singleton warning conflates two very different fixes.

Examples

// src/platforms/esp/32/drivers/channel_manager_esp32.cpp.hpp
int PRIORITY_FORCE   = 4;   // should be `constexpr int`
int PRIORITY_PARLIO  = 3;
int PRIORITY_LCD_RGB = 2;
// ... 14 total in this file

// src/fl/audio/detector/equalizer.cpp.hpp
const int kBassStart   = 20;   // should be `constexpr int`
const int kBassEnd     = 250;
// ... 6 total in this file

// src/platforms/arm/rp/rpcommon/spi_hw_manager_rp.cpp.hpp
int PRIORITY_SPI_HW_8 = 3;

Concrete steps

  1. Create ci/lint_cpp_rs/src/checkers/prefer_constexpr.rs as a sibling to singleton_elision.rs.
  2. Match namespace-scope int / const int / unsigned etc. definitions with a trivial integer literal RHS (int literal, hex, binary, single-token bit-shift like (1u << 5)).
  3. Emit warning: "namespace-scope NAME is a compile-time integer constant; use constexpr so the compiler inlines it and no storage is emitted".
  4. Move the trivial-literal-with-non-constexpr detection OUT of SingletonElisionChecker so the two rules don't overlap.
  5. Register in processor_registry_cli.rs (warn-only).

Acceptance criteria

  • bash lint reports ~30 PreferConstexprChecker warnings distinct from ~85 SingletonElisionChecker warnings.
  • No overlap between the two rules.
  • Fixing a hit by adding constexpr silences both.

Activity

  1. added
    type: feature
    priority: low
    area: ci
    on Jul 1, 2026
  2. zackees commented on Jul 29, 2026

    @zackees
    MemberAuthor

    This issue is now almost entirely resolved — the ~30 integer constants it was filed against are gone from the checker output.

    Neither cited example still appears. Current fastled-lint --checker singleton_elision on master (34 findings) contains no hits in either file:

    • channel_manager_esp32.cpp.hpp (cited: 14 hits, PRIORITY_FORCE / PRIORITY_PARLIO / PRIORITY_LCD_RGB) — 0
    • equalizer.cpp.hpp (cited: 6 hits, kBassStart / kBassEnd) — 0

    The original count came from ci/scan_singleton_elision.py, which still reports 101. The Rust checker reports 34. That gap is mostly #3754's real literal tokenizer removing false positives, so 34 is the truer number and the constant-splitting work has largely already happened.

    What was actually left: one item. MCPWM_TIMER_TAG in gpio_isr_rx/mcpwm_timer.cpp.hpp was static const char* — a mutable pointer occupying storage on top of the string literal. Fixed to static constexpr const char* in the linked PR.


    One open design question, worth a maintainer decision

    The remaining set contains a case this issue's framing doesn't cover, and it blocks #3492.

    objectfled_spi_mode.cpp.hpp:100 is flagged:

    static constexpr u32 kSpiMaxOutputWords = kSpiMaxInputBytes * kSpiWordsPerByte;

    It is already static constexpr — already the thing a "split constants out" fix would produce. It is flagged only because the triviality test accepts a literal RHS but not one computed from other constants.

    That matters for #3492 (promote to hard-fail when the count reaches zero): this finding cannot be driven to zero. The warning says "wrap in fl::Singleton<T>", which is not meaningful advice for a compile-time scalar — it emits no storage unless ODR-used, and if it is ODR-used the remedy is to stop taking its address, not to wrap it. The only way to clear it today is an FL_LINT_ALLOW_GLOBAL annotation on something that is already correct.

    I prototyped skipping constexpr scalars (keeping constexpr tables flagged, since an array is far more likely to be ODR-used through a pointer and then really does occupy flash) — and reverted it, because singleton_elision_still_flags_non_literal_constexpr in lint_core/tests.rs deliberately pins the current behaviour for exactly these RHS forms (kMin * 2.0f, kOther + 1), added under #3486. That is a documented decision and not mine to flip unilaterally.

    So, one of:

    1. Skip constexpr scalars, keep constexpr tables — updates that test; makes the count reachable-to-zero.
    2. Keep as-is — then lint: promote SingletonElisionChecker to hard-fail (Tier 3, ratchet) #3492 needs to accept annotations on already-correct constexpr as its terminal state.

    Option 1 is what I'd suggest, but it changes a tested contract, so flagging rather than doing.

  3. moved this from Triage to Done in FastLED Trackeron Jul 31, 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

    area: cipriority: lowtype: feature

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions