core: fix clippy

This commit is contained in:
sonhmai 2024-08-03 20:04:14 +07:00
parent 7937c165fe
commit 0e7bd95e4e
14 changed files with 56 additions and 73 deletions

View file

@ -39,13 +39,13 @@ pub enum LimboError {
#[macro_export]
macro_rules! bail_parse_error {
($($arg:tt)*) => {
return Err(crate::error::LimboError::ParseError(format!($($arg)*)))
return Err($crate::error::LimboError::ParseError(format!($($arg)*)))
};
}
#[macro_export]
macro_rules! bail_corrupt_error {
($($arg:tt)*) => {
return Err(crate::error::LimboError::Corrupt(format!($($arg)*)))
return Err($crate::error::LimboError::Corrupt(format!($($arg)*)))
};
}

View file

@ -67,7 +67,7 @@ impl IO for DarwinIO {
match cf {
CompletionCallback::Read(ref file, ref c, pos) => {
let mut file = file.borrow_mut();
let c: &Completion = &c;
let c: &Completion = c;
let r = match c {
Completion::Read(r) => r,
Completion::Write(_) => unreachable!(),

View file

@ -376,7 +376,7 @@ impl BTreeCursor {
let buf: &mut [u8] = buf_ref.as_mut_slice();
// copy data
buf[pc as usize..pc as usize + payload.len()].copy_from_slice(&payload);
buf[pc as usize..pc as usize + payload.len()].copy_from_slice(payload);
// memmove(pIns+2, pIns, 2*(pPage->nCell - i));
let (pointer_area_pc_by_idx, _) = page.cell_get_raw_pointer_region();
let pointer_area_pc_by_idx = pointer_area_pc_by_idx + (2 * cell_idx);
@ -575,9 +575,8 @@ impl BTreeCursor {
fn read_page_sync(&mut self, page_idx: usize) -> Rc<RefCell<Page>> {
loop {
let page_ref = self.pager.read_page(page_idx);
match page_ref {
Ok(p) => return p,
Err(_) => {}
if let Ok(p) = page_ref {
return p;
}
}
}
@ -646,7 +645,7 @@ impl BTreeCursor {
let usable_space = (db_header.page_size - db_header.unused_space as u16) as usize;
assert!(top + amount <= usable_space);
return top as u16;
top as u16
}
fn defragment_page(&self, page: &PageContent, db_header: Ref<DatabaseHeader>) {
@ -795,8 +794,8 @@ impl BTreeCursor {
// return SQLITE_CORRUPT_PAGE(pPage);
// }
// don't count header and cell pointers?
nfree = nfree - first_cell as usize;
return nfree as u16;
nfree -= first_cell as usize;
nfree as u16
}
}
@ -809,7 +808,7 @@ fn find_free_cell(page_ref: &PageContent, db_header: Ref<DatabaseHeader>, amount
let buf = buf_ref.as_slice();
let usable_space = (db_header.page_size - db_header.unused_space as u16) as usize;
let maxpc = (usable_space - amount);
let maxpc = usable_space - amount;
let mut found = false;
while pc <= maxpc {
let next = u16::from_be_bytes(buf[pc..pc + 2].try_into().unwrap());

View file

@ -30,8 +30,8 @@ impl DatabaseStorage for FileStorage {
};
let size = r.buf().len();
assert!(page_idx > 0);
if size < 512 || size > 65536 || size & (size - 1) != 0 {
return Err(LimboError::NotADB.into());
if !(512..=65536).contains(&size) || size & (size - 1) != 0 {
return Err(LimboError::NotADB);
}
let pos = (page_idx - 1) * size;
self.file.pread(pos, c)?;

View file

@ -115,7 +115,7 @@ struct DumbLruPageCache {
impl DumbLruPageCache {
pub fn new(capacity: usize) -> Self {
Self {
capacity: capacity,
capacity,
map: RefCell::new(HashMap::new()),
head: RefCell::new(None),
tail: RefCell::new(None),
@ -125,7 +125,7 @@ impl DumbLruPageCache {
pub fn insert(&mut self, key: usize, value: Rc<RefCell<Page>>) {
self.delete(key);
let mut entry = Box::new(PageCacheEntry {
key: key,
key,
next: None,
prev: None,
page: value,
@ -156,22 +156,17 @@ impl DumbLruPageCache {
fn get_ptr(&mut self, key: usize) -> Option<NonNull<PageCacheEntry>> {
let m = self.map.borrow_mut();
let ptr = m.get(&key);
match ptr {
Some(v) => Some(*v),
None => None,
}
ptr.copied()
}
pub fn get(&mut self, key: &usize) -> Option<Rc<RefCell<Page>>> {
let ptr = self.get_ptr(*key);
if ptr.is_none() {
return None;
}
ptr?;
let ptr = unsafe { ptr.unwrap().as_mut() };
let page = ptr.page.clone();
self.detach(ptr);
self.touch(ptr);
return Some(page);
Some(page)
}
pub fn resize(&mut self, capacity: usize) {
@ -385,7 +380,7 @@ impl Pager {
*/
pub fn allocate_page(&self) -> Result<Rc<RefCell<Page>>> {
let header = &self.db_header;
let mut header = RefCell::borrow_mut(&header);
let mut header = RefCell::borrow_mut(header);
header.database_size += 1;
{
// update database size

View file

@ -827,7 +827,7 @@ pub fn write_varint(buf: &mut [u8], value: u64) -> usize {
for i in 0..n {
buf[i] = encoded[n - 1 - i];
}
return n;
n
}
pub fn begin_read_wal_header(io: &Box<dyn File>) -> Result<Rc<RefCell<WalHeader>>> {

View file

@ -195,9 +195,8 @@ pub fn translate_expr(
for arg in args {
let reg = program.alloc_register();
let _ = translate_expr(program, select, arg, reg, cursor_hint)?;
match arg {
ast::Expr::Literal(_) => program.mark_last_insn_constant(),
_ => {}
if let ast::Expr::Literal(_) = arg {
program.mark_last_insn_constant()
}
}
program.emit_insn(Insn::Function {
@ -269,7 +268,7 @@ pub fn translate_expr(
}
}
program.emit_insn(Insn::Function {
start_reg: start_reg,
start_reg,
dest: target_register,
func: ScalarFunc::Date,
});
@ -293,7 +292,7 @@ pub fn translate_expr(
}
}
program.emit_insn(Insn::Function {
start_reg: start_reg,
start_reg,
dest: target_register,
func: ScalarFunc::Time,
});
@ -334,7 +333,7 @@ pub fn translate_expr(
}
ScalarFunc::Min => {
let args = if let Some(args) = args {
if args.len() < 1 {
if args.is_empty() {
crate::bail_parse_error!(
"min function with less than one argument"
);
@ -346,9 +345,8 @@ pub fn translate_expr(
for arg in args {
let reg = program.alloc_register();
let _ = translate_expr(program, select, arg, reg, cursor_hint)?;
match arg {
ast::Expr::Literal(_) => program.mark_last_insn_constant(),
_ => {}
if let ast::Expr::Literal(_) = arg {
program.mark_last_insn_constant()
}
}
@ -361,7 +359,7 @@ pub fn translate_expr(
}
ScalarFunc::Max => {
let args = if let Some(args) = args {
if args.len() < 1 {
if args.is_empty() {
crate::bail_parse_error!(
"max function with less than one argument"
);
@ -373,9 +371,8 @@ pub fn translate_expr(
for arg in args {
let reg = program.alloc_register();
let _ = translate_expr(program, select, arg, reg, cursor_hint)?;
match arg {
ast::Expr::Literal(_) => program.mark_last_insn_constant(),
_ => {}
if let ast::Expr::Literal(_) = arg {
program.mark_last_insn_constant()
}
}

View file

@ -172,9 +172,7 @@ pub fn translate_insert(
record_reg: record_register,
flag: 0,
});
program.emit_insn(Insn::InsertAwait {
cursor_id: cursor_id,
});
program.emit_insn(Insn::InsertAwait { cursor_id });
}
program.emit_insn(Insn::Goto {

View file

@ -50,10 +50,8 @@ impl SrcTable<'_> {
{
if *join_type == JoinType::LEFT | JoinType::OUTER {
true
} else if *join_type == JoinType::RIGHT | JoinType::OUTER {
true
} else {
false
*join_type == JoinType::RIGHT | JoinType::OUTER
}
} else {
false

View file

@ -374,14 +374,12 @@ fn translate_condition_expr(
let lhs_reg = program.alloc_register();
let rhs_reg = program.alloc_register();
let _ = translate_expr(program, Some(select), lhs, lhs_reg, cursor_hint);
match lhs.as_ref() {
ast::Expr::Literal(_) => program.mark_last_insn_constant(),
_ => {}
if let ast::Expr::Literal(_) = lhs.as_ref() {
program.mark_last_insn_constant()
}
let _ = translate_expr(program, Some(select), rhs, rhs_reg, cursor_hint);
match rhs.as_ref() {
ast::Expr::Literal(_) => program.mark_last_insn_constant(),
_ => {}
if let ast::Expr::Literal(_) = rhs.as_ref() {
program.mark_last_insn_constant()
}
match op {
ast::Operator::Greater => {
@ -757,26 +755,24 @@ fn translate_condition_expr(
condition_metadata.jump_target_when_false,
);
}
} else if condition_metadata.jump_if_condition_is_true {
program.emit_insn_with_label_dependency(
Insn::IfNot {
reg: cur_reg,
target_pc: condition_metadata.jump_target_when_true,
null_reg: cur_reg,
},
condition_metadata.jump_target_when_true,
);
} else {
if condition_metadata.jump_if_condition_is_true {
program.emit_insn_with_label_dependency(
Insn::IfNot {
reg: cur_reg,
target_pc: condition_metadata.jump_target_when_true,
null_reg: cur_reg,
},
condition_metadata.jump_target_when_true,
);
} else {
program.emit_insn_with_label_dependency(
Insn::If {
reg: cur_reg,
target_pc: condition_metadata.jump_target_when_false,
null_reg: cur_reg,
},
condition_metadata.jump_target_when_false,
);
}
program.emit_insn_with_label_dependency(
Insn::If {
reg: cur_reg,
target_pc: condition_metadata.jump_target_when_false,
null_reg: cur_reg,
},
condition_metadata.jump_target_when_false,
);
}
}
_ => todo!("op {:?} not implemented", expr),

View file

@ -352,7 +352,7 @@ impl OwnedRecord {
}
assert!(header_size <= 126);
header_bytes_buf.extend(std::iter::repeat(0).take(9));
let n = write_varint(&mut header_bytes_buf.as_mut_slice(), header_size as u64);
let n = write_varint(header_bytes_buf.as_mut_slice(), header_size as u64);
header_bytes_buf.truncate(n);
buf.splice(initial_i..initial_i, header_bytes_buf.iter().cloned());
}

View file

@ -111,7 +111,7 @@ fn get_date_time_from_time_value_integer(value: i64) -> Option<NaiveDateTime> {
}
fn get_date_time_from_time_value_float(value: f64) -> Option<NaiveDateTime> {
if value.is_infinite() || value.is_nan() || value < 0.0 || value >= 5373484.5 {
if value.is_infinite() || value.is_nan() || !(0.0..5373484.5).contains(&value) {
return None;
}
match julian_day_converter::julian_day_to_datetime(value) {

View file

@ -686,6 +686,6 @@ pub fn insn_to_str(
p3,
p4.to_string(),
p5,
manual_comment.map_or(format!("{}", comment), |mc| format!("{}; {}", comment, mc))
manual_comment.map_or(comment.to_string(), |mc| format!("{}; {}", comment, mc))
)
}

View file

@ -439,7 +439,7 @@ impl Program {
(OwnedValue::Float(lhs), OwnedValue::Float(rhs)) => {
state.registers[dest] = OwnedValue::Float(lhs + rhs);
}
((OwnedValue::Null, _) | (_, OwnedValue::Null)) => {
(OwnedValue::Null, _) | (_, OwnedValue::Null) => {
state.registers[dest] = OwnedValue::Null;
}
_ => {