From 5893b52fd16e5950536698c6a667f37675d081cd Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sat, 30 Apr 2022 22:55:58 -0700 Subject: [PATCH] cleanup: use `while let Some(...)` instead of checking before popping --- lib/src/dag_walk.rs | 3 +-- lib/src/index.rs | 9 +++------ lib/src/index_store.rs | 6 ++---- lib/src/working_copy.rs | 3 +-- src/commands.rs | 3 +-- 5 files changed, 8 insertions(+), 16 deletions(-) diff --git a/lib/src/dag_walk.rs b/lib/src/dag_walk.rs index 405022dd8..7de2159fe 100644 --- a/lib/src/dag_walk.rs +++ b/lib/src/dag_walk.rs @@ -87,8 +87,7 @@ where for start_node in start_nodes { let mut stack = vec![(start_node, false)]; - while !stack.is_empty() { - let (node, neighbors_visited) = stack.pop().unwrap(); + while let Some((node, neighbors_visited)) = stack.pop() { let id = id_fn(&node); if emitted.contains(&id) { continue; diff --git a/lib/src/index.rs b/lib/src/index.rs index bbf396658..c97e37e5a 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -763,8 +763,7 @@ impl<'a> CompositeIndex<'a> { let ancestor_generation = self.entry_by_pos(ancestor_pos).generation_number(); let mut work = vec![descendant_pos]; let mut visited = HashSet::new(); - while !work.is_empty() { - let descendant_pos = work.pop().unwrap(); + while let Some(descendant_pos) = work.pop() { let descendant_entry = self.entry_by_pos(descendant_pos); if descendant_pos == ancestor_pos { return true; @@ -889,8 +888,7 @@ impl<'a> CompositeIndex<'a> { // set of candidates. Stop walking when we have gone past the minimum // candidate generation. let mut visited = HashSet::new(); - while !work.is_empty() { - let item = work.pop().unwrap().0; + while let Some(IndexEntryByGeneration(item)) = work.pop() { if !visited.insert(item.pos) { continue; } @@ -1017,8 +1015,7 @@ impl<'a> Iterator for RevWalk<'a> { type Item = IndexEntry<'a>; fn next(&mut self) -> Option { - while !self.wanted_boundary_set.is_empty() { - let item = self.items.pop().unwrap(); + while let Some(item) = self.items.pop() { if item.wanted { self.wanted_boundary_set.remove(&item.entry.0.pos); if self.unwanted_boundary_set.contains(&item.entry.0.pos) { diff --git a/lib/src/index_store.rs b/lib/src/index_store.rs index 00965696e..2d07e2283 100644 --- a/lib/src/index_store.rs +++ b/lib/src/index_store.rs @@ -187,8 +187,7 @@ fn topo_order_earlier_first( let mut visited = HashSet::new(); let mut in_parent_file = HashSet::new(); let parent_file_source = parent_file.as_ref().map(|file| file.as_ref()); - while !work.is_empty() { - let commit = work.pop().unwrap(); + while let Some(commit) = work.pop() { if parent_file_source.map_or(false, |index| index.has_id(commit.id())) { in_parent_file.insert(commit.id().clone()); continue; @@ -213,8 +212,7 @@ fn topo_order_earlier_first( let mut result = vec![]; let mut visited = in_parent_file; - while !commits.is_empty() { - let commit = commits.pop().unwrap(); + while let Some(commit) = commits.pop() { let mut waiting_for_earlier_commit = false; for earlier in commit.parents().iter().chain(commit.predecessors().iter()) { if !visited.contains(earlier.id()) { diff --git a/lib/src/working_copy.rs b/lib/src/working_copy.rs index 6de403974..e367a4a5e 100644 --- a/lib/src/working_copy.rs +++ b/lib/src/working_copy.rs @@ -335,8 +335,7 @@ impl TreeState { )]; let mut tree_builder = self.store.tree_builder(self.tree_id.clone()); let mut deleted_files: HashSet<_> = self.file_states.keys().cloned().collect(); - while !work.is_empty() { - let (dir, disk_dir, git_ignore) = work.pop().unwrap(); + while let Some((dir, disk_dir, git_ignore)) = work.pop() { if sparse_matcher.visit(&dir).is_nothing() { continue; } diff --git a/src/commands.rs b/src/commands.rs index 9188df48d..dfc2188d5 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -864,8 +864,7 @@ fn find_all_operations( let mut visited = HashSet::new(); let mut work: VecDeque<_> = op_heads_store.get_op_heads().into_iter().collect(); let mut operations = vec![]; - while !work.is_empty() { - let op_id = work.pop_front().unwrap(); + while let Some(op_id) = work.pop_front() { if visited.insert(op_id.clone()) { let store_operation = op_store.read_operation(&op_id).unwrap(); work.extend(store_operation.parents.iter().cloned());