From 6b3b545a9ace7683bcf0aa9ae1ccb911df1deae5 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sat, 6 Jul 2024 16:50:02 +0700 Subject: [PATCH] Some lint fixes for newer Rust --- rslib/src/cloze.rs | 5 +---- rslib/src/config/bool.rs | 6 +++++- rslib/src/image_occlusion/imagedata.rs | 8 +++++--- rslib/src/import_export/package/apkg/import/decks.rs | 2 +- rslib/src/notes/mod.rs | 2 +- rslib/src/scheduler/fsrs/retention.rs | 7 +++---- 6 files changed, 16 insertions(+), 14 deletions(-) diff --git a/rslib/src/cloze.rs b/rslib/src/cloze.rs index 8df6f98d3..5aab2e83f 100644 --- a/rslib/src/cloze.rs +++ b/rslib/src/cloze.rs @@ -146,10 +146,7 @@ impl ExtractedCloze<'_> { /// If cloze starts with image-occlusion:, return the text following that. fn image_occlusion(&self) -> Option<&str> { - let Some(first_node) = self.nodes.first() else { - return None; - }; - let TextOrCloze::Text(text) = first_node else { + let TextOrCloze::Text(text) = self.nodes.first()? else { return None; }; text.strip_prefix("image-occlusion:") diff --git a/rslib/src/config/bool.rs b/rslib/src/config/bool.rs index 4f320cd76..b8ae82d54 100644 --- a/rslib/src/config/bool.rs +++ b/rslib/src/config/bool.rs @@ -55,7 +55,11 @@ pub enum BoolKey { /// This is a workaround for old clients that used ints to represent boolean /// values. For new config items, prefer using a bool directly. #[derive(Deserialize, Default)] -struct BoolLike(#[serde(deserialize_with = "deserialize_bool_from_anything")] bool); +struct BoolLike( + #[serde(deserialize_with = "deserialize_bool_from_anything")] + #[allow(dead_code)] + bool, +); impl Collection { pub fn get_config_bool(&self, key: BoolKey) -> bool { diff --git a/rslib/src/image_occlusion/imagedata.rs b/rslib/src/image_occlusion/imagedata.rs index 1e354c213..f55e8f0d8 100644 --- a/rslib/src/image_occlusion/imagedata.rs +++ b/rslib/src/image_occlusion/imagedata.rs @@ -97,10 +97,12 @@ impl Collection { let idxs = nt.get_io_field_indexes()?; cloze_note.occlusions = parse_image_occlusions(fields[idxs.occlusions as usize].as_str()); - cloze_note.header = fields[idxs.header as usize].clone(); - cloze_note.back_extra = fields[idxs.back_extra as usize].clone(); + cloze_note.header.clone_from(&fields[idxs.header as usize]); + cloze_note + .back_extra + .clone_from(&fields[idxs.back_extra as usize]); cloze_note.image_data = "".into(); - cloze_note.tags = note.tags.clone(); + cloze_note.tags.clone_from(¬e.tags); let image_file_name = &fields[idxs.image as usize]; let src = self diff --git a/rslib/src/import_export/package/apkg/import/decks.rs b/rslib/src/import_export/package/apkg/import/decks.rs index 400554095..bba4505a6 100644 --- a/rslib/src/import_export/package/apkg/import/decks.rs +++ b/rslib/src/import_export/package/apkg/import/decks.rs @@ -157,7 +157,7 @@ impl Deck { fn update_normal_with_other(normal: &mut NormalDeck, other: &NormalDeck) { if !other.description.is_empty() { normal.markdown_description = other.markdown_description; - normal.description = other.description.clone(); + normal.description.clone_from(&other.description); } if other.config_id != 1 { normal.config_id = other.config_id; diff --git a/rslib/src/notes/mod.rs b/rslib/src/notes/mod.rs index 81ef08948..b71e31c3d 100644 --- a/rslib/src/notes/mod.rs +++ b/rslib/src/notes/mod.rs @@ -663,7 +663,7 @@ mod test { // match the python implementation for now assert_eq!(anki_base91(0), ""); assert_eq!(anki_base91(1), "b"); - assert_eq!(anki_base91(u64::max_value()), "Rj&Z5m[>Zp"); + assert_eq!(anki_base91(u64::MAX), "Rj&Z5m[>Zp"); assert_eq!(anki_base91(1234567890), "saAKk"); } diff --git a/rslib/src/scheduler/fsrs/retention.rs b/rslib/src/scheduler/fsrs/retention.rs index 54e5afc9d..d0c271afa 100644 --- a/rslib/src/scheduler/fsrs/retention.rs +++ b/rslib/src/scheduler/fsrs/retention.rs @@ -71,8 +71,7 @@ impl Collection { .is_ok() }, )? - .max(0.75) - .min(0.95) as f32) + .clamp(0.75, 0.95) as f32) } pub fn get_optimal_retention_parameters( @@ -226,10 +225,10 @@ impl Collection { } } -fn median_secs(group: &Vec) -> f64 { +fn median_secs(group: &[u32]) -> f64 { let length = group.len(); if length > 0 { - let mut group_vec = group.clone(); + let mut group_vec = group.to_vec(); group_vec.sort_unstable(); let median_millis = if length % 2 == 0 { let mid = length / 2;