From f1ef96b1d31a2876953ceb0a3ea1c563c47587e9 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Wed, 18 Nov 2026 09:19:32 +0530 Subject: [PATCH] fix(browser): make the wasm32 browser library clippy-clean and lint it in CI Co-Authored-By: Claude Sonnet 5.5 --- .github/workflows/lint.yml | 18 +++++++++++++++++- CHANGELOG.md | 4 ++++ src/browser/indexeddb.rs | 4 ++++ src/browser/mod.rs | 6 ++++-- src/graph/types.rs | 7 +++++-- src/lib.rs | 1 + src/storage/backend/mod.rs | 3 --- src/storage/mod.rs | 1 + src/storage/persistent_facts.rs | 7 +++++++ 9 files changed, 43 insertions(+), 8 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 27662da543..2d9efd1490 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -3,7 +3,8 @@ name: Lint # rustfmt and clippy in one job: formatting takes a second once the toolchain # is installed. Clippy lints the library and binary with every feature; tests, # examples and benches are not linted (the panic-prevention lints in Cargo.toml -# would need thousands of changes there). wasm.yml lints the wasm targets. +# would need thousands of changes there). A separate job lints the browser +# library on wasm32, including code that native builds do not compile. on: push: @@ -34,3 +35,18 @@ jobs: - name: Run clippy run: cargo clippy --all-features -- -D warnings + + wasm-clippy: + name: Clippy (wasm32 browser library) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Install Rust toolchain + uses: dtolnay/rust-toolchain@stable + with: + targets: wasm32-unknown-unknown + components: clippy + + - name: Run clippy + run: cargo clippy --target wasm32-unknown-unknown --features browser --lib -- -D warnings diff --git a/CHANGELOG.md b/CHANGELOG.md index d610387d34..c8682dac05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **IndexedDB flushes are atomic, settle on abort, and refuse later writes after a failure (#894)**, backported from v3 for the next v2.x patch. A synchronous exception while queueing pages aborts the whole batch; an abort with no request error rejects instead of hanging, and repeated request errors cannot throw from a consumed callback. The failed operation reports `STG-048` naming the IndexedDB write and browser exception (page-preparation errors retain their own code). Subsequent transact/retract, checkpoint and import calls report `STG-046` without changing memory. Overlapping flushes run in order; a checkpoint waits for earlier writes even when it has no dirty pages. Queries, rules and export remain available, including rejected work already applied in memory; export is not a durability acknowledgement. Discard the failed handle and reopen to recover the successful prefix before the failure. A failed import leaves IndexedDB unchanged. Format v7, the IndexedDB schema, public methods, healthy write volume and read-only open behavior are unchanged. Native v2.x behavior is unchanged; the two error codes retain their v3 meanings. +### CI + +- Lint the browser library on `wasm32-unknown-unknown` with warnings denied in `lint.yml`, alongside the existing native gate. Gate unused native internals and clean up browser-only lints without changing behavior, the public API or format v7. + ### Documentation - Fix the Rust Quick Start (#884): register recursive rule clauses in separate `execute()` calls and define the keyword entity IDs used by prepared queries. Clarify the entity ID in the prepared-query error example. diff --git a/src/browser/indexeddb.rs b/src/browser/indexeddb.rs index 032881f0ea..41727a5302 100644 --- a/src/browser/indexeddb.rs +++ b/src/browser/indexeddb.rs @@ -164,6 +164,8 @@ impl IndexedDbBackend { let open_request = idb_factory.open_with_u32(db_name, 1)?; // Create the object store if this is a fresh database (version upgrade). + // Preserve the callback's existing trap on unexpected upgrade failures. + #[allow(clippy::unwrap_used)] let on_upgrade: Closure = Closure::once(move |event: web_sys::Event| { let target = event.target().unwrap(); @@ -205,6 +207,8 @@ impl IndexedDbBackend { let mut pages = HashMap::with_capacity(keys_arr.length() as usize); for i in 0..keys_arr.length() { let key = keys_arr.get(i); + // Preserve v2's truncating, saturating conversion of numeric IndexedDB keys. + #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] let page_id = key .as_f64() .ok_or_else(|| JsValue::from_str("page_id is not a number"))? diff --git a/src/browser/mod.rs b/src/browser/mod.rs index 88c3274142..fcf93d77ce 100644 --- a/src/browser/mod.rs +++ b/src/browser/mod.rs @@ -215,6 +215,8 @@ impl BrowserDb { #[wasm_bindgen(js_name = exportGraph)] pub fn export_graph(&self) -> Result { let inner = self.inner.borrow(); + // page_count_raw returns the HashMap's usize length widened to u64; this round-trips. + #[allow(clippy::cast_possible_truncation)] let page_count = inner .pfs .with_backend(|b| b.page_count_raw()) @@ -243,7 +245,7 @@ impl BrowserDb { pub async fn import_graph(&self, data: js_sys::Uint8Array) -> Result<(), JsValue> { self.inner.borrow().refuse_failed_flush()?; let bytes = data.to_vec(); - if bytes.len() % crate::storage::PAGE_SIZE != 0 { + if !bytes.len().is_multiple_of(crate::storage::PAGE_SIZE) { return Err(JsValue::from_str( "import data length is not a multiple of PAGE_SIZE", )); @@ -362,7 +364,7 @@ impl BrowserDb { f.tx_id = tx_id; f.tx_count = tx_count; if f.asserted && f.valid_from == VALID_FROM_USE_TX_TIME { - f.valid_from = tx_id as i64; + f.valid_from = tx_id.cast_signed(); } f }) diff --git a/src/graph/types.rs b/src/graph/types.rs index 2eadab8fc1..fa1d18f3c5 100644 --- a/src/graph/types.rs +++ b/src/graph/types.rs @@ -1,5 +1,4 @@ use serde::{Deserialize, Serialize}; -use std::time::{SystemTime, UNIX_EPOCH}; use uuid::Uuid; // ============================================================================ @@ -20,10 +19,14 @@ pub(crate) fn tx_id_now() -> TxId { // On WASM browser targets, `std::time::SystemTime::now()` panics. // Use `js_sys::Date::now()` which returns milliseconds since the Unix // epoch as an f64 (same precision as `Date.now()` in JavaScript). - js_sys::Date::now() as u64 + // Preserve the existing truncating, saturating conversion (including NaN → 0). + #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] + let ms = js_sys::Date::now() as u64; + ms } #[cfg(not(all(target_arch = "wasm32", feature = "browser")))] { + use std::time::{SystemTime, UNIX_EPOCH}; u64::try_from( SystemTime::now() .duration_since(UNIX_EPOCH) diff --git a/src/lib.rs b/src/lib.rs index 80ce28b87d..a44d5d0d8c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,6 +82,7 @@ pub(crate) mod query; pub mod repl; pub(crate) mod storage; pub(crate) mod temporal; +#[cfg(not(target_arch = "wasm32"))] pub(crate) mod wal; #[cfg(all(target_arch = "wasm32", feature = "browser"))] diff --git a/src/storage/backend/mod.rs b/src/storage/backend/mod.rs index f24e835c18..7ff154ef68 100644 --- a/src/storage/backend/mod.rs +++ b/src/storage/backend/mod.rs @@ -21,6 +21,3 @@ pub use memory::MemoryBackend; #[cfg(not(target_arch = "wasm32"))] #[allow(unused_imports)] pub use file::FileBackend; - -#[cfg(all(target_arch = "wasm32", feature = "browser"))] -pub use crate::browser::buffer::BrowserBufferBackend; diff --git a/src/storage/mod.rs b/src/storage/mod.rs index 9a409e653f..a3e1bed0bd 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -9,6 +9,7 @@ pub mod backend; pub mod btree; pub mod btree_v6; pub mod cache; +#[cfg(not(target_arch = "wasm32"))] pub(crate) mod dir_sync; pub mod index; pub mod packed_pages; diff --git a/src/storage/persistent_facts.rs b/src/storage/persistent_facts.rs index 9eac695d76..b50ae2a5d1 100644 --- a/src/storage/persistent_facts.rs +++ b/src/storage/persistent_facts.rs @@ -1065,6 +1065,7 @@ impl PersistentFactStorage { /// The `last_checkpointed_tx_count` recorded in the on-disk header. /// /// Used by WAL replay to skip entries already present in the main file. + #[cfg(not(target_arch = "wasm32"))] pub fn last_checkpointed_tx_count(&self) -> u64 { self.last_checkpointed_tx_count } @@ -1078,11 +1079,13 @@ impl PersistentFactStorage { /// /// Used by checkpoint to ensure save() always writes even if no new /// facts have been added since the last save. + #[cfg(not(target_arch = "wasm32"))] pub fn force_dirty(&mut self) { self.mark_dirty(); } /// Check if storage has unsaved changes + #[cfg(not(target_arch = "wasm32"))] pub fn is_dirty(&self) -> bool { self.dirty } @@ -1104,6 +1107,8 @@ impl PersistentFactStorage { where F: FnOnce(&B) -> R, { + // Preserve the existing panic on poison; recovering would expose suspect state. + #[allow(clippy::unwrap_used)] let guard = self.backend.lock().unwrap(); f(&*guard) } @@ -1116,6 +1121,8 @@ impl PersistentFactStorage { where F: FnOnce(&mut B) -> R, { + // Preserve the existing panic on poison; recovering would expose suspect state. + #[allow(clippy::unwrap_used)] let mut guard = self.backend.lock().unwrap(); f(&mut *guard) }