From 8cd2951248426ac4245717a5c6fc374dec27eb18 Mon Sep 17 00:00:00 2001 From: Samuel Moelius Date: Sat, 9 Dec 2023 12:39:59 -0500 Subject: [PATCH] Enable `try_io_result` for `rustsec_utils` --- rustsec_util/Cargo.toml | 1 + rustsec_util/src/bin/rustsec_advisories.rs | 6 ++++-- rustsec_util/src/bin/rustsec_issues.rs | 6 ++++-- rustsec_util/src/lib.rs | 15 ++++++++++----- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/rustsec_util/Cargo.toml b/rustsec_util/Cargo.toml index 9fb539f4..cf9ecdc4 100644 --- a/rustsec_util/Cargo.toml +++ b/rustsec_util/Cargo.toml @@ -26,4 +26,5 @@ libraries = [ { git = "https://github.com/trailofbits/dylint", pattern = "examples/supplementary" }, { git = "https://github.com/trailofbits/dylint", pattern = "examples/restriction/inconsistent_qualification" }, { git = "https://github.com/trailofbits/dylint", pattern = "examples/restriction/suboptimal_pattern" }, + { git = "https://github.com/trailofbits/dylint", pattern = "examples/restriction/try_io_result" }, ] diff --git a/rustsec_util/src/bin/rustsec_advisories.rs b/rustsec_util/src/bin/rustsec_advisories.rs index 1039d07d..de20466a 100644 --- a/rustsec_util/src/bin/rustsec_advisories.rs +++ b/rustsec_util/src/bin/rustsec_advisories.rs @@ -1,4 +1,4 @@ -use anyhow::{ensure, Result}; +use anyhow::{ensure, Context, Result}; use cargo_metadata::MetadataCommand; use once_cell::sync::Lazy; use regex::Regex; @@ -44,7 +44,9 @@ fn main() -> Result<()> { for advisory in advisories { print!("{}...", advisory.metadata.package); - std::io::stdout().flush()?; + std::io::stdout() + .flush() + .with_context(|| "failed to flush stdout")?; let tempdir = test_package(advisory.metadata.package.as_str())?; diff --git a/rustsec_util/src/bin/rustsec_issues.rs b/rustsec_util/src/bin/rustsec_issues.rs index 7ea9b589..bc53aff6 100644 --- a/rustsec_util/src/bin/rustsec_issues.rs +++ b/rustsec_util/src/bin/rustsec_issues.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{Context, Result}; use log::debug; use once_cell::sync::Lazy; use regex::Regex; @@ -66,7 +66,9 @@ fn main() -> Result<()> { } checked.insert(name); print!("{name}..."); - std::io::stdout().flush()?; + std::io::stdout() + .flush() + .with_context(|| "failed to flush stdout")?; if is_unmaintained(name)? { println!("found"); advisory_outcomes.push((name, advisory_url.clone(), Outcome::Found)); diff --git a/rustsec_util/src/lib.rs b/rustsec_util/src/lib.rs index b505adc5..4ca090e2 100644 --- a/rustsec_util/src/lib.rs +++ b/rustsec_util/src/lib.rs @@ -1,4 +1,4 @@ -use anyhow::{ensure, Result}; +use anyhow::{ensure, Context, Result}; use once_cell::sync::Lazy; use std::{ env::consts::EXE_SUFFIX, @@ -82,7 +82,7 @@ pub fn display_advisory_outcomes Result { - let tempdir = tempdir()?; + let tempdir = tempdir().with_context(|| "failed to create temporary directory")?; let output = command_output( Command::new("cargo") @@ -91,10 +91,13 @@ pub fn test_package(package: &str) -> Result { )?; ensure!(output.status.success()); + let path = tempdir.path().join("Cargo.toml"); let mut manifest = OpenOptions::new() .append(true) - .open(tempdir.path().join("Cargo.toml"))?; - writeln!(manifest, r#"{package} = "*""#)?; + .open(&path) + .with_context(|| format!("failed to open {path:?}"))?; + writeln!(manifest, r#"{package} = "*""#) + .with_context(|| format!("failed to write to {path:?}"))?; Ok(tempdir) } @@ -119,7 +122,9 @@ pub fn cargo_unmaintained(name: &str, dir: &Path) -> Command { #[cfg_attr(dylint_lib = "general", allow(non_local_effect_before_error_return))] pub fn command_output(command: &mut Command) -> Result { - let output = command.output()?; + let output = command + .output() + .with_context(|| format!("failed to execute command: {command:?}"))?; let status = output.status; let stdout = String::from_utf8(output.stdout)?; let stderr = String::from_utf8(output.stderr)?;