Skip to content

lint: full libclang-based Rust AST subsystem for structural lints (deferred) #3493

Open

Description

@zackees

Part of #3481 (Tier 3, deferred).

Task

The original directive for the Singleton lint asked for it to be built on the "clang query protocol" — libclang's AST cursor API, not regex. The current SingletonElisionChecker in ci/lint_cpp_rs/src/checkers/singleton_elision.rs uses regex + brace-depth tracking (same style as the other FileContentCheckers in the crate). This is pragmatic for one rule but doesn't scale.

Why AST-based

  • Regex-based checkers cannot distinguish a namespace-scope int gFoo from a local int gFoo when both fit on similar lines with unusual context.
  • Templates, macros, and multi-line declarations regularly fool the brace-depth heuristic.
  • Clang's hasGlobalStorage() matcher is the correct predicate for "this variable is at namespace scope and emits storage" — no ambiguity.
  • Rebuilding this once for the whole crate enables cheap addition of future structural rules (dependency-direction, layer-violation, cross-TU consistency).

Approach

  1. Add clang-sys / clang crate dep to ci/lint_cpp_rs/Cargo.toml, gated behind a libclang feature so systems without libclang installed can still run the regex checkers.
  2. Introduce a new checker trait AstChecker alongside FileContentChecker.
  3. Parse each TU once (via a shared TranslationUnit cache) and walk cursors, feeding each cursor to every registered AstChecker.
  4. Reimplement SingletonElisionChecker as an AstChecker using Cursor::kind() == CXCursor_VarDecl && cursor.storage_class() == CX_SC_Static && cursor.linkage() != CX_Linkage_NoLinkage.
  5. Keep the regex version as a fallback for environments without libclang.

Acceptance criteria

  • bash lint produces identical SingletonElisionChecker output whether run with the regex or AST backend (mod false-positive fixes exposed by the more accurate AST predicate).
  • At least one additional AST checker migrated from the Python ci/ scripts to demonstrate the pattern (candidates: SingletonInHeadersChecker, or a new dependency-direction rule).
  • Documentation added to agents/docs/linter-architecture.md.

Scope note

This is a substantial effort (2–3 weeks). Deliberately deferred until after Tier 1 and Tier 2 have proven the Singleton-elision rule is worth having. Do NOT block Tier 1 or Tier 2 on this.

Activity

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

    @zackees
    MemberAuthor

    Queue classification corrected: this issue is explicitly marked “Tier 3, deferred” in both its title and body, but it lacked the deferred label and was therefore being counted as actionable.

    The deferral remains valid. This is a 2–3 week cross-platform libclang subsystem (feature-gated parser, shared TU cache, new checker trait, regex fallback, checker migration, parity validation, and documentation). In addition, #3492 established that the current Singleton rule first needs a corrected semantic target—dynamic initialization / retained .init_array contributors—before AST parity is meaningful.

    Restart criteria:

    1. Specify the corrected structural rule and its expected corpus independently of the current regex output.
    2. Confirm libclang distribution/loading strategy on Windows, Linux, and macOS CI.
    3. Benchmark parse/TU-cache cost against the current lint budget.
    4. Split the subsystem foundation, first AST checker, fallback/parity lane, and second checker migration into bounded PRs.
    5. Remove deferred only when an owner and CI dependency strategy are assigned.

    No implementation work will be performed while this label is present, per the burn-down policy.

  3. added
    deferred
    on Aug 20, 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: cideferredpriority: lowtype: feature

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions