From 131c9f44b5e5ba6f07224862adc7fdabf82d5fbf Mon Sep 17 00:00:00 2001 From: dvermd <888-dvermd@users.noreply.git.duniter.org> Date: Sat, 21 Dec 2019 23:31:08 +0100 Subject: [PATCH] [fix] wot:operations: #134 remove unwrap and deny its use --- lib/dubp/wot/lib.rs | 2 ++ lib/dubp/wot/operations/centrality.rs | 18 ++++++------------ lib/dubp/wot/operations/distance.rs | 7 +++++-- lib/dubp/wot/operations/path.rs | 7 +++++-- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/dubp/wot/lib.rs b/lib/dubp/wot/lib.rs index d092b6a3..927c7a3f 100644 --- a/lib/dubp/wot/lib.rs +++ b/lib/dubp/wot/lib.rs @@ -27,6 +27,8 @@ //! [js-tests]: https://github.com/duniter/wot/blob/master/wotcpp/webOfTrust.cpp #![deny( + clippy::option_unwrap_used, + clippy::result_unwrap_used, missing_docs, missing_debug_implementations, missing_copy_implementations, diff --git a/lib/dubp/wot/operations/centrality.rs b/lib/dubp/wot/operations/centrality.rs index f1225e11..029d8927 100644 --- a/lib/dubp/wot/operations/centrality.rs +++ b/lib/dubp/wot/operations/centrality.rs @@ -50,8 +50,7 @@ impl<T: WebOfTrust> CentralitiesCalculator<T> for UlrikBrandesCentralityCalculat sigma[s.0] = 1.0; d[s.0] = 0; q.push_back(s); - while !q.is_empty() { - let v = q.pop_front().unwrap(); + while let Some(v) = q.pop_front() { stack.push(v); for w in wot.get_links_source(v).expect("v don't have any source !") { // w found for the first time ? @@ -68,8 +67,7 @@ impl<T: WebOfTrust> CentralitiesCalculator<T> for UlrikBrandesCentralityCalculat } let mut delta = vec![0.0; wot_size]; // stack returns vertices in order of non-increasing distance from s - while !stack.is_empty() { - let w = stack.pop().unwrap(); + while let Some(w) = stack.pop() { if paths.contains_key(&w) { for v in paths.get(&w).expect("Not found w in p !") { if enabled_nodes.contains(&w) { @@ -103,8 +101,7 @@ impl<T: WebOfTrust> CentralitiesCalculator<T> for UlrikBrandesCentralityCalculat sigma[s.0] = 1.0; d[s.0] = 0; q.push_back(s); - while !q.is_empty() { - let v = q.pop_front().unwrap(); + while let Some(v) = q.pop_front() { stack.push(v); for w in wot.get_links_source(v).expect("v don't have any source !") { // w found for the first time ? @@ -121,8 +118,7 @@ impl<T: WebOfTrust> CentralitiesCalculator<T> for UlrikBrandesCentralityCalculat } let mut delta = vec![0.0; wot_size]; // stack returns vertices in order of non-increasing distance from s - while !stack.is_empty() { - let w = stack.pop().unwrap(); + while let Some(w) = stack.pop() { if paths.contains_key(&w) { for v in paths.get(&w).expect("Not found w in p !") { if enabled_nodes.contains(&w) { @@ -156,8 +152,7 @@ impl<T: WebOfTrust> CentralitiesCalculator<T> for UlrikBrandesCentralityCalculat sigma[s.0] = 1.0; d[s.0] = 0; q.push_back(s); - while !q.is_empty() { - let v = q.pop_front().unwrap(); + while let Some(v) = q.pop_front() { stack.push(v); if d[v.0] < step_max as isize { for w in wot.get_links_source(v).expect("v don't have any source !") { @@ -176,8 +171,7 @@ impl<T: WebOfTrust> CentralitiesCalculator<T> for UlrikBrandesCentralityCalculat } let mut delta = vec![0.0; wot_size]; // stack returns vertices in order of non-increasing distance from s - while !stack.is_empty() { - let w = stack.pop().unwrap(); + while let Some(w) = stack.pop() { if paths.contains_key(&w) { for v in paths.get(&w).expect("Not found w in p !") { if enabled_nodes.contains(&w) { diff --git a/lib/dubp/wot/operations/distance.rs b/lib/dubp/wot/operations/distance.rs index 5c6ccb5f..53d77f43 100644 --- a/lib/dubp/wot/operations/distance.rs +++ b/lib/dubp/wot/operations/distance.rs @@ -97,7 +97,7 @@ impl<T: WebOfTrust + Sync> DistanceCalculator<T> for RustyDistanceCalculator { .par_iter() .map(|&id| { wot.get_links_source(id) - .unwrap() + .expect("get_links_source must return a value") .iter() .filter(|source| !area.contains(source)) .cloned() @@ -116,7 +116,10 @@ impl<T: WebOfTrust + Sync> DistanceCalculator<T> for RustyDistanceCalculator { let mut success = area.iter().filter(|n| sentries.contains(n)).count() as u32; let success_at_border = border.iter().filter(|n| sentries.contains(n)).count() as u32; let mut sentries = sentries.len() as u32; - if wot.is_sentry(node, sentry_requirement as usize).unwrap() { + if wot + .is_sentry(node, sentry_requirement as usize) + .expect("is_sentry must return a value") + { sentries -= 1; success -= 1; } diff --git a/lib/dubp/wot/operations/path.rs b/lib/dubp/wot/operations/path.rs index 4304e182..5ca9b702 100644 --- a/lib/dubp/wot/operations/path.rs +++ b/lib/dubp/wot/operations/path.rs @@ -52,7 +52,10 @@ impl<T: WebOfTrust> PathFinder<T> for RustyPathFinder { let mut next_border = HashSet::new(); for node in border { - for source in &wot.get_links_source(node).unwrap() { + for source in &wot + .get_links_source(node) + .expect("links source must not be None") + { match graph[source.0].0 { path_distance if path_distance > distance => { // shorter path, we replace @@ -80,7 +83,7 @@ impl<T: WebOfTrust> PathFinder<T> for RustyPathFinder { let mut new_paths = vec![]; for path in &paths { - let node = path.last().unwrap(); + let node = path.last().expect("path should not be empty"); if node == &to { // If path is complete, we keep it. -- GitLab