Browse Source

Fix cargo clippy errors.

Jing Yang 4 năm trước cách đây
mục cha
commit
4a5de0b558

+ 2 - 3
kvraft/src/client.rs

@@ -30,9 +30,8 @@ impl Clerk {
 
         let key = key.as_ref();
         loop {
-            match inner.get(key.to_owned(), Default::default()) {
-                Some(val) => return val,
-                None => {}
+            if let Some(val) = inner.get(key.to_owned(), Default::default()) {
+                return val;
             }
         }
     }

+ 4 - 4
kvraft/src/common.rs

@@ -105,10 +105,10 @@ pub trait ValidReply {
 
 impl<T> ValidReply for Result<T, KVError> {
     fn is_reply_valid(&self) -> bool {
-        return match self.as_ref().err() {
-            Some(KVError::TimedOut) | Some(KVError::NotLeader) => false,
-            _ => true,
-        };
+        !matches!(
+            self.as_ref().err(),
+            Some(KVError::TimedOut) | Some(KVError::NotLeader)
+        )
     }
 }
 

+ 2 - 1
kvraft/src/server.rs

@@ -242,6 +242,7 @@ impl KVServer {
             let mut state = self.state.lock();
             let applied = state.applied_op.get(&unique_id.clerk_id);
             if let Some((applied_unique_id, result)) = applied {
+                #[allow(clippy::comparison_chain)]
                 if unique_id < *applied_unique_id {
                     return Err(CommitError::Expired(unique_id));
                 } else if unique_id == *applied_unique_id {
@@ -330,7 +331,7 @@ impl KVServer {
             *guard = Err(CommitError::Duplicate(result))
         }
 
-        return result;
+        result
     }
 
     fn validate_term(term: usize) {

+ 2 - 2
kvraft/src/snapshot_holder.rs

@@ -36,10 +36,10 @@ impl<T: Serialize> SnapshotHolder<T> {
 
         let data = bincode::serialize(state)
             .expect("Serialization should never fail.");
-        return Some(Snapshot {
+        Some(Snapshot {
             data,
             last_included_index: curr,
-        });
+        })
     }
 }
 

+ 1 - 1
kvraft/src/testing_utils/config.rs

@@ -264,7 +264,7 @@ impl Config {
                 over_limits.push_str(&str);
             }
         }
-        if over_limits.len() != 0 {
+        if !over_limits.is_empty() {
             anyhow::bail!("logs were not trimmed to {}:{}", upper, over_limits);
         }
         Ok(())

+ 4 - 4
kvraft/src/testing_utils/generic_test.rs

@@ -58,7 +58,7 @@ fn appending_client(
         } else {
             let value = clerk
                 .get(&key)
-                .expect(&format!("Key {} should exist.", index));
+                .unwrap_or_else(|| panic!("Key {} should exist.", index));
             assert_eq!(value, last);
         }
         eprintln!("client {} done {}.", index, op_count);
@@ -227,11 +227,11 @@ pub fn generic_test(test_params: GenericTestParams) {
 
         // Stop partitions.
         partition_stop.store(true, Ordering::Release);
-        partition_result.map(|result| {
+        if let Some(result) = partition_result {
             result.join().expect("Partition thread should never fail");
             cfg.connect_all();
             sleep_election_timeouts(1);
-        });
+        }
         let partition_stopped = start.elapsed();
 
         // Tell all clients to stop.
@@ -247,7 +247,7 @@ pub fn generic_test(test_params: GenericTestParams) {
             if !last_result.is_empty() {
                 let real_result = clerk
                     .get(index.to_string())
-                    .expect(&format!("Key {} should exist.", index));
+                    .unwrap_or_else(|| panic!("Key {} should exist.", index));
                 assert_eq!(real_result, last_result);
             }
             eprintln!("Client {} committed {} operations", index, op_count);

+ 1 - 2
kvraft/src/testing_utils/rpcs.rs

@@ -24,10 +24,9 @@ pub fn register_kv_server<
         make_rpc_handler(move |args| kv_clone.as_ref().get(args)),
     )?;
 
-    let kv_clone = kv.clone();
     server.register_rpc_handler(
         PUT_APPEND.to_owned(),
-        make_rpc_handler(move |args| kv_clone.as_ref().put_append(args)),
+        make_rpc_handler(move |args| kv.as_ref().put_append(args)),
     )?;
 
     network.add_server(server_name, server);

+ 2 - 2
linearizability/src/model.rs

@@ -13,7 +13,7 @@ pub trait Model:
         history: &[Operation<Self::Input, Self::Output>],
     ) -> Vec<Vec<&Operation<Self::Input, Self::Output>>> {
         let history: Vec<&Operation<Self::Input, Self::Output>> =
-            history.iter().map(|e| e).collect();
+            history.iter().collect();
         return vec![history];
     }
     fn step(&mut self, input: &Self::Input, output: &Self::Output) -> bool;
@@ -56,7 +56,7 @@ impl Model for KvModel {
     ) -> Vec<Vec<&Operation<KvInput, KvOutput>>> {
         let mut by_key =
             HashMap::<String, Vec<&Operation<KvInput, KvOutput>>>::new();
-        for op in history.into_iter() {
+        for op in history {
             by_key.entry(op.call_op.key.clone()).or_default().push(op);
         }
         let mut result = vec![];