diff --git a/rust/hg-core/src/ancestors.rs b/rust/hg-core/src/ancestors.rs --- a/rust/hg-core/src/ancestors.rs +++ b/rust/hg-core/src/ancestors.rs @@ -175,7 +175,7 @@ impl MissingAncestors { /// /// This is useful in unit tests, but also setdiscovery.py does /// read the bases attribute of a ancestor.missingancestors instance. - pub fn get_bases<'a>(&'a self) -> &'a HashSet { + pub fn get_bases(&self) -> &HashSet { &self.bases } @@ -288,7 +288,7 @@ impl MissingAncestors { .collect(); let revs_visit = &mut revs; let mut both_visit: HashSet = - revs_visit.intersection(&bases_visit).cloned().collect(); + revs_visit.intersection(bases_visit).cloned().collect(); if revs_visit.is_empty() { return Ok(Vec::new()); } @@ -503,18 +503,18 @@ mod tests { MissingAncestors::new(SampleGraph, [5, 3, 1, 3].iter().cloned()); let mut as_vec: Vec = missing_ancestors.get_bases().iter().cloned().collect(); - as_vec.sort(); + as_vec.sort_unstable(); assert_eq!(as_vec, [1, 3, 5]); assert_eq!(missing_ancestors.max_base, 5); missing_ancestors.add_bases([3, 7, 8].iter().cloned()); as_vec = missing_ancestors.get_bases().iter().cloned().collect(); - as_vec.sort(); + as_vec.sort_unstable(); assert_eq!(as_vec, [1, 3, 5, 7, 8]); assert_eq!(missing_ancestors.max_base, 8); as_vec = missing_ancestors.bases_heads()?.iter().cloned().collect(); - as_vec.sort(); + as_vec.sort_unstable(); assert_eq!(as_vec, [3, 5, 7, 8]); Ok(()) } @@ -531,7 +531,7 @@ mod tests { .remove_ancestors_from(&mut revset) .unwrap(); let mut as_vec: Vec = revset.into_iter().collect(); - as_vec.sort(); + as_vec.sort_unstable(); assert_eq!(as_vec.as_slice(), expected); } @@ -572,6 +572,7 @@ mod tests { /// the one in test-ancestor.py. An early version of Rust MissingAncestors /// failed this, yet none of the integration tests of the whole suite /// catched it. + #[allow(clippy::unnecessary_cast)] #[test] fn test_remove_ancestors_from_case1() { let graph: VecGraph = vec![ diff --git a/rust/hg-core/src/config/config.rs b/rust/hg-core/src/config/config.rs --- a/rust/hg-core/src/config/config.rs +++ b/rust/hg-core/src/config/config.rs @@ -117,7 +117,7 @@ fn should_ignore(plain: &PlainInfo, sect } let sections_to_delete: &[&[u8]] = &[b"defaults", b"commands", b"command-templates"]; - return sections_to_delete.contains(§ion); + sections_to_delete.contains(§ion) } impl Config { @@ -207,7 +207,7 @@ impl Config { file_paths.sort(); for file_path in &file_paths { if file_path.extension() == Some(std::ffi::OsStr::new("rc")) { - self.add_trusted_file(&file_path)? + self.add_trusted_file(file_path)? } } } @@ -259,7 +259,7 @@ impl Config { // `mercurial/helptext/config.txt` suggests it should be reversed if let Some(installation_prefix) = hg.parent().and_then(Path::parent) { if installation_prefix != root { - add_for_prefix(&installation_prefix)? + add_for_prefix(installation_prefix)? } } add_for_prefix(root)?; @@ -348,7 +348,7 @@ impl Config { expected_type: &'static str, parse: impl Fn(&'config [u8]) -> Option, ) -> Result, ConfigValueParseError> { - match self.get_inner(§ion, &item) { + match self.get_inner(section, item) { Some((layer, v)) => match parse(&v.bytes) { Some(b) => Ok(Some(b)), None => Err(ConfigValueParseError { @@ -463,7 +463,7 @@ impl Config { ) -> Option<(&ConfigLayer, &ConfigValue)> { // Filter out the config items that are hidden by [PLAIN]. // This differs from python hg where we delete them from the config. - let should_ignore = should_ignore(&self.plain, §ion, &item); + let should_ignore = should_ignore(&self.plain, section, item); for layer in self.layers.iter().rev() { if !layer.trusted { continue; @@ -480,8 +480,8 @@ impl Config { { continue; } - if let Some(v) = layer.get(§ion, &item) { - return Some((&layer, v)); + if let Some(v) = layer.get(section, item) { + return Some((layer, v)); } } None @@ -561,7 +561,7 @@ impl Config { fn get_all(&self, section: &[u8], item: &[u8]) -> Vec<&[u8]> { let mut res = vec![]; for layer in self.layers.iter().rev() { - if let Some(v) = layer.get(§ion, &item) { + if let Some(v) = layer.get(section, item) { res.push(v.bytes.as_ref()); } } @@ -592,11 +592,11 @@ impl Config { add(b"git", b"git", b"1"); add(b"git", b"showfunc", b"1"); add(b"git", b"word-diff", b"1"); - return layer; + layer } // introduce the tweaked defaults as implied by ui.tweakdefaults - pub fn tweakdefaults<'a>(&mut self) -> () { + pub fn tweakdefaults(&mut self) { self.layers.insert(0, Config::tweakdefaults_layer()); } } diff --git a/rust/hg-core/src/config/layer.rs b/rust/hg-core/src/config/layer.rs --- a/rust/hg-core/src/config/layer.rs +++ b/rust/hg-core/src/config/layer.rs @@ -94,11 +94,7 @@ impl ConfigLayer { /// Returns whether this layer comes from `--config` CLI arguments pub(crate) fn is_from_command_line(&self) -> bool { - if let ConfigOrigin::CommandLine = self.origin { - true - } else { - false - } + matches!(self.origin, ConfigOrigin::CommandLine) } /// Add an entry to the config, overwriting the old one if already present. @@ -111,13 +107,13 @@ impl ConfigLayer { ) { self.sections .entry(section) - .or_insert_with(|| HashMap::new()) + .or_insert_with(HashMap::new) .insert(item, ConfigValue { bytes: value, line }); } /// Returns the config value in `
.` if it exists pub fn get(&self, section: &[u8], item: &[u8]) -> Option<&ConfigValue> { - Some(self.sections.get(section)?.get(item)?) + self.sections.get(section)?.get(item) } /// Returns the keys defined in the given section @@ -171,7 +167,7 @@ impl ConfigLayer { while let Some((index, bytes)) = lines_iter.next() { let line = Some(index + 1); - if let Some(m) = INCLUDE_RE.captures(&bytes) { + if let Some(m) = INCLUDE_RE.captures(bytes) { let filename_bytes = &m[1]; let filename_bytes = crate::utils::expand_vars(filename_bytes); // `Path::parent` only fails for the root directory, @@ -205,18 +201,18 @@ impl ConfigLayer { } } } - } else if let Some(_) = EMPTY_RE.captures(&bytes) { - } else if let Some(m) = SECTION_RE.captures(&bytes) { + } else if EMPTY_RE.captures(bytes).is_some() { + } else if let Some(m) = SECTION_RE.captures(bytes) { section = m[1].to_vec(); - } else if let Some(m) = ITEM_RE.captures(&bytes) { + } else if let Some(m) = ITEM_RE.captures(bytes) { let item = m[1].to_vec(); let mut value = m[2].to_vec(); loop { match lines_iter.peek() { None => break, Some((_, v)) => { - if let Some(_) = COMMENT_RE.captures(&v) { - } else if let Some(_) = CONT_RE.captures(&v) { + if COMMENT_RE.captures(v).is_some() { + } else if CONT_RE.captures(v).is_some() { value.extend(b"\n"); value.extend(&m[1]); } else { @@ -227,7 +223,7 @@ impl ConfigLayer { lines_iter.next(); } current_layer.add(section.clone(), item, value, line); - } else if let Some(m) = UNSET_RE.captures(&bytes) { + } else if let Some(m) = UNSET_RE.captures(bytes) { if let Some(map) = current_layer.sections.get_mut(§ion) { map.remove(&m[1]); } @@ -261,7 +257,7 @@ impl DisplayBytes for ConfigLayer { sections.sort_by(|e0, e1| e0.0.cmp(e1.0)); for (section, items) in sections.into_iter() { - let mut items: Vec<_> = items.into_iter().collect(); + let mut items: Vec<_> = items.iter().collect(); items.sort_by(|e0, e1| e0.0.cmp(e1.0)); for (item, config_entry) in items { diff --git a/rust/hg-core/src/config/values.rs b/rust/hg-core/src/config/values.rs --- a/rust/hg-core/src/config/values.rs +++ b/rust/hg-core/src/config/values.rs @@ -200,11 +200,7 @@ fn parse_list_without_trim_start(input: // https://docs.python.org/3/library/stdtypes.html?#bytes.isspace fn is_space(byte: u8) -> bool { - if let b' ' | b'\t' | b'\n' | b'\r' | b'\x0b' | b'\x0c' = byte { - true - } else { - false - } + matches!(byte, b' ' | b'\t' | b'\n' | b'\r' | b'\x0b' | b'\x0c') } } diff --git a/rust/hg-core/src/copy_tracing.rs b/rust/hg-core/src/copy_tracing.rs --- a/rust/hg-core/src/copy_tracing.rs +++ b/rust/hg-core/src/copy_tracing.rs @@ -59,7 +59,7 @@ impl CopySource { Self { rev, path: winner.path, - overwritten: overwritten, + overwritten, } } @@ -489,7 +489,7 @@ fn chain_changes<'a>( if cs1 == cs2 { cs1.mark_delete(current_rev); } else { - cs1.mark_delete_with_pair(current_rev, &cs2); + cs1.mark_delete_with_pair(current_rev, cs2); } e2.insert(cs1.clone()); } @@ -513,15 +513,14 @@ fn add_one_copy( ) { let dest = path_map.tokenize(path_dest); let source = path_map.tokenize(path_source); - let entry; - if let Some(v) = base_copies.get(&source) { - entry = match &v.path { + let entry = if let Some(v) = base_copies.get(&source) { + match &v.path { Some(path) => Some((*(path)).to_owned()), None => Some(source.to_owned()), } } else { - entry = Some(source.to_owned()); - } + Some(source.to_owned()) + }; // Each new entry is introduced by the children, we // record this information as we will need it to take // the right decision when merging conflicting copy @@ -563,17 +562,15 @@ fn merge_copies_dict( MergePick::Major | MergePick::Any => (src_major, src_minor), MergePick::Minor => (src_minor, src_major), }; - MergeResult::UseNewValue(CopySource::new_from_merge( + MergeResult::NewValue(CopySource::new_from_merge( current_merge, winner, loser, )) } else { match pick { - MergePick::Any | MergePick::Major => { - MergeResult::UseRightValue - } - MergePick::Minor => MergeResult::UseLeftValue, + MergePick::Any | MergePick::Major => MergeResult::RightValue, + MergePick::Minor => MergeResult::LeftValue, } } }) diff --git a/rust/hg-core/src/dagops.rs b/rust/hg-core/src/dagops.rs --- a/rust/hg-core/src/dagops.rs +++ b/rust/hg-core/src/dagops.rs @@ -181,7 +181,7 @@ mod tests { let mut revs: HashSet = revs.iter().cloned().collect(); retain_heads(graph, &mut revs)?; let mut as_vec: Vec = revs.iter().cloned().collect(); - as_vec.sort(); + as_vec.sort_unstable(); Ok(as_vec) } @@ -206,7 +206,7 @@ mod tests { ) -> Result, GraphError> { let heads = heads(graph, revs.iter())?; let mut as_vec: Vec = heads.iter().cloned().collect(); - as_vec.sort(); + as_vec.sort_unstable(); Ok(as_vec) } @@ -231,7 +231,7 @@ mod tests { ) -> Result, GraphError> { let set: HashSet<_> = revs.iter().cloned().collect(); let mut as_vec = roots(graph, &set)?; - as_vec.sort(); + as_vec.sort_unstable(); Ok(as_vec) } diff --git a/rust/hg-core/src/dirstate.rs b/rust/hg-core/src/dirstate.rs --- a/rust/hg-core/src/dirstate.rs +++ b/rust/hg-core/src/dirstate.rs @@ -32,7 +32,7 @@ impl DirstateParents { }; pub fn is_merge(&self) -> bool { - return !(self.p2 == NULL_NODE); + !(self.p2 == NULL_NODE) } } diff --git a/rust/hg-core/src/dirstate/dirs_multiset.rs b/rust/hg-core/src/dirstate/dirs_multiset.rs --- a/rust/hg-core/src/dirstate/dirs_multiset.rs +++ b/rust/hg-core/src/dirstate/dirs_multiset.rs @@ -232,7 +232,7 @@ mod tests { #[test] fn test_delete_path_empty_path() { let mut map = - DirsMultiset::from_manifest(&vec![HgPathBuf::new()]).unwrap(); + DirsMultiset::from_manifest(&[HgPathBuf::new()]).unwrap(); let path = HgPath::new(b""); assert_eq!(Ok(()), map.delete_path(path)); assert_eq!( diff --git a/rust/hg-core/src/dirstate/entry.rs b/rust/hg-core/src/dirstate/entry.rs --- a/rust/hg-core/src/dirstate/entry.rs +++ b/rust/hg-core/src/dirstate/entry.rs @@ -180,11 +180,7 @@ impl TruncatedTimestamp { if self.truncated_seconds != other.truncated_seconds { false } else if self.nanoseconds == 0 || other.nanoseconds == 0 { - if self.second_ambiguous { - false - } else { - true - } + !self.second_ambiguous } else { self.nanoseconds == other.nanoseconds } @@ -706,9 +702,9 @@ impl TryFrom for EntryState { } } -impl Into for EntryState { - fn into(self) -> u8 { - match self { +impl From for u8 { + fn from(val: EntryState) -> Self { + match val { EntryState::Normal => b'n', EntryState::Added => b'a', EntryState::Removed => b'r', diff --git a/rust/hg-core/src/dirstate_tree/dirstate_map.rs b/rust/hg-core/src/dirstate_tree/dirstate_map.rs --- a/rust/hg-core/src/dirstate_tree/dirstate_map.rs +++ b/rust/hg-core/src/dirstate_tree/dirstate_map.rs @@ -320,9 +320,7 @@ impl<'tree, 'on_disk> NodeRef<'tree, 'on on_disk: &'on_disk [u8], ) -> Result, DirstateV2ParseError> { match self { - NodeRef::InMemory(_path, node) => { - Ok(node.copy_source.as_ref().map(|s| &**s)) - } + NodeRef::InMemory(_path, node) => Ok(node.copy_source.as_deref()), NodeRef::OnDisk(node) => node.copy_source(on_disk), } } @@ -340,9 +338,9 @@ impl<'tree, 'on_disk> NodeRef<'tree, 'on Cow::Owned(in_memory) => BorrowedPath::InMemory(in_memory), }) } - NodeRef::OnDisk(node) => node - .copy_source(on_disk)? - .map(|source| BorrowedPath::OnDisk(source)), + NodeRef::OnDisk(node) => { + node.copy_source(on_disk)?.map(BorrowedPath::OnDisk) + } }) } @@ -418,10 +416,7 @@ impl Default for NodeData { impl NodeData { fn has_entry(&self) -> bool { - match self { - NodeData::Entry(_) => true, - _ => false, - } + matches!(self, NodeData::Entry(_)) } fn as_entry(&self) -> Option<&DirstateEntry> { @@ -509,7 +504,7 @@ impl<'on_disk> DirstateMap<'on_disk> { Ok(()) }, )?; - let parents = Some(parents.clone()); + let parents = Some(*parents); Ok((map, parents)) } @@ -681,10 +676,8 @@ impl<'on_disk> DirstateMap<'on_disk> { .checked_sub(1) .expect("tracked count to be >= 0"); } - } else { - if wc_tracked { - ancestor.tracked_descendants_count += 1; - } + } else if wc_tracked { + ancestor.tracked_descendants_count += 1; } })?; @@ -734,7 +727,7 @@ impl<'on_disk> DirstateMap<'on_disk> { ancestor.tracked_descendants_count += tracked_count_increment; })?; if let Some(old_entry) = old_entry_opt { - let mut e = old_entry.clone(); + let mut e = old_entry; if e.tracked() { // XXX // This is probably overkill for more case, but we need this to @@ -775,7 +768,7 @@ impl<'on_disk> DirstateMap<'on_disk> { .expect("tracked_descendants_count should be >= 0"); })? .expect("node should exist"); - let mut new_entry = old_entry.clone(); + let mut new_entry = old_entry; new_entry.set_untracked(); node.data = NodeData::Entry(new_entry); Ok(()) @@ -803,7 +796,7 @@ impl<'on_disk> DirstateMap<'on_disk> { } })? .expect("node should exist"); - let mut new_entry = old_entry.clone(); + let mut new_entry = old_entry; new_entry.set_clean(mode, size, mtime); node.data = NodeData::Entry(new_entry); Ok(()) @@ -1364,7 +1357,7 @@ impl OwningDirstateMap { value: &HgPath, ) -> Result, DirstateV2ParseError> { self.with_dmap_mut(|map| { - let node = map.get_or_insert_node(&key, |_ancestor| {})?; + let node = map.get_or_insert_node(key, |_ancestor| {})?; let had_copy_source = node.copy_source.is_none(); let old = node .copy_source @@ -1864,11 +1857,8 @@ mod tests { map.set_untracked(p(b"some/nested/removed"))?; assert_eq!(map.get_map().unreachable_bytes, 0); - match map.get_map().root { - ChildNodes::InMemory(_) => { - panic!("root should not have been mutated") - } - _ => (), + if let ChildNodes::InMemory(_) = map.get_map().root { + panic!("root should not have been mutated") } // We haven't mutated enough (nothing, actually), we should still be in // the append strategy @@ -1879,9 +1869,8 @@ mod tests { let unreachable_bytes = map.get_map().unreachable_bytes; assert!(unreachable_bytes > 0); - match map.get_map().root { - ChildNodes::OnDisk(_) => panic!("root should have been mutated"), - _ => (), + if let ChildNodes::OnDisk(_) = map.get_map().root { + panic!("root should have been mutated") } // This should not mutate the structure either, since `root` has @@ -1889,22 +1878,20 @@ mod tests { map.set_untracked(p(b"merged"))?; assert_eq!(map.get_map().unreachable_bytes, unreachable_bytes); - match map.get_map().get_node(p(b"other/added_with_p2"))?.unwrap() { - NodeRef::InMemory(_, _) => { - panic!("'other/added_with_p2' should not have been mutated") - } - _ => (), + if let NodeRef::InMemory(_, _) = + map.get_map().get_node(p(b"other/added_with_p2"))?.unwrap() + { + panic!("'other/added_with_p2' should not have been mutated") } // But this should, since it's in a different path // than `some/nested/add` map.set_untracked(p(b"other/added_with_p2"))?; assert!(map.get_map().unreachable_bytes > unreachable_bytes); - match map.get_map().get_node(p(b"other/added_with_p2"))?.unwrap() { - NodeRef::OnDisk(_) => { - panic!("'other/added_with_p2' should have been mutated") - } - _ => (), + if let NodeRef::OnDisk(_) = + map.get_map().get_node(p(b"other/added_with_p2"))?.unwrap() + { + panic!("'other/added_with_p2' should have been mutated") } // We have rewritten most of the tree, we should create a new file diff --git a/rust/hg-core/src/dirstate_tree/on_disk.rs b/rust/hg-core/src/dirstate_tree/on_disk.rs --- a/rust/hg-core/src/dirstate_tree/on_disk.rs +++ b/rust/hg-core/src/dirstate_tree/on_disk.rs @@ -246,11 +246,9 @@ impl<'on_disk> Docket<'on_disk> { pub fn parents(&self) -> DirstateParents { use crate::Node; let p1 = Node::try_from(&self.header.parent_1[..USED_NODE_ID_BYTES]) - .unwrap() - .clone(); + .unwrap(); let p2 = Node::try_from(&self.header.parent_2[..USED_NODE_ID_BYTES]) - .unwrap() - .clone(); + .unwrap(); DirstateParents { p1, p2 } } @@ -322,7 +320,7 @@ impl Node { read_hg_path(on_disk, self.full_path) } - pub(super) fn base_name_start<'on_disk>( + pub(super) fn base_name_start( &self, ) -> Result { let start = self.base_name_start.get(); @@ -355,7 +353,7 @@ impl Node { )) } - pub(super) fn has_copy_source<'on_disk>(&self) -> bool { + pub(super) fn has_copy_source(&self) -> bool { self.copy_source.start.get() != 0 } @@ -414,12 +412,12 @@ impl Node { } else { libc::S_IFREG }; - let permisions = if self.flags().contains(Flags::MODE_EXEC_PERM) { + let permissions = if self.flags().contains(Flags::MODE_EXEC_PERM) { 0o755 } else { 0o644 }; - (file_type | permisions).into() + file_type | permissions } fn mtime(&self) -> Result { diff --git a/rust/hg-core/src/dirstate_tree/owning.rs b/rust/hg-core/src/dirstate_tree/owning.rs --- a/rust/hg-core/src/dirstate_tree/owning.rs +++ b/rust/hg-core/src/dirstate_tree/owning.rs @@ -24,7 +24,7 @@ impl OwningDirstateMap { OwningDirstateMapBuilder { on_disk, - map_builder: |bytes| DirstateMap::empty(&bytes), + map_builder: |bytes| DirstateMap::empty(bytes), } .build() } @@ -42,7 +42,7 @@ impl OwningDirstateMap { OwningDirstateMapTryBuilder { on_disk, map_builder: |bytes| { - DirstateMap::new_v1(&bytes).map(|(dmap, p)| { + DirstateMap::new_v1(bytes).map(|(dmap, p)| { parents = p.unwrap_or(DirstateParents::NULL); dmap }) @@ -66,7 +66,7 @@ impl OwningDirstateMap { OwningDirstateMapTryBuilder { on_disk, map_builder: |bytes| { - DirstateMap::new_v2(&bytes, data_size, metadata) + DirstateMap::new_v2(bytes, data_size, metadata) }, } .try_build() diff --git a/rust/hg-core/src/dirstate_tree/status.rs b/rust/hg-core/src/dirstate_tree/status.rs --- a/rust/hg-core/src/dirstate_tree/status.rs +++ b/rust/hg-core/src/dirstate_tree/status.rs @@ -154,7 +154,7 @@ pub fn status<'dirstate>( hg_path, &DirEntry { hg_path: Cow::Borrowed(HgPath::new(b"")), - fs_path: Cow::Borrowed(&root_dir), + fs_path: Cow::Borrowed(root_dir), symlink_metadata: None, file_type: FakeFileType::Directory, }, @@ -245,7 +245,7 @@ impl<'a> HasIgnoredAncestor<'a> { None => false, Some(parent) => { *(parent.cache.get_or_init(|| { - parent.force(ignore_fn) || ignore_fn(&self.path) + parent.force(ignore_fn) || ignore_fn(self.path) })) } } @@ -402,7 +402,7 @@ impl<'a, 'tree, 'on_disk> StatusCommon<' let entry = DirEntry { hg_path: Cow::Borrowed( dirstate_node - .full_path(&self.dmap.on_disk)?, + .full_path(self.dmap.on_disk)?, ), fs_path: Cow::Borrowed(&fs_path), symlink_metadata: Some(fs_metadata), @@ -420,7 +420,8 @@ impl<'a, 'tree, 'on_disk> StatusCommon<' Err(error) => { let hg_path = dirstate_node.full_path(self.dmap.on_disk)?; - Ok(self.io_error(error, hg_path)) + self.io_error(error, hg_path); + Ok(()) } } }) @@ -472,28 +473,25 @@ impl<'a, 'tree, 'on_disk> StatusCommon<' .par_bridge() .map(|pair| { use itertools::EitherOrBoth::*; - let has_dirstate_node_or_is_ignored; - match pair { + let has_dirstate_node_or_is_ignored = match pair { Both(dirstate_node, fs_entry) => { self.traverse_fs_and_dirstate( - &fs_entry, + fs_entry, dirstate_node, has_ignored_ancestor, )?; - has_dirstate_node_or_is_ignored = true + true } Left(dirstate_node) => { self.traverse_dirstate_only(dirstate_node)?; - has_dirstate_node_or_is_ignored = true; + true } - Right(fs_entry) => { - has_dirstate_node_or_is_ignored = self.traverse_fs_only( - has_ignored_ancestor.force(&self.ignore_fn), - directory_hg_path, - fs_entry, - ) - } - } + Right(fs_entry) => self.traverse_fs_only( + has_ignored_ancestor.force(&self.ignore_fn), + directory_hg_path, + fs_entry, + ), + }; Ok(has_dirstate_node_or_is_ignored) }) .try_reduce(|| true, |a, b| Ok(a && b)) @@ -524,7 +522,7 @@ impl<'a, 'tree, 'on_disk> StatusCommon<' .push(hg_path.detach_from_tree()) } let is_ignored = HasIgnoredAncestor::create( - Some(&has_ignored_ancestor), + Some(has_ignored_ancestor), hg_path, ); let is_at_repo_root = false; @@ -544,14 +542,14 @@ impl<'a, 'tree, 'on_disk> StatusCommon<' outdated_dircache, )? } else { - if file_or_symlink && self.matcher.matches(&hg_path) { + if file_or_symlink && self.matcher.matches(hg_path) { if let Some(entry) = dirstate_node.entry()? { if !entry.any_tracked() { // Forward-compat if we start tracking unknown/ignored // files for caching reasons self.mark_unknown_or_ignored( has_ignored_ancestor.force(&self.ignore_fn), - &hg_path, + hg_path, ); } if entry.added() { @@ -620,12 +618,13 @@ impl<'a, 'tree, 'on_disk> StatusCommon<' Ok(meta) => meta, Err(_) => return Ok(()), }; - let directory_mtime = if let Ok(option) = - TruncatedTimestamp::for_reliable_mtime_of(&metadata, status_start) - { - if let Some(directory_mtime) = option { - directory_mtime - } else { + + let directory_mtime = match TruncatedTimestamp::for_reliable_mtime_of( + &metadata, + status_start, + ) { + Ok(Some(directory_mtime)) => directory_mtime, + Ok(None) => { // The directory was modified too recently, // don’t cache its `read_dir` results. // @@ -643,9 +642,10 @@ impl<'a, 'tree, 'on_disk> StatusCommon<' // by the same script. return Ok(()); } - } else { - // OS/libc does not support mtime? - return Ok(()); + Err(_) => { + // OS/libc does not support mtime? + return Ok(()); + } }; // We’ve observed (through `status_start`) that time has // “progressed” since `directory_mtime`, so any further @@ -713,8 +713,9 @@ impl<'a, 'tree, 'on_disk> StatusCommon<' { self.push_outcome(Outcome::Modified, dirstate_node)? } else { - let mtime_looks_clean; - if let Some(dirstate_mtime) = entry.truncated_mtime() { + let mtime_looks_clean = if let Some(dirstate_mtime) = + entry.truncated_mtime() + { let fs_mtime = TruncatedTimestamp::for_mtime_of(&fs_metadata) .expect("OS/libc does not support mtime?"); // There might be a change in the future if for example the @@ -722,10 +723,10 @@ impl<'a, 'tree, 'on_disk> StatusCommon<' // case where the issues the user would face // would be a lot worse and there is nothing we // can really do. - mtime_looks_clean = fs_mtime.likely_equal(dirstate_mtime) + fs_mtime.likely_equal(dirstate_mtime) } else { // No mtime in the dirstate entry - mtime_looks_clean = false + false }; if !mtime_looks_clean { self.push_outcome(Outcome::Unsure, dirstate_node)? @@ -769,7 +770,7 @@ impl<'a, 'tree, 'on_disk> StatusCommon<' if entry.removed() { self.push_outcome(Outcome::Removed, dirstate_node)? } else { - self.push_outcome(Outcome::Deleted, &dirstate_node)? + self.push_outcome(Outcome::Deleted, dirstate_node)? } } } @@ -816,26 +817,24 @@ impl<'a, 'tree, 'on_disk> StatusCommon<' } } is_ignored + } else if file_or_symlink { + if self.matcher.matches(&hg_path) { + self.mark_unknown_or_ignored( + has_ignored_ancestor, + &BorrowedPath::InMemory(&hg_path), + ) + } else { + // We haven’t computed whether this path is ignored. It + // might not be, and a future run of status might have a + // different matcher that matches it. So treat it as not + // ignored. That is, inhibit readdir caching of the parent + // directory. + false + } } else { - if file_or_symlink { - if self.matcher.matches(&hg_path) { - self.mark_unknown_or_ignored( - has_ignored_ancestor, - &BorrowedPath::InMemory(&hg_path), - ) - } else { - // We haven’t computed whether this path is ignored. It - // might not be, and a future run of status might have a - // different matcher that matches it. So treat it as not - // ignored. That is, inhibit readdir caching of the parent - // directory. - false - } - } else { - // This is neither a directory, a plain file, or a symlink. - // Treat it like an ignored file. - true - } + // This is neither a directory, a plain file, or a symlink. + // Treat it like an ignored file. + true } } @@ -845,7 +844,7 @@ impl<'a, 'tree, 'on_disk> StatusCommon<' has_ignored_ancestor: bool, hg_path: &BorrowedPath<'_, 'on_disk>, ) -> bool { - let is_ignored = has_ignored_ancestor || (self.ignore_fn)(&hg_path); + let is_ignored = has_ignored_ancestor || (self.ignore_fn)(hg_path); if is_ignored { if self.options.list_ignored { self.push_outcome_without_copy_source( @@ -853,13 +852,8 @@ impl<'a, 'tree, 'on_disk> StatusCommon<' hg_path, ) } - } else { - if self.options.list_unknown { - self.push_outcome_without_copy_source( - Outcome::Unknown, - hg_path, - ) - } + } else if self.options.list_unknown { + self.push_outcome_without_copy_source(Outcome::Unknown, hg_path) } is_ignored } diff --git a/rust/hg-core/src/discovery.rs b/rust/hg-core/src/discovery.rs --- a/rust/hg-core/src/discovery.rs +++ b/rust/hg-core/src/discovery.rs @@ -194,7 +194,7 @@ impl PartialDiscovery< size: usize, ) -> Vec { if !self.randomize { - sample.sort(); + sample.sort_unstable(); sample.truncate(size); return sample; } @@ -513,14 +513,14 @@ mod tests { ) -> Vec { let mut as_vec: Vec = disco.undecided.as_ref().unwrap().iter().cloned().collect(); - as_vec.sort(); + as_vec.sort_unstable(); as_vec } fn sorted_missing(disco: &PartialDiscovery) -> Vec { let mut as_vec: Vec = disco.missing.iter().cloned().collect(); - as_vec.sort(); + as_vec.sort_unstable(); as_vec } @@ -529,7 +529,7 @@ mod tests { ) -> Result, GraphError> { let mut as_vec: Vec = disco.common_heads()?.iter().cloned().collect(); - as_vec.sort(); + as_vec.sort_unstable(); Ok(as_vec) } @@ -621,7 +621,7 @@ mod tests { disco.undecided = Some((1..=13).collect()); let mut sample_vec = disco.take_quick_sample(vec![], 4)?; - sample_vec.sort(); + sample_vec.sort_unstable(); assert_eq!(sample_vec, vec![10, 11, 12, 13]); Ok(()) } @@ -632,7 +632,7 @@ mod tests { disco.ensure_undecided()?; let mut sample_vec = disco.take_quick_sample(vec![12], 4)?; - sample_vec.sort(); + sample_vec.sort_unstable(); // r12's only parent is r9, whose unique grand-parent through the // diamond shape is r4. This ends there because the distance from r4 // to the root is only 3. @@ -650,11 +650,11 @@ mod tests { assert_eq!(cache.get(&10).cloned(), None); let mut children_4 = cache.get(&4).cloned().unwrap(); - children_4.sort(); + children_4.sort_unstable(); assert_eq!(children_4, vec![5, 6, 7]); let mut children_7 = cache.get(&7).cloned().unwrap(); - children_7.sort(); + children_7.sort_unstable(); assert_eq!(children_7, vec![9, 11]); Ok(()) @@ -684,7 +684,7 @@ mod tests { let (sample_set, size) = disco.bidirectional_sample(7)?; assert_eq!(size, 7); let mut sample: Vec = sample_set.into_iter().collect(); - sample.sort(); + sample.sort_unstable(); // our DAG is a bit too small for the results to be really interesting // at least it shows that // - we went both ways diff --git a/rust/hg-core/src/filepatterns.rs b/rust/hg-core/src/filepatterns.rs --- a/rust/hg-core/src/filepatterns.rs +++ b/rust/hg-core/src/filepatterns.rs @@ -313,7 +313,7 @@ pub fn build_single_regex( PatternSyntax::RootGlob | PatternSyntax::Path | PatternSyntax::RelGlob - | PatternSyntax::RootFiles => normalize_path_bytes(&pattern), + | PatternSyntax::RootFiles => normalize_path_bytes(pattern), PatternSyntax::Include | PatternSyntax::SubInclude => { return Err(PatternError::NonRegexPattern(entry.clone())) } @@ -368,7 +368,7 @@ pub fn parse_pattern_file_contents( let mut warnings: Vec = vec![]; let mut current_syntax = - default_syntax_override.unwrap_or(b"relre:".as_ref()); + default_syntax_override.unwrap_or_else(|| b"relre:".as_ref()); for (line_number, mut line) in lines.split(|c| *c == b'\n').enumerate() { let line_number = line_number + 1; @@ -402,7 +402,7 @@ pub fn parse_pattern_file_contents( continue; } - let mut line_syntax: &[u8] = ¤t_syntax; + let mut line_syntax: &[u8] = current_syntax; for (s, rels) in SYNTAXES.iter() { if let Some(rest) = line.drop_prefix(rels) { @@ -418,7 +418,7 @@ pub fn parse_pattern_file_contents( } inputs.push(IgnorePattern::new( - parse_pattern_syntax(&line_syntax).map_err(|e| match e { + parse_pattern_syntax(line_syntax).map_err(|e| match e { PatternError::UnsupportedSyntax(syntax) => { PatternError::UnsupportedSyntaxInFile( syntax, @@ -428,7 +428,7 @@ pub fn parse_pattern_file_contents( } _ => e, })?, - &line, + line, file_path, )); } @@ -502,7 +502,7 @@ pub fn get_patterns_from_file( } PatternSyntax::SubInclude => { let mut sub_include = SubInclude::new( - &root_dir, + root_dir, &entry.pattern, &entry.source, )?; @@ -564,11 +564,11 @@ impl SubInclude { let prefix = canonical_path(root_dir, root_dir, new_root)?; Ok(Self { - prefix: path_to_hg_path_buf(prefix).and_then(|mut p| { + prefix: path_to_hg_path_buf(prefix).map(|mut p| { if !p.is_empty() { p.push_byte(b'/'); } - Ok(p) + p })?, path: path.to_owned(), root: new_root.to_owned(), diff --git a/rust/hg-core/src/lock.rs b/rust/hg-core/src/lock.rs --- a/rust/hg-core/src/lock.rs +++ b/rust/hg-core/src/lock.rs @@ -107,7 +107,7 @@ fn unlock(hg_vfs: Vfs, lock_filename: &s fn lock_should_be_broken(data: &Option) -> bool { (|| -> Option { let (prefix, pid) = data.as_ref()?.split_once(':')?; - if prefix != &*LOCK_PREFIX { + if prefix != *LOCK_PREFIX { return Some(false); } let process_is_running; @@ -144,6 +144,8 @@ lazy_static::lazy_static! { /// Same as https://github.com/python/cpython/blob/v3.10.0/Modules/socketmodule.c#L5414 const BUFFER_SIZE: usize = 1024; + // This cast is *needed* for platforms with signed chars + #[allow(clippy::unnecessary_cast)] let mut buffer = [0 as libc::c_char; BUFFER_SIZE]; let hostname_bytes = unsafe { let result = libc::gethostname(buffer.as_mut_ptr(), BUFFER_SIZE); diff --git a/rust/hg-core/src/matchers.rs b/rust/hg-core/src/matchers.rs --- a/rust/hg-core/src/matchers.rs +++ b/rust/hg-core/src/matchers.rs @@ -302,11 +302,11 @@ impl<'a> Matcher for IncludeMatcher<'a> } fn matches(&self, filename: &HgPath) -> bool { - (self.match_fn)(filename.as_ref()) + (self.match_fn)(filename) } fn visit_children_set(&self, directory: &HgPath) -> VisitChildrenSet { - let dir = directory.as_ref(); + let dir = directory; if self.prefix && self.roots.contains(dir) { return VisitChildrenSet::Recursive; } @@ -318,11 +318,11 @@ impl<'a> Matcher for IncludeMatcher<'a> return VisitChildrenSet::This; } - if self.parents.contains(directory.as_ref()) { + if self.parents.contains(dir.as_ref()) { let multiset = self.get_all_parents_children(); if let Some(children) = multiset.get(dir) { return VisitChildrenSet::Set( - children.into_iter().map(HgPathBuf::from).collect(), + children.iter().map(HgPathBuf::from).collect(), ); } } @@ -446,7 +446,7 @@ impl Matcher for IntersectionMatcher { VisitChildrenSet::This } (VisitChildrenSet::Set(m1), VisitChildrenSet::Set(m2)) => { - let set: HashSet<_> = m1.intersection(&m2).cloned().collect(); + let set: HashSet<_> = m1.intersection(m2).cloned().collect(); if set.is_empty() { VisitChildrenSet::Empty } else { @@ -699,10 +699,9 @@ fn roots_and_dirs( PatternSyntax::RootGlob | PatternSyntax::Glob => { let mut root = HgPathBuf::new(); for p in pattern.split(|c| *c == b'/') { - if p.iter().any(|c| match *c { - b'[' | b'{' | b'*' | b'?' => true, - _ => false, - }) { + if p.iter() + .any(|c| matches!(*c, b'[' | b'{' | b'*' | b'?')) + { break; } root.push(HgPathBuf::from_bytes(p).as_ref()); @@ -780,10 +779,10 @@ fn roots_dirs_and_parents( /// Returns a function that checks whether a given file (in the general sense) /// should be matched. -fn build_match<'a, 'b>( +fn build_match<'a>( ignore_patterns: Vec, -) -> PatternResult<(Vec, IgnoreFnType<'b>)> { - let mut match_funcs: Vec> = vec![]; +) -> PatternResult<(Vec, IgnoreFnType<'a>)> { + let mut match_funcs: Vec> = vec![]; // For debugging and printing let mut patterns = vec![]; @@ -921,9 +920,8 @@ impl<'a> IncludeMatcher<'a> { dirs, parents, } = roots_dirs_and_parents(&ignore_patterns)?; - let prefix = ignore_patterns.iter().all(|k| match k.syntax { - PatternSyntax::Path | PatternSyntax::RelPath => true, - _ => false, + let prefix = ignore_patterns.iter().all(|k| { + matches!(k.syntax, PatternSyntax::Path | PatternSyntax::RelPath) }); let (patterns, match_fn) = build_match(ignore_patterns)?; diff --git a/rust/hg-core/src/narrow.rs b/rust/hg-core/src/narrow.rs --- a/rust/hg-core/src/narrow.rs +++ b/rust/hg-core/src/narrow.rs @@ -37,9 +37,11 @@ pub fn matcher( } // Treat "narrowspec does not exist" the same as "narrowspec file exists // and is empty". - let store_spec = repo.store_vfs().try_read(FILENAME)?.unwrap_or(vec![]); - let working_copy_spec = - repo.hg_vfs().try_read(DIRSTATE_FILENAME)?.unwrap_or(vec![]); + let store_spec = repo.store_vfs().try_read(FILENAME)?.unwrap_or_default(); + let working_copy_spec = repo + .hg_vfs() + .try_read(DIRSTATE_FILENAME)? + .unwrap_or_default(); if store_spec != working_copy_spec { return Err(HgError::abort( "working copy's narrowspec is stale", diff --git a/rust/hg-core/src/operations/cat.rs b/rust/hg-core/src/operations/cat.rs --- a/rust/hg-core/src/operations/cat.rs +++ b/rust/hg-core/src/operations/cat.rs @@ -70,7 +70,7 @@ fn find_files_in_manifest<'query>( Some(item) => res.push((file, item)), } } - return Ok((res, missing)); + Ok((res, missing)) } /// Output the given revision of files @@ -94,10 +94,8 @@ pub fn cat<'a>( files.sort_unstable(); - let (found, missing) = find_files_in_manifest( - &manifest, - files.into_iter().map(|f| f.as_ref()), - )?; + let (found, missing) = + find_files_in_manifest(&manifest, files.into_iter())?; for (file_path, file_node) in found { found_any = true; diff --git a/rust/hg-core/src/repo.rs b/rust/hg-core/src/repo.rs --- a/rust/hg-core/src/repo.rs +++ b/rust/hg-core/src/repo.rs @@ -68,9 +68,9 @@ impl Repo { return Ok(ancestor.to_path_buf()); } } - return Err(RepoError::NotFound { + Err(RepoError::NotFound { at: current_directory, - }); + }) } /// Find a repository, either at the given path (which must contain a `.hg` @@ -87,13 +87,11 @@ impl Repo { ) -> Result { if let Some(root) = explicit_path { if is_dir(root.join(".hg"))? { - Self::new_at_path(root.to_owned(), config) + Self::new_at_path(root, config) } else if is_file(&root)? { Err(HgError::unsupported("bundle repository").into()) } else { - Err(RepoError::NotFound { - at: root.to_owned(), - }) + Err(RepoError::NotFound { at: root }) } } else { let root = Self::find_repo_root()?; @@ -108,9 +106,8 @@ impl Repo { ) -> Result { let dot_hg = working_directory.join(".hg"); - let mut repo_config_files = Vec::new(); - repo_config_files.push(dot_hg.join("hgrc")); - repo_config_files.push(dot_hg.join("hgrc-not-shared")); + let mut repo_config_files = + vec![dot_hg.join("hgrc"), dot_hg.join("hgrc-not-shared")]; let hg_vfs = Vfs { base: &dot_hg }; let mut reqs = requirements::load_if_exists(hg_vfs)?; @@ -254,7 +251,7 @@ impl Repo { .hg_vfs() .read("dirstate") .io_not_found_as_none()? - .unwrap_or(Vec::new())) + .unwrap_or_default()) } pub fn dirstate_parents(&self) -> Result { @@ -277,8 +274,7 @@ impl Repo { .set(Some(docket.uuid.to_owned())); docket.parents() } else { - crate::dirstate::parsers::parse_dirstate_parents(&dirstate)? - .clone() + *crate::dirstate::parsers::parse_dirstate_parents(&dirstate)? }; self.dirstate_parents.set(parents); Ok(parents) diff --git a/rust/hg-core/src/revlog/changelog.rs b/rust/hg-core/src/revlog/changelog.rs --- a/rust/hg-core/src/revlog/changelog.rs +++ b/rust/hg-core/src/revlog/changelog.rs @@ -165,7 +165,7 @@ impl<'changelog> ChangelogRevisionData<' pub fn files(&self) -> impl Iterator { self.bytes[self.timestamp_end + 1..self.files_end] .split(|b| b == &b'\n') - .map(|path| HgPath::new(path)) + .map(HgPath::new) } /// The change description. diff --git a/rust/hg-core/src/revlog/filelog.rs b/rust/hg-core/src/revlog/filelog.rs --- a/rust/hg-core/src/revlog/filelog.rs +++ b/rust/hg-core/src/revlog/filelog.rs @@ -49,7 +49,7 @@ impl Filelog { file_rev: Revision, ) -> Result { let data: Vec = self.revlog.get_rev_data(file_rev)?.into_owned(); - Ok(FilelogRevisionData(data.into())) + Ok(FilelogRevisionData(data)) } /// The given node ID is that of the file as found in a filelog, not of a @@ -161,7 +161,7 @@ impl FilelogEntry<'_> { // this `FilelogEntry` does not have such metadata: let file_data_len = uncompressed_len; - return file_data_len != other_len; + file_data_len != other_len } pub fn data(&self) -> Result { diff --git a/rust/hg-core/src/revlog/index.rs b/rust/hg-core/src/revlog/index.rs --- a/rust/hg-core/src/revlog/index.rs +++ b/rust/hg-core/src/revlog/index.rs @@ -21,11 +21,11 @@ pub struct IndexHeaderFlags { impl IndexHeaderFlags { /// Corresponds to FLAG_INLINE_DATA in python pub fn is_inline(self) -> bool { - return self.flags & 1 != 0; + self.flags & 1 != 0 } /// Corresponds to FLAG_GENERALDELTA in python pub fn uses_generaldelta(self) -> bool { - return self.flags & 2 != 0; + self.flags & 2 != 0 } } @@ -35,9 +35,9 @@ impl IndexHeader { fn format_flags(&self) -> IndexHeaderFlags { // No "unknown flags" check here, unlike in python. Maybe there should // be. - return IndexHeaderFlags { + IndexHeaderFlags { flags: BigEndian::read_u16(&self.header_bytes[0..2]), - }; + } } /// The only revlog version currently supported by rhg. @@ -45,7 +45,7 @@ impl IndexHeader { /// Corresponds to `_format_version` in Python. fn format_version(&self) -> u16 { - return BigEndian::read_u16(&self.header_bytes[2..4]); + BigEndian::read_u16(&self.header_bytes[2..4]) } const EMPTY_INDEX_HEADER: IndexHeader = IndexHeader { @@ -59,7 +59,7 @@ impl IndexHeader { }; fn parse(index_bytes: &[u8]) -> Result { - if index_bytes.len() == 0 { + if index_bytes.is_empty() { return Ok(IndexHeader::EMPTY_INDEX_HEADER); } if index_bytes.len() < 4 { @@ -67,13 +67,13 @@ impl IndexHeader { "corrupted revlog: can't read the index format header", )); } - return Ok(IndexHeader { + Ok(IndexHeader { header_bytes: { let bytes: [u8; 4] = index_bytes[0..4].try_into().expect("impossible"); bytes }, - }); + }) } } @@ -127,8 +127,7 @@ impl Index { uses_generaldelta, }) } else { - Err(HgError::corrupted("unexpected inline revlog length") - .into()) + Err(HgError::corrupted("unexpected inline revlog length")) } } else { Ok(Self { @@ -466,8 +465,8 @@ mod tests { .with_inline(false) .build(); - assert_eq!(is_inline(&bytes), false); - assert_eq!(uses_generaldelta(&bytes), false); + assert!(!is_inline(&bytes)); + assert!(!uses_generaldelta(&bytes)); } #[test] @@ -478,8 +477,8 @@ mod tests { .with_inline(true) .build(); - assert_eq!(is_inline(&bytes), true); - assert_eq!(uses_generaldelta(&bytes), false); + assert!(is_inline(&bytes)); + assert!(!uses_generaldelta(&bytes)); } #[test] @@ -490,8 +489,8 @@ mod tests { .with_inline(true) .build(); - assert_eq!(is_inline(&bytes), true); - assert_eq!(uses_generaldelta(&bytes), true); + assert!(is_inline(&bytes)); + assert!(uses_generaldelta(&bytes)); } #[test] diff --git a/rust/hg-core/src/revlog/nodemap.rs b/rust/hg-core/src/revlog/nodemap.rs --- a/rust/hg-core/src/revlog/nodemap.rs +++ b/rust/hg-core/src/revlog/nodemap.rs @@ -71,7 +71,7 @@ pub trait NodeMap { /// /// If several Revisions match the given prefix, a [`MultipleResults`] /// error is returned. - fn find_bin<'a>( + fn find_bin( &self, idx: &impl RevlogIndex, prefix: NodePrefix, @@ -88,7 +88,7 @@ pub trait NodeMap { /// /// If several Revisions match the given prefix, a [`MultipleResults`] /// error is returned. - fn unique_prefix_len_bin<'a>( + fn unique_prefix_len_bin( &self, idx: &impl RevlogIndex, node_prefix: NodePrefix, @@ -249,7 +249,7 @@ fn has_prefix_or_none( rev: Revision, ) -> Result, NodeMapError> { idx.node(rev) - .ok_or_else(|| NodeMapError::RevisionNotInIndex(rev)) + .ok_or(NodeMapError::RevisionNotInIndex(rev)) .map(|node| { if prefix.is_prefix_of(node) { Some(rev) @@ -468,7 +468,7 @@ impl NodeTree { if let Element::Rev(old_rev) = deepest.element { let old_node = index .node(old_rev) - .ok_or_else(|| NodeMapError::RevisionNotInIndex(old_rev))?; + .ok_or(NodeMapError::RevisionNotInIndex(old_rev))?; if old_node == node { return Ok(()); // avoid creating lots of useless blocks } @@ -865,7 +865,7 @@ mod tests { hex: &str, ) -> Result<(), NodeMapError> { let node = pad_node(hex); - self.index.insert(rev, node.clone()); + self.index.insert(rev, node); self.nt.insert(&self.index, &node, rev)?; Ok(()) } @@ -887,13 +887,13 @@ mod tests { /// Drain `added` and restart a new one fn commit(self) -> Self { let mut as_vec: Vec = - self.nt.readonly.iter().map(|block| block.clone()).collect(); + self.nt.readonly.iter().copied().collect(); as_vec.extend(self.nt.growable); as_vec.push(self.nt.root); Self { index: self.index, - nt: NodeTree::from(as_vec).into(), + nt: NodeTree::from(as_vec), } } } @@ -967,15 +967,15 @@ mod tests { let idx = &mut nt_idx.index; let node0_hex = hex_pad_right("444444"); - let mut node1_hex = hex_pad_right("444444").clone(); + let mut node1_hex = hex_pad_right("444444"); node1_hex.pop(); node1_hex.push('5'); let node0 = Node::from_hex(&node0_hex).unwrap(); let node1 = Node::from_hex(&node1_hex).unwrap(); - idx.insert(0, node0.clone()); + idx.insert(0, node0); nt.insert(idx, &node0, 0)?; - idx.insert(1, node1.clone()); + idx.insert(1, node1); nt.insert(idx, &node1, 1)?; assert_eq!(nt.find_bin(idx, (&node0).into())?, Some(0)); diff --git a/rust/hg-core/src/revlog/path_encode.rs b/rust/hg-core/src/revlog/path_encode.rs --- a/rust/hg-core/src/revlog/path_encode.rs +++ b/rust/hg-core/src/revlog/path_encode.rs @@ -2,6 +2,7 @@ use sha1::{Digest, Sha1}; #[derive(PartialEq, Debug)] #[allow(non_camel_case_types)] +#[allow(clippy::upper_case_acronyms)] enum path_state { START, /* first byte of a path component */ A, /* "AUX" */ @@ -27,6 +28,7 @@ enum path_state { /* state machine for dir-encoding */ #[allow(non_camel_case_types)] +#[allow(clippy::upper_case_acronyms)] enum dir_state { DDOT, DH, @@ -61,7 +63,7 @@ fn rewrap_option<'a, 'b: 'a>( } } -fn hexencode<'a>(mut dest: Option<&'a mut [u8]>, destlen: &mut usize, c: u8) { +fn hexencode(mut dest: Option<&mut [u8]>, destlen: &mut usize, c: u8) { let hexdigit = b"0123456789abcdef"; charcopy( rewrap_option(&mut dest), @@ -534,10 +536,7 @@ fn hash_mangle(src: &[u8], sha: &[u8]) - let last_slash = src.iter().rposition(|b| *b == b'/'); let last_dot: Option = { let s = last_slash.unwrap_or(0); - src[s..] - .iter() - .rposition(|b| *b == b'.') - .and_then(|i| Some(i + s)) + src[s..].iter().rposition(|b| *b == b'.').map(|i| i + s) }; let mut dest = vec![0; MAXSTOREPATHLEN]; @@ -545,8 +544,8 @@ fn hash_mangle(src: &[u8], sha: &[u8]) - { let mut first = true; - for slice in src[..last_slash.unwrap_or_else(|| src.len())] - .split(|b| *b == b'/') + for slice in + src[..last_slash.unwrap_or(src.len())].split(|b| *b == b'/') { let slice = &slice[..std::cmp::min(slice.len(), dirprefixlen)]; if destlen + (slice.len() + if first { 0 } else { 1 }) @@ -641,6 +640,6 @@ pub fn path_encode(path: &[u8]) -> Vec &NULL_NODE, }; - &hash(data, h1.as_bytes(), h2.as_bytes()) == expected + hash(data, h1.as_bytes(), h2.as_bytes()) == expected } /// Build the full data of a revision out its snapshot @@ -253,8 +254,8 @@ impl Revlog { /// Return the revlog data. fn data(&self) -> &[u8] { - match self.data_bytes { - Some(ref data_bytes) => &data_bytes, + match &self.data_bytes { + Some(data_bytes) => data_bytes, None => panic!( "forgot to load the data or trying to access inline data" ), diff --git a/rust/hg-core/src/revset.rs b/rust/hg-core/src/revset.rs --- a/rust/hg-core/src/revset.rs +++ b/rust/hg-core/src/revset.rs @@ -21,7 +21,7 @@ pub fn resolve_single( match input { "." => { let p1 = repo.dirstate_parents()?.p1; - return Ok(changelog.revlog.rev_from_node(p1.into())?); + return changelog.revlog.rev_from_node(p1.into()); } "null" => return Ok(NULL_REVISION), _ => {} @@ -33,7 +33,7 @@ pub fn resolve_single( let msg = format!("cannot parse revset '{}'", input); Err(HgError::unsupported(msg).into()) } - result => return result, + result => result, } } diff --git a/rust/hg-core/src/sparse.rs b/rust/hg-core/src/sparse.rs --- a/rust/hg-core/src/sparse.rs +++ b/rust/hg-core/src/sparse.rs @@ -164,7 +164,7 @@ pub(crate) fn parse_config( fn read_temporary_includes( repo: &Repo, ) -> Result>, SparseConfigError> { - let raw = repo.hg_vfs().try_read("tempsparse")?.unwrap_or(vec![]); + let raw = repo.hg_vfs().try_read("tempsparse")?.unwrap_or_default(); if raw.is_empty() { return Ok(vec![]); } @@ -179,7 +179,7 @@ fn patterns_for_rev( if !repo.has_sparse() { return Ok(None); } - let raw = repo.hg_vfs().try_read("sparse")?.unwrap_or(vec![]); + let raw = repo.hg_vfs().try_read("sparse")?.unwrap_or_default(); if raw.is_empty() { return Ok(None); @@ -200,9 +200,10 @@ fn patterns_for_rev( let output = cat(repo, &rev.to_string(), vec![HgPath::new(&profile)]) .map_err(|_| { - HgError::corrupted(format!( + HgError::corrupted( "dirstate points to non-existent parent node" - )) + .to_string(), + ) })?; if output.results.is_empty() { config.warnings.push(SparseWarning::ProfileNotFound { @@ -252,9 +253,9 @@ pub fn matcher( repo.changelog()? .rev_from_node(parents.p1.into()) .map_err(|_| { - HgError::corrupted(format!( - "dirstate points to non-existent parent node" - )) + HgError::corrupted( + "dirstate points to non-existent parent node".to_string(), + ) })?; if p1_rev != NULL_REVISION { revs.push(p1_rev) @@ -263,9 +264,9 @@ pub fn matcher( repo.changelog()? .rev_from_node(parents.p2.into()) .map_err(|_| { - HgError::corrupted(format!( - "dirstate points to non-existent parent node" - )) + HgError::corrupted( + "dirstate points to non-existent parent node".to_string(), + ) })?; if p2_rev != NULL_REVISION { revs.push(p2_rev) @@ -325,7 +326,7 @@ fn force_include_matcher( } let forced_include_matcher = IncludeMatcher::new( temp_includes - .into_iter() + .iter() .map(|include| { IgnorePattern::new(PatternSyntax::Path, include, Path::new("")) }) diff --git a/rust/hg-core/src/utils.rs b/rust/hg-core/src/utils.rs --- a/rust/hg-core/src/utils.rs +++ b/rust/hg-core/src/utils.rs @@ -137,11 +137,8 @@ impl SliceExt for [u8] { } fn split_2_by_slice(&self, separator: &[u8]) -> Option<(&[u8], &[u8])> { - if let Some(pos) = find_slice_in_slice(self, separator) { - Some((&self[..pos], &self[pos + separator.len()..])) - } else { - None - } + find_slice_in_slice(self, separator) + .map(|pos| (&self[..pos], &self[pos + separator.len()..])) } } @@ -369,7 +366,7 @@ where MergeResult::RightValue => { left.insert(key, right_value); } - MergeResult::UseNewValue(new_value) => { + MergeResult::NewValue(new_value) => { left.insert(key, new_value); } }, diff --git a/rust/hg-core/src/utils/files.rs b/rust/hg-core/src/utils/files.rs --- a/rust/hg-core/src/utils/files.rs +++ b/rust/hg-core/src/utils/files.rs @@ -230,7 +230,7 @@ pub fn canonical_path( // TODO hint to the user about using --cwd // Bubble up the responsibility to Python for now Err(HgPathError::NotUnderRoot { - path: original_name.to_owned(), + path: original_name, root: root.to_owned(), }) } @@ -424,7 +424,7 @@ mod tests { assert_eq!( canonical_path(&root, Path::new(""), &beneath_repo), Err(HgPathError::NotUnderRoot { - path: beneath_repo.to_owned(), + path: beneath_repo, root: root.to_owned() }) ); diff --git a/rust/hg-core/src/utils/hg_path.rs b/rust/hg-core/src/utils/hg_path.rs --- a/rust/hg-core/src/utils/hg_path.rs +++ b/rust/hg-core/src/utils/hg_path.rs @@ -205,7 +205,7 @@ impl HgPath { /// ``` pub fn split_filename(&self) -> (&Self, &Self) { match &self.inner.iter().rposition(|c| *c == b'/') { - None => (HgPath::new(""), &self), + None => (HgPath::new(""), self), Some(size) => ( HgPath::new(&self.inner[..*size]), HgPath::new(&self.inner[*size + 1..]), @@ -326,7 +326,7 @@ impl HgPath { #[cfg(unix)] /// Split a pathname into drive and path. On Posix, drive is always empty. pub fn split_drive(&self) -> (&HgPath, &HgPath) { - (HgPath::new(b""), &self) + (HgPath::new(b""), self) } /// Checks for errors in the path, short-circuiting at the first one. @@ -396,7 +396,7 @@ impl HgPathBuf { Default::default() } - pub fn push>(&mut self, other: &T) -> () { + pub fn push>(&mut self, other: &T) { if !self.inner.is_empty() && self.inner.last() != Some(&b'/') { self.inner.push(b'/'); } @@ -431,7 +431,7 @@ impl Deref for HgPathBuf { #[inline] fn deref(&self) -> &HgPath { - &HgPath::new(&self.inner) + HgPath::new(&self.inner) } } @@ -441,15 +441,15 @@ impl> From<&T> } } -impl Into> for HgPathBuf { - fn into(self) -> Vec { - self.inner +impl From for Vec { + fn from(val: HgPathBuf) -> Self { + val.inner } } impl Borrow for HgPathBuf { fn borrow(&self) -> &HgPath { - &HgPath::new(self.as_bytes()) + HgPath::new(self.as_bytes()) } } @@ -491,7 +491,7 @@ pub fn hg_path_to_os_string Vec> { let mut ancs: Vec> = Vec::new(); - for i in 0..vg.len() { + (0..vg.len()).for_each(|i| { let mut ancs_i = HashSet::new(); ancs_i.insert(i as Revision); for p in vg[i].iter().cloned() { @@ -62,7 +62,7 @@ fn ancestors_sets(vg: &VecGraph) -> Vec< } } ancs.push(ancs_i); - } + }); ancs }