From 567ceccd9f119da5248313b40a67282d6ca3b61c Mon Sep 17 00:00:00 2001 From: forehalo Date: Wed, 20 Dec 2023 17:32:42 +0800 Subject: [PATCH] chore: apply clippy suggestions --- .github/workflows/y-octo.yml | 7 +++---- y-octo-node/src/array.rs | 1 + y-octo-node/src/map.rs | 1 + y-octo-node/src/text.rs | 2 ++ y-octo/src/doc/codec/id.rs | 25 ------------------------- y-octo/src/doc/common/range.rs | 21 +++++++++------------ y-octo/src/doc/hasher.rs | 7 +------ y-octo/src/doc/history.rs | 4 ++-- y-octo/src/doc/store.rs | 2 +- y-octo/src/doc/types/array.rs | 4 ++-- y-octo/src/doc/types/map.rs | 8 ++++---- 11 files changed, 26 insertions(+), 56 deletions(-) diff --git a/.github/workflows/y-octo.yml b/.github/workflows/y-octo.yml index 88fb809..b3d91f6 100644 --- a/.github/workflows/y-octo.yml +++ b/.github/workflows/y-octo.yml @@ -34,7 +34,7 @@ jobs: - name: Setup Rust uses: ./.github/actions/setup-rust with: - toolchain: nightly-2023-08-19 + toolchain: nightly components: clippy, rustfmt - name: Install Node.js @@ -53,8 +53,7 @@ jobs: - name: Build & Check run: | - cargo vendor > .cargo/config.toml - cargo clippy --all-features --message-format=json -- -D warnings | clippy-sarif | tee rust-clippy-results.sarif | sarif-fmt + cargo +nightly clippy --all-features --message-format=json -- -D warnings | clippy-sarif | tee rust-clippy-results.sarif | sarif-fmt RUSTDOCFLAGS="-D rustdoc::broken-intra-doc-links" cargo doc --workspace --all-features --no-deps env: CARGO_TERM_COLOR: always @@ -63,7 +62,7 @@ jobs: run: | yarn prettier --check . yarn taplo fmt --check . - cargo +nightly-2023-08-19 fmt --all --check + cargo +nightly fmt --all --check - name: Upload analysis results to GitHub uses: github/codeql-action/upload-sarif@v2 diff --git a/y-octo-node/src/array.rs b/y-octo-node/src/array.rs index 367f9fc..2a10396 100644 --- a/y-octo-node/src/array.rs +++ b/y-octo-node/src/array.rs @@ -10,6 +10,7 @@ pub struct YArray { #[napi] impl YArray { + #[allow(clippy::new_without_default)] #[napi(constructor)] pub fn new() -> Self { unimplemented!() diff --git a/y-octo-node/src/map.rs b/y-octo-node/src/map.rs index 537f18d..4d1fe08 100644 --- a/y-octo-node/src/map.rs +++ b/y-octo-node/src/map.rs @@ -10,6 +10,7 @@ pub struct YMap { #[napi] impl YMap { + #[allow(clippy::new_without_default)] #[napi(constructor)] pub fn new() -> Self { unimplemented!() diff --git a/y-octo-node/src/text.rs b/y-octo-node/src/text.rs index 1aa6c65..cd4fadb 100644 --- a/y-octo-node/src/text.rs +++ b/y-octo-node/src/text.rs @@ -9,6 +9,7 @@ pub struct YText { #[napi] impl YText { + #[allow(clippy::new_without_default)] #[napi(constructor)] pub fn new() -> Self { unimplemented!() @@ -43,6 +44,7 @@ impl YText { self.text.len() as i64 } + #[allow(clippy::inherent_to_string)] #[napi] pub fn to_string(&self) -> String { self.text.to_string() diff --git a/y-octo/src/doc/codec/id.rs b/y-octo/src/doc/codec/id.rs index 0c0d41e..8941da1 100644 --- a/y-octo/src/doc/codec/id.rs +++ b/y-octo/src/doc/codec/id.rs @@ -1,5 +1,4 @@ use std::{ - cmp::Ordering, fmt::Display, hash::Hash, ops::{Add, Sub}, @@ -44,22 +43,6 @@ impl Add for Id { } } -#[allow(clippy::incorrect_partial_ord_impl_on_ord_type)] -impl PartialOrd for Id { - fn partial_cmp(&self, other: &Self) -> Option { - match self.client.cmp(&other.client) { - Ordering::Equal => Some(self.clock.cmp(&other.clock)), - _ => None, - } - } -} - -impl Ord for Id { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.clock.cmp(&other.clock) - } -} - impl Display for Id { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "({}, {})", self.client, self.clock) @@ -72,16 +55,8 @@ mod tests { #[test] fn basic_id_operation() { - let id_with_same_client_1 = Id::new(1, 1); - let id_with_same_client_2 = Id::new(1, 2); - assert!(id_with_same_client_1 < id_with_same_client_2); - let id_with_different_client_1 = Id::new(1, 1); let id_with_different_client_2 = Id::new(2, 1); - assert_eq!( - id_with_different_client_1.partial_cmp(&id_with_different_client_2), - None - ); assert_ne!(id_with_different_client_1, id_with_different_client_2); assert_eq!(Id::new(1, 1), Id::new(1, 1)); diff --git a/y-octo/src/doc/common/range.rs b/y-octo/src/doc/common/range.rs index 1059ed1..ff8984c 100644 --- a/y-octo/src/doc/common/range.rs +++ b/y-octo/src/doc/common/range.rs @@ -383,18 +383,15 @@ mod tests { #[test] fn test_range_covered() { - assert_eq!(OrderRange::check_range_covered(&[0..1], &[2..3]), false); - assert_eq!(OrderRange::check_range_covered(&[0..1], &[0..3]), true); - assert_eq!(OrderRange::check_range_covered(&[0..1], &[1..3]), false); - assert_eq!(OrderRange::check_range_covered(&[0..1], &[0..3]), true); - assert_eq!(OrderRange::check_range_covered(&[1..2], &[0..3]), true); - assert_eq!(OrderRange::check_range_covered(&[1..2, 2..3], &[0..3]), true); - assert_eq!(OrderRange::check_range_covered(&[1..2, 2..3, 3..4], &[0..3]), false); - assert_eq!(OrderRange::check_range_covered(&[0..1, 2..3], &[0..2, 2..4]), true); - assert_eq!( - OrderRange::check_range_covered(&[0..1, 2..3, 3..4], &[0..2, 2..4]), - true - ); + assert!(!OrderRange::check_range_covered(&[0..1], &[2..3])); + assert!(OrderRange::check_range_covered(&[0..1], &[0..3])); + assert!(!OrderRange::check_range_covered(&[0..1], &[1..3])); + assert!(OrderRange::check_range_covered(&[0..1], &[0..3])); + assert!(OrderRange::check_range_covered(&[1..2], &[0..3])); + assert!(OrderRange::check_range_covered(&[1..2, 2..3], &[0..3])); + assert!(!OrderRange::check_range_covered(&[1..2, 2..3, 3..4], &[0..3])); + assert!(OrderRange::check_range_covered(&[0..1, 2..3], &[0..2, 2..4])); + assert!(OrderRange::check_range_covered(&[0..1, 2..3, 3..4], &[0..2, 2..4]),); } #[test] diff --git a/y-octo/src/doc/hasher.rs b/y-octo/src/doc/hasher.rs index 2248354..629611d 100644 --- a/y-octo/src/doc/hasher.rs +++ b/y-octo/src/doc/hasher.rs @@ -5,6 +5,7 @@ use std::{ use super::Client; +#[derive(Default)] pub struct ClientHasher(Client); impl Hasher for ClientHasher { @@ -19,12 +20,6 @@ impl Hasher for ClientHasher { } } -impl Default for ClientHasher { - fn default() -> Self { - Self(0) - } -} - #[derive(Default, Clone)] pub struct ClientHasherBuilder; diff --git a/y-octo/src/doc/history.rs b/y-octo/src/doc/history.rs index 0208864..874532c 100644 --- a/y-octo/src/doc/history.rs +++ b/y-octo/src/doc/history.rs @@ -64,7 +64,7 @@ impl StoreHistory { // make items as reference let mut store_items = store_items.iter().collect::>(); - store_items.sort_by(|a, b| a.id.cmp(&b.id)); + store_items.sort_by(|a, b| a.id.clock.cmp(&b.id.clock)); self.parse_items(store_items) } @@ -130,7 +130,7 @@ impl StoreHistory { // make items as reference let mut store_items = store_items.iter().collect::>(); - store_items.sort_by(|a, b| a.id.cmp(&b.id)); + store_items.sort_by(|a, b| a.id.clock.cmp(&b.id.clock)); self.parse_items(store_items) } diff --git a/y-octo/src/doc/store.rs b/y-octo/src/doc/store.rs index e6fa78d..e4b4ce3 100644 --- a/y-octo/src/doc/store.rs +++ b/y-octo/src/doc/store.rs @@ -808,7 +808,7 @@ impl DocStore { clock..clock + n.len() }) .collect::>(); - if ranges.len() > 0 { + if !ranges.is_empty() { delete_set.batch_push(*client, ranges); } } diff --git a/y-octo/src/doc/types/array.rs b/y-octo/src/doc/types/array.rs index da07d17..04b8390 100644 --- a/y-octo/src/doc/types/array.rs +++ b/y-octo/src/doc/types/array.rs @@ -13,7 +13,7 @@ impl Iterator for ArrayIter<'_> { for item in self.0.by_ref() { if let Some(item) = item.get() { if item.countable() { - return Some(Value::try_from(&item.content).unwrap()); + return Some(Value::from(&item.content)); } } } @@ -40,7 +40,7 @@ impl Array { // TODO: rewrite to content.read(&mut [Any]) return match &item.content { Content::Any(any) => return any.get(offset as usize).map(|any| Value::Any(any.clone())), - _ => Value::try_from(&item.content).map_or_else(|_| None, Some), + _ => Some(Value::from(&item.content)), }; } diff --git a/y-octo/src/doc/types/map.rs b/y-octo/src/doc/types/map.rs index 4b5a8fe..60e468c 100644 --- a/y-octo/src/doc/types/map.rs +++ b/y-octo/src/doc/types/map.rs @@ -1,4 +1,4 @@ -use std::collections::hash_map::Iter; +use std::{collections::hash_map::Iter, rc::Rc}; use super::*; use crate::{ @@ -71,10 +71,10 @@ pub(crate) trait MapType: AsInner { let ty = self.as_inner().ty(); if let Some(ty) = ty { - let ty = Arc::new(ty); + let ty = Rc::new(ty); EntriesInnerIterator { - iter: Some(unsafe { &*Arc::as_ptr(&ty) }.map.iter()), + iter: Some(unsafe { &*Rc::as_ptr(&ty) }.map.iter()), _lock: Some(ty), } } else { @@ -99,7 +99,7 @@ pub(crate) trait MapType: AsInner { } pub(crate) struct EntriesInnerIterator<'a> { - _lock: Option>>, + _lock: Option>>, iter: Option>, }