From a7100d8e9b3cd3fcdf2e10b419149353fbe4703f Mon Sep 17 00:00:00 2001 From: Lauri Virtanen Date: Sun, 24 Nov 2024 19:27:01 +0200 Subject: [PATCH 1/2] Autofix clippy issues with `cargo fix --clippy` --- bindings/wasm/lib.rs | 5 +---- core/io/linux.rs | 2 +- core/lib.rs | 14 +++++++------- core/storage/btree.rs | 8 ++++---- core/storage/sqlite3_ondisk.rs | 1 - core/storage/wal.rs | 6 +++--- core/vdbe/mod.rs | 2 +- simulator/main.rs | 7 +++---- test/src/lib.rs | 7 +++---- 9 files changed, 23 insertions(+), 29 deletions(-) diff --git a/bindings/wasm/lib.rs b/bindings/wasm/lib.rs index 2e1d0dd22..036a7ad1d 100644 --- a/bindings/wasm/lib.rs +++ b/bindings/wasm/lib.rs @@ -57,10 +57,7 @@ impl Statement { #[wasm_bindgen] pub fn raw(mut self, toggle: Option) -> Self { - self.raw = match toggle { - Some(toggle) => toggle, - None => true, - }; + self.raw = toggle.unwrap_or(true); self } diff --git a/core/io/linux.rs b/core/io/linux.rs index 3a2de5f42..fde8a9616 100644 --- a/core/io/linux.rs +++ b/core/io/linux.rs @@ -70,7 +70,7 @@ impl LinuxIO { } impl InnerLinuxIO { - pub fn get_iovec<'a>(&'a mut self, buf: *const u8, len: usize) -> &'a iovec { + pub fn get_iovec(&mut self, buf: *const u8, len: usize) -> &iovec { let iovec = &mut self.iovecs[self.next_iovec]; iovec.iov_base = buf as *mut std::ffi::c_void; iovec.iov_len = len; diff --git a/core/lib.rs b/core/lib.rs index c1dccaa14..b7344c260 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -187,7 +187,7 @@ impl Connection { match cmd { Cmd::Stmt(stmt) => { let program = Rc::new(translate::translate( - &*self.schema.borrow(), + &self.schema.borrow(), stmt, self.header.clone(), self.pager.clone(), @@ -212,18 +212,18 @@ impl Connection { match cmd { Cmd::Stmt(stmt) => { let program = Rc::new(translate::translate( - &*self.schema.borrow(), + &self.schema.borrow(), stmt, self.header.clone(), self.pager.clone(), - Rc::downgrade(&self), + Rc::downgrade(self), )?); let stmt = Statement::new(program, self.pager.clone()); Ok(Some(Rows { stmt })) } Cmd::Explain(stmt) => { let program = translate::translate( - &*self.schema.borrow(), + &self.schema.borrow(), stmt, self.header.clone(), self.pager.clone(), @@ -235,7 +235,7 @@ impl Connection { Cmd::ExplainQueryPlan(stmt) => { match stmt { ast::Stmt::Select(select) => { - let plan = prepare_select_plan(&*self.schema.borrow(), select)?; + let plan = prepare_select_plan(&self.schema.borrow(), select)?; let (plan, _) = optimize_plan(plan)?; println!("{}", plan); } @@ -257,7 +257,7 @@ impl Connection { match cmd { Cmd::Explain(stmt) => { let program = translate::translate( - &*self.schema.borrow(), + &self.schema.borrow(), stmt, self.header.clone(), self.pager.clone(), @@ -268,7 +268,7 @@ impl Connection { Cmd::ExplainQueryPlan(_stmt) => todo!(), Cmd::Stmt(stmt) => { let program = translate::translate( - &*self.schema.borrow(), + &self.schema.borrow(), stmt, self.header.clone(), self.pager.clone(), diff --git a/core/storage/btree.rs b/core/storage/btree.rs index b3298ecc4..3a7c833fa 100644 --- a/core/storage/btree.rs +++ b/core/storage/btree.rs @@ -273,7 +273,7 @@ impl BTreeCursor { } } - if cell_idx >= contents.cell_count() + 1 { + if cell_idx > contents.cell_count() { // end let has_parent = self.stack.current() > 0; if has_parent { @@ -876,7 +876,7 @@ impl BTreeCursor { .insert(overflow_cell.index, to_static_buf(&overflow_cell.payload)); } *self.write_info.rightmost_pointer.borrow_mut() = - page_copy.rightmost_pointer().clone(); + page_copy.rightmost_pointer(); self.write_info.page_copy.replace(Some(page_copy)); @@ -1222,7 +1222,7 @@ impl BTreeCursor { fn allocate_page(&self, page_type: PageType, offset: usize) -> Rc> { let page = self.pager.allocate_page().unwrap(); - btree_init_page(&page, page_type, &*self.database_header.borrow(), offset); + btree_init_page(&page, page_type, &self.database_header.borrow(), offset); page } @@ -1423,7 +1423,7 @@ impl BTreeCursor { // return SQLITE_CORRUPT_PAGE(pPage); // } // don't count header and cell pointers? - nfree = nfree - first_cell as usize; + nfree -= first_cell as usize; nfree as u16 } diff --git a/core/storage/sqlite3_ondisk.rs b/core/storage/sqlite3_ondisk.rs index 590f94a86..bdfa09832 100644 --- a/core/storage/sqlite3_ondisk.rs +++ b/core/storage/sqlite3_ondisk.rs @@ -48,7 +48,6 @@ use crate::storage::database::DatabaseStorage; use crate::storage::pager::{Page, Pager}; use crate::types::{OwnedRecord, OwnedValue}; use crate::{File, Result}; -use cfg_block::cfg_block; use log::trace; use std::cell::RefCell; use std::pin::Pin; diff --git a/core/storage/wal.rs b/core/storage/wal.rs index b86e06efb..774e57cc9 100644 --- a/core/storage/wal.rs +++ b/core/storage/wal.rs @@ -159,7 +159,7 @@ impl Wal for WalFile { &page, db_size, write_counter, - &*header, + &header, checksums, )?; self.last_checksum.replace(checksums); @@ -235,9 +235,9 @@ impl Wal for WalFile { } if *self.syncing.borrow() { - return Ok(CheckpointStatus::IO); + Ok(CheckpointStatus::IO) } else { - return Ok(CheckpointStatus::Done); + Ok(CheckpointStatus::Done) } } } diff --git a/core/vdbe/mod.rs b/core/vdbe/mod.rs index 2b2033405..033cd0de5 100644 --- a/core/vdbe/mod.rs +++ b/core/vdbe/mod.rs @@ -2227,7 +2227,7 @@ impl Program { let rows = Rows { stmt }; let mut schema = RefCell::borrow_mut(&conn.schema); // TODO: This function below is synchronous, make it not async - parse_schema_rows(Some(rows), &mut *schema, conn.pager.io.clone())?; + parse_schema_rows(Some(rows), &mut schema, conn.pager.io.clone())?; state.pc += 1; } } diff --git a/simulator/main.rs b/simulator/main.rs index c09ea508e..3b7a5e404 100644 --- a/simulator/main.rs +++ b/simulator/main.rs @@ -1,5 +1,4 @@ use limbo_core::{Connection, Database, File, OpenFlags, PlatformIO, Result, RowResult, IO}; -use log; use rand::prelude::*; use rand_chacha::ChaCha8Rng; use std::cell::RefCell; @@ -279,7 +278,7 @@ fn gen_random_text(env: &mut SimulatorEnv) -> String { let size = env.rng.gen_range(1024..max_size); let mut name = String::new(); for i in 0..size { - name.push(((i % 26) as u8 + 'A' as u8) as char); + name.push(((i % 26) as u8 + b'A') as char); } name } else { @@ -527,7 +526,7 @@ impl limbo_core::File for SimulatorFile { } fn size(&self) -> Result { - Ok(self.inner.size()?) + self.inner.size() } } @@ -573,7 +572,7 @@ impl Value { Value::Integer(i) => i.to_string(), Value::Float(f) => f.to_string(), Value::Text(t) => format!("'{}'", t.clone()), - Value::Blob(vec) => to_sqlite_blob(&vec), + Value::Blob(vec) => to_sqlite_blob(vec), } } } diff --git a/test/src/lib.rs b/test/src/lib.rs index 63ee44f1f..1f338bb37 100644 --- a/test/src/lib.rs +++ b/test/src/lib.rs @@ -340,8 +340,8 @@ mod tests { log::debug!("counting"); let list_query = "SELECT count(x) FROM test"; loop { - match conn.query(list_query).unwrap() { - Some(ref mut rows) => loop { + if let Some(ref mut rows) = conn.query(list_query).unwrap() { + loop { match rows.next_row()? { RowResult::Row(row) => { let first_value = &row.values[0]; @@ -357,8 +357,7 @@ mod tests { } RowResult::Done => break, } - }, - None => {} + } } } } From afeb1cbe74c60884fc396faaff3aa1b0da60f7f2 Mon Sep 17 00:00:00 2001 From: Lauri Virtanen Date: Sun, 24 Nov 2024 19:51:58 +0200 Subject: [PATCH 2/2] Clippy warning fixes --- core/storage/btree.rs | 4 +--- core/storage/sqlite3_ondisk.rs | 2 +- core/translate/emitter.rs | 1 - core/vdbe/mod.rs | 38 +++++++++++++++++----------------- simulator/main.rs | 6 +++--- 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/core/storage/btree.rs b/core/storage/btree.rs index 3a7c833fa..c9e6721da 100644 --- a/core/storage/btree.rs +++ b/core/storage/btree.rs @@ -9,7 +9,6 @@ use crate::types::{Cursor, CursorResult, OwnedRecord, OwnedValue, SeekKey, SeekO use crate::Result; use std::cell::{Ref, RefCell}; -use std::i32; use std::pin::Pin; use std::rc::Rc; @@ -875,8 +874,7 @@ impl BTreeCursor { scratch_cells .insert(overflow_cell.index, to_static_buf(&overflow_cell.payload)); } - *self.write_info.rightmost_pointer.borrow_mut() = - page_copy.rightmost_pointer(); + *self.write_info.rightmost_pointer.borrow_mut() = page_copy.rightmost_pointer(); self.write_info.page_copy.replace(Some(page_copy)); diff --git a/core/storage/sqlite3_ondisk.rs b/core/storage/sqlite3_ondisk.rs index bdfa09832..3f7935491 100644 --- a/core/storage/sqlite3_ondisk.rs +++ b/core/storage/sqlite3_ondisk.rs @@ -1190,7 +1190,7 @@ pub fn payload_overflows( pub fn checksum_wal( buf: &[u8], - wal_header: &WalHeader, + _wal_header: &WalHeader, input: (u32, u32), native_endian: bool, // Sqlite interprets big endian as "native" ) -> (u32, u32) { diff --git a/core/translate/emitter.rs b/core/translate/emitter.rs index b1ba2d2bf..5f3402389 100644 --- a/core/translate/emitter.rs +++ b/core/translate/emitter.rs @@ -1,7 +1,6 @@ use std::cell::RefCell; use std::collections::HashMap; use std::rc::{Rc, Weak}; -use std::usize; use sqlite3_parser::ast; diff --git a/core/vdbe/mod.rs b/core/vdbe/mod.rs index 033cd0de5..7ad1522f7 100644 --- a/core/vdbe/mod.rs +++ b/core/vdbe/mod.rs @@ -2823,7 +2823,7 @@ fn exec_cast(value: &OwnedValue, datatype: &str) -> OwnedValue { match affinity(datatype) { // NONE Casting a value to a type-name with no affinity causes the value to be converted into a BLOB. Casting to a BLOB consists of first casting the value to TEXT in the encoding of the database connection, then interpreting the resulting byte sequence as a BLOB instead of as TEXT. // Historically called NONE, but it's the same as BLOB - Affinity::BLOB => { + Affinity::Blob => { // Convert to TEXT first, then interpret as BLOB // TODO: handle encoding let text = value.to_string(); @@ -2831,12 +2831,12 @@ fn exec_cast(value: &OwnedValue, datatype: &str) -> OwnedValue { } // TEXT To cast a BLOB value to TEXT, the sequence of bytes that make up the BLOB is interpreted as text encoded using the database encoding. // Casting an INTEGER or REAL value into TEXT renders the value as if via sqlite3_snprintf() except that the resulting TEXT uses the encoding of the database connection. - Affinity::TEXT => { + Affinity::Text => { // Convert everything to text representation // TODO: handle encoding and whatever sqlite3_snprintf does OwnedValue::Text(Rc::new(value.to_string())) } - Affinity::REAL => match value { + Affinity::Real => match value { OwnedValue::Blob(b) => { // Convert BLOB to TEXT first let text = String::from_utf8_lossy(b); @@ -2847,7 +2847,7 @@ fn exec_cast(value: &OwnedValue, datatype: &str) -> OwnedValue { OwnedValue::Float(f) => OwnedValue::Float(*f), _ => OwnedValue::Float(0.0), }, - Affinity::INTEGER => match value { + Affinity::Integer => match value { OwnedValue::Blob(b) => { // Convert BLOB to TEXT first let text = String::from_utf8_lossy(b); @@ -2871,7 +2871,7 @@ fn exec_cast(value: &OwnedValue, datatype: &str) -> OwnedValue { } _ => OwnedValue::Integer(0), }, - Affinity::NUMERIC => match value { + Affinity::Numeric => match value { OwnedValue::Blob(b) => { let text = String::from_utf8_lossy(b); cast_text_to_numeric(&text) @@ -2885,11 +2885,11 @@ fn exec_cast(value: &OwnedValue, datatype: &str) -> OwnedValue { } enum Affinity { - INTEGER, - TEXT, - BLOB, - REAL, - NUMERIC, + Integer, + Text, + Blob, + Real, + Numeric, } /// For tables not declared as STRICT, the affinity of a column is determined by the declared type of the column, according to the following rules in the order shown: @@ -2903,26 +2903,26 @@ fn affinity(datatype: &str) -> Affinity { // Note: callers of this function must ensure that the datatype is uppercase. // Rule 1: INT -> INTEGER affinity if datatype.contains("INT") { - return Affinity::INTEGER; + return Affinity::Integer; } // Rule 2: CHAR/CLOB/TEXT -> TEXT affinity if datatype.contains("CHAR") || datatype.contains("CLOB") || datatype.contains("TEXT") { - return Affinity::TEXT; + return Affinity::Text; } // Rule 3: BLOB or empty -> BLOB affinity (historically called NONE) if datatype.contains("BLOB") || datatype.is_empty() { - return Affinity::BLOB; + return Affinity::Blob; } // Rule 4: REAL/FLOA/DOUB -> REAL affinity if datatype.contains("REAL") || datatype.contains("FLOA") || datatype.contains("DOUB") { - return Affinity::REAL; + return Affinity::Real; } // Rule 5: Otherwise -> NUMERIC affinity - Affinity::NUMERIC + Affinity::Numeric } /// When casting a TEXT value to INTEGER, the longest possible prefix of the value that can be interpreted as an integer number @@ -2971,7 +2971,7 @@ fn cast_text_to_real(text: &str) -> OwnedValue { OwnedValue::Float(0.0) } -/// NUMERIC Casting a TEXT or BLOB value into NUMERIC yields either an INTEGER or a REAL result. +/// NUMERIC Casting a TEXT or BLOB value into NUMERIC yields either an INTEGER or a REAL result. /// If the input text looks like an integer (there is no decimal point nor exponent) and the value /// is small enough to fit in a 64-bit signed integer, then the result will be INTEGER. /// Input text that looks like floating point (there is a decimal point and/or an exponent) @@ -3129,7 +3129,7 @@ mod tests { mock.expect_seek_to_last() .return_once(|| Ok(CursorResult::Ok(()))); mock.expect_rowid() - .return_once(|| Ok(Some(std::i64::MAX as u64))); + .return_once(|| Ok(Some(i64::MAX as u64))); mock.expect_seek() .with(predicate::always(), predicate::always()) .returning(|rowid, _| { @@ -3150,7 +3150,7 @@ mod tests { mock.expect_seek_to_last() .return_once(|| Ok(CursorResult::Ok(()))); mock.expect_rowid() - .return_once(|| Ok(Some(std::i64::MAX as u64))); + .return_once(|| Ok(Some(i64::MAX as u64))); mock.expect_seek() .with(predicate::always(), predicate::always()) .return_once(|_, _| Ok(CursorResult::IO)); @@ -3163,7 +3163,7 @@ mod tests { mock.expect_seek_to_last() .return_once(|| Ok(CursorResult::Ok(()))); mock.expect_rowid() - .return_once(|| Ok(Some(std::i64::MAX as u64))); + .return_once(|| Ok(Some(i64::MAX as u64))); mock.expect_seek() .with(predicate::always(), predicate::always()) .returning(|_, _| Ok(CursorResult::Ok(true))); diff --git a/simulator/main.rs b/simulator/main.rs index 3b7a5e404..3c71bfef5 100644 --- a/simulator/main.rs +++ b/simulator/main.rs @@ -202,7 +202,7 @@ fn do_write(env: &mut SimulatorEnv, conn: &mut Rc) -> Result<()> { // gen insert query for column in &columns { let value = match column.column_type { - ColumnType::Integer => Value::Integer(env.rng.gen_range(std::i64::MIN..std::i64::MAX)), + ColumnType::Integer => Value::Integer(env.rng.gen_range(i64::MIN..i64::MAX)), ColumnType::Float => Value::Float(env.rng.gen_range(-1e10..1e10)), ColumnType::Text => Value::Text(gen_random_text(env)), ColumnType::Blob => Value::Blob(gen_random_text(env).as_bytes().to_vec()), @@ -224,7 +224,7 @@ fn do_write(env: &mut SimulatorEnv, conn: &mut Rc) -> Result<()> { Ok(()) } -fn compare_equal_rows(a: &Vec>, b: &Vec>) { +fn compare_equal_rows(a: &[Vec], b: &[Vec]) { assert_eq!(a.len(), b.len(), "lengths are different"); for (r1, r2) in a.iter().zip(b) { for (v1, v2) in r1.iter().zip(r2) { @@ -577,7 +577,7 @@ impl Value { } } -fn to_sqlite_blob(bytes: &Vec) -> String { +fn to_sqlite_blob(bytes: &[u8]) -> String { let hex: String = bytes.iter().map(|b| format!("{:02X}", b)).collect(); format!("X'{}'", hex) }