diff --git a/asyncgit/src/asyncjob/mod.rs b/asyncgit/src/asyncjob/mod.rs index f1dd645a..258faa0e 100644 --- a/asyncgit/src/asyncjob/mod.rs +++ b/asyncgit/src/asyncjob/mod.rs @@ -96,11 +96,7 @@ impl AsyncSingleJob { /// take out last finished job pub fn take_last(&self) -> Option { - if let Ok(mut last) = self.last.lock() { - last.take() - } else { - None - } + self.last.lock().map_or(None, |mut last| last.take()) } /// spawns `task` if nothing is running currently, @@ -164,11 +160,7 @@ impl AsyncSingleJob { } fn take_next(&self) -> Option { - if let Ok(mut next) = self.next.lock() { - next.take() - } else { - None - } + self.next.lock().map_or(None, |mut next| next.take()) } } diff --git a/asyncgit/src/sync/blame.rs b/asyncgit/src/sync/blame.rs index 0a5005d2..09d00855 100644 --- a/asyncgit/src/sync/blame.rs +++ b/asyncgit/src/sync/blame.rs @@ -131,12 +131,12 @@ pub fn blame_file( return ( Some(hunk), - line.unwrap_or_else(|_| "".into()), + line.unwrap_or_else(|_| String::new()), ); } } - (None, line.unwrap_or_else(|_| "".into())) + (None, line.unwrap_or_else(|_| String::new())) }) .collect(); diff --git a/asyncgit/src/sync/cred.rs b/asyncgit/src/sync/cred.rs index 4e7d7960..3f908a63 100644 --- a/asyncgit/src/sync/cred.rs +++ b/asyncgit/src/sync/cred.rs @@ -73,18 +73,19 @@ pub fn extract_username_password( /// extract credentials from url pub fn extract_cred_from_url(url: &str) -> BasicAuthCredential { - if let Ok(url) = url::Url::parse(url) { - BasicAuthCredential::new( - if url.username() == "" { - None - } else { - Some(url.username().to_owned()) - }, - url.password().map(std::borrow::ToOwned::to_owned), - ) - } else { - BasicAuthCredential::new(None, None) - } + url::Url::parse(url).map_or_else( + |_| BasicAuthCredential::new(None, None), + |url| { + BasicAuthCredential::new( + if url.username() == "" { + None + } else { + Some(url.username().to_owned()) + }, + url.password().map(std::borrow::ToOwned::to_owned), + ) + }, + ) } #[cfg(test)] diff --git a/asyncgit/src/sync/remotes/callbacks.rs b/asyncgit/src/sync/remotes/callbacks.rs index 7b448265..76e260a8 100644 --- a/asyncgit/src/sync/remotes/callbacks.rs +++ b/asyncgit/src/sync/remotes/callbacks.rs @@ -202,16 +202,15 @@ impl Callbacks { } match &self.basic_credential { - _ if allowed_types.is_ssh_key() => { - match username_from_url { - Some(username) => { - Cred::ssh_key_from_agent(username) - } - None => Err(GitError::from_str( - " Couldn't extract username from url.", - )), - } - } + _ if allowed_types.is_ssh_key() => username_from_url + .map_or_else( + || { + Err(GitError::from_str( + " Couldn't extract username from url.", + )) + }, + Cred::ssh_key_from_agent, + ), Some(BasicAuthCredential { username: Some(user), password: Some(pwd), diff --git a/asyncgit/src/sync/reset.rs b/asyncgit/src/sync/reset.rs index f057f231..76e12b69 100644 --- a/asyncgit/src/sync/reset.rs +++ b/asyncgit/src/sync/reset.rs @@ -13,9 +13,9 @@ pub fn reset_stage(repo_path: &RepoPath, path: &str) -> Result<()> { let obj = repo.find_object(id.into(), Some(ObjectType::Commit))?; - repo.reset_default(Some(&obj), &[path])?; + repo.reset_default(Some(&obj), [path])?; } else { - repo.reset_default(None, &[path])?; + repo.reset_default(None, [path])?; } Ok(()) diff --git a/asyncgit/src/sync/tags.rs b/asyncgit/src/sync/tags.rs index 9e9d3ba7..700c46fd 100644 --- a/asyncgit/src/sync/tags.rs +++ b/asyncgit/src/sync/tags.rs @@ -75,17 +75,20 @@ pub fn get_tags(repo_path: &RepoPath) -> Result { // works on annotated tags lightweight tags `id` already // points to the target commit // see https://github.com/libgit2/libgit2/issues/5586 - let commit = if let Ok(commit) = repo + let commit = repo .find_tag(id) .and_then(|tag| tag.target()) .and_then(|target| target.peel_to_commit()) - { - Some(CommitId::new(commit.id())) - } else if repo.find_commit(id).is_ok() { - Some(CommitId::new(id)) - } else { - None - }; + .map_or_else( + |_| { + if repo.find_commit(id).is_ok() { + Some(CommitId::new(id)) + } else { + None + } + }, + |commit| Some(CommitId::new(commit.id())), + ); let annotation = repo .find_tag(id) diff --git a/src/components/blame_file.rs b/src/components/blame_file.rs index 6d7fcc74..7125aca4 100644 --- a/src/components/blame_file.rs +++ b/src/components/blame_file.rs @@ -488,10 +488,9 @@ impl BlameFileComponent { truncated_author, author_width = MAX_AUTHOR_WIDTH ); - let time = blame_hunk.map_or_else( - || "".into(), - |hunk| utils::time_to_string(hunk.time, true), - ); + let time = blame_hunk.map_or_else(String::new, |hunk| { + utils::time_to_string(hunk.time, true) + }); let is_blamed_commit = self .file_blame diff --git a/src/components/commitlist.rs b/src/components/commitlist.rs index e8ebc817..4e0aa3f9 100644 --- a/src/components/commitlist.rs +++ b/src/components/commitlist.rs @@ -292,10 +292,9 @@ impl CommitList { // commit tags txt.push(Span::styled( - Cow::from(tags.map_or_else( - || String::from(""), - |tags| format!(" {}", tags), - )), + Cow::from(tags.map_or_else(String::new, |tags| { + format!(" {}", tags) + })), theme.tags(selected), )); diff --git a/src/components/revision_files.rs b/src/components/revision_files.rs index c9146620..50f7122c 100644 --- a/src/components/revision_files.rs +++ b/src/components/revision_files.rs @@ -126,7 +126,7 @@ impl RevisionFilesComponent { let indent = item.info().indent(); let indent_str = if indent == 0 { - String::from("") + String::new() } else { format!("{:w$}", " ", w = (indent as usize) * 2) }; diff --git a/src/components/status_tree.rs b/src/components/status_tree.rs index 67e84496..c5ad55bd 100644 --- a/src/components/status_tree.rs +++ b/src/components/status_tree.rs @@ -159,7 +159,7 @@ impl StatusTreeComponent { theme: &'b SharedTheme, ) -> Option> { let indent_str = if indent == 0 { - String::from("") + String::new() } else { format!("{:w$}", " ", w = (indent as usize) * 2) }; diff --git a/src/keys/key_config.rs b/src/keys/key_config.rs index 6016e52f..d9a4c51d 100644 --- a/src/keys/key_config.rs +++ b/src/keys/key_config.rs @@ -96,7 +96,7 @@ impl KeyConfig { KeyCode::Null => { self.get_modifier_hint(ev.modifiers).into() } - _ => "".into(), + _ => String::new(), } } diff --git a/src/tabs/status.rs b/src/tabs/status.rs index 622912c4..de25ffd5 100644 --- a/src/tabs/status.rs +++ b/src/tabs/status.rs @@ -264,21 +264,21 @@ impl Status { .join(",") ) } - RepoState::Rebase => { - if let Ok(p) = sync::rebase_progress(repo) { - format!( - "Step: {}/{} Current Commit: {}", - p.current + 1, - p.steps, - p.current_commit - .as_ref() - .map(CommitId::get_short_string) - .unwrap_or_default(), - ) - } else { - String::new() - } - } + RepoState::Rebase => sync::rebase_progress(repo) + .map_or_else( + |_| String::new(), + |p| { + format!( + "Step: {}/{} Current Commit: {}", + p.current + 1, + p.steps, + p.current_commit + .as_ref() + .map(CommitId::get_short_string) + .unwrap_or_default(), + ) + }, + ), RepoState::Revert => { format!( "Revert {}", diff --git a/src/ui/stateful_paragraph.rs b/src/ui/stateful_paragraph.rs index 21bcad14..40f52f65 100644 --- a/src/ui/stateful_paragraph.rs +++ b/src/ui/stateful_paragraph.rs @@ -121,14 +121,11 @@ impl<'a> StatefulWidget for StatefulParagraph<'a> { state: &mut Self::State, ) { buf.set_style(area, self.style); - let text_area = match self.block.take() { - Some(b) => { - let inner_area = b.inner(area); - b.render(area, buf); - inner_area - } - None => area, - }; + let text_area = self.block.take().map_or(area, |b| { + let inner_area = b.inner(area); + b.render(area, buf); + inner_area + }); if text_area.height < 1 { return;