From cabfc3b02d1edec03869fabdabf6a7f8b0519160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Gardstr=C3=B6m?= Date: Wed, 10 Jan 2024 00:57:00 +0100 Subject: [PATCH] make clippy happy --- src/docker/image.rs | 6 ++--- src/docker/shared.rs | 4 ++-- src/shell.rs | 9 ++++---- xtask/src/changelog.rs | 6 ++--- xtask/src/hooks.rs | 4 ++-- xtask/src/util.rs | 52 +++++++++++++++++++++--------------------- 6 files changed, 41 insertions(+), 40 deletions(-) diff --git a/src/docker/image.rs b/src/docker/image.rs index 2a469fe70..cbce5c066 100644 --- a/src/docker/image.rs +++ b/src/docker/image.rs @@ -35,7 +35,7 @@ impl PossibleImage { } } else { let platform = if self.toolchain.len() == 1 { - self.toolchain.get(0).expect("should contain at least one") + self.toolchain.first().expect("should contain at least one") } else { let same_arch = self .toolchain @@ -48,7 +48,7 @@ impl PossibleImage { if same_arch.len() == 1 { // pick the platform with the same architecture - same_arch.get(0).expect("should contain one element") + same_arch.first().expect("should contain one element") } else if let Some(platform) = same_arch .iter() .find(|platform| &platform.os == engine.os.as_ref().unwrap_or(&Os::Linux)) @@ -62,7 +62,7 @@ impl PossibleImage { } else { let platform = self .toolchain - .get(0) + .first() .expect("should be at least one platform"); // FIXME: Don't throw away msg_info.warn( diff --git a/src/docker/shared.rs b/src/docker/shared.rs index b0f19a7b4..5040f8e28 100644 --- a/src/docker/shared.rs +++ b/src/docker/shared.rs @@ -1278,7 +1278,7 @@ pub fn get_image_name( }; Ok(compatible - .get(0) + .first() .expect("should not be empty") .image_name(CROSS_IMAGE, version)) } @@ -1316,7 +1316,7 @@ pub fn get_image( let pick = if compatible.len() == 1 { // If only one match, use that - compatible.get(0).expect("should not be empty") + compatible.first().expect("should not be empty") } else if compatible .iter() .filter(|provided| provided.sub.is_none()) diff --git a/src/shell.rs b/src/shell.rs index 96498bc37..f3c36b639 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -481,8 +481,9 @@ pub fn default_ident() -> usize { #[must_use] pub fn indent(message: &str, spaces: usize) -> String { - message - .lines() - .map(|s| format!("{:spaces$}{s}", "")) - .collect() + use std::fmt::Write as _; + message.lines().fold(String::new(), |mut string, line| { + let _ = write!(string, "{:spaces$}{line}", ""); + string + }) } diff --git a/xtask/src/changelog.rs b/xtask/src/changelog.rs index 1b4652fe9..49c5f15a0 100644 --- a/xtask/src/changelog.rs +++ b/xtask/src/changelog.rs @@ -96,7 +96,7 @@ impl IdType { impl cmp::PartialOrd for IdType { fn partial_cmp(&self, other: &IdType) -> Option { - self.max_number().partial_cmp(&other.max_number()) + Some(self.cmp(other)) } } @@ -141,7 +141,7 @@ impl ChangelogType { impl cmp::PartialOrd for ChangelogType { fn partial_cmp(&self, other: &ChangelogType) -> Option { - self.sort_by().partial_cmp(&other.sort_by()) + Some(self.cmp(other)) } } @@ -171,7 +171,7 @@ impl ChangelogContents { impl cmp::PartialOrd for ChangelogContents { fn partial_cmp(&self, other: &ChangelogContents) -> Option { - self.sort_by().partial_cmp(&other.sort_by()) + Some(self.cmp(other)) } } diff --git a/xtask/src/hooks.rs b/xtask/src/hooks.rs index a3acf7ca7..57dcbbe46 100644 --- a/xtask/src/hooks.rs +++ b/xtask/src/hooks.rs @@ -140,7 +140,7 @@ fn python_lint(flake8: Option<&str>, msg_info: &mut MessageInfo) -> cross::Resul .map(parse_command) .unwrap_or_else(|| Ok(vec!["flake8".to_owned()]))?; let mut cmd = Command::new( - args.get(0) + args.first() .ok_or_else(|| eyre::eyre!("empty string provided for flake8 command"))?, ); cmd.args(&args[1..]); @@ -160,7 +160,7 @@ fn python_test(tox: Option<&str>, msg_info: &mut MessageInfo) -> cross::Result<( .map(parse_command) .unwrap_or_else(|| Ok(vec!["tox".to_owned()]))?; let mut cmd = Command::new( - args.get(0) + args.first() .ok_or_else(|| eyre::eyre!("empty string provided for tox command"))?, ); cmd.args(&args[1..]); diff --git a/xtask/src/util.rs b/xtask/src/util.rs index 28e687bc4..64cbe4ae9 100644 --- a/xtask/src/util.rs +++ b/xtask/src/util.rs @@ -336,6 +336,32 @@ pub fn read_dockerfiles(msg_info: &mut MessageInfo) -> cross::Result cross::Result<()> { + let mut file = fs::OpenOptions::new() + .write(true) + .truncate(true) + .create(true) + .open(path)?; + writeln!(file, "{}", contents)?; + Ok(()) +} + +// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#environment-files +pub fn write_to_gha_env_file(env_name: &str, contents: &str) -> cross::Result<()> { + let path = if let Ok(path) = env::var(env_name) { + PathBuf::from(path) + } else { + eyre::ensure!( + env::var("GITHUB_ACTIONS").is_err(), + "expected GHA envfile to exist" + ); + return Ok(()); + }; + let mut file = fs::OpenOptions::new().append(true).open(path)?; + writeln!(file, "{}", contents)?; + Ok(()) +} + #[cfg(test)] mod tests { use super::*; @@ -416,29 +442,3 @@ mod tests { } } } - -pub fn write_to_string(path: &Path, contents: &str) -> cross::Result<()> { - let mut file = fs::OpenOptions::new() - .write(true) - .truncate(true) - .create(true) - .open(path)?; - writeln!(file, "{}", contents)?; - Ok(()) -} - -// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#environment-files -pub fn write_to_gha_env_file(env_name: &str, contents: &str) -> cross::Result<()> { - let path = if let Ok(path) = env::var(env_name) { - PathBuf::from(path) - } else { - eyre::ensure!( - env::var("GITHUB_ACTIONS").is_err(), - "expected GHA envfile to exist" - ); - return Ok(()); - }; - let mut file = fs::OpenOptions::new().append(true).open(path)?; - writeln!(file, "{}", contents)?; - Ok(()) -}