From a74370460b7a0b8ef2688cc1a0f68503752e1e16 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 3 Sep 2025 20:09:32 -0500 Subject: [PATCH 1/2] refactor(cli): Remove unused return value --- src/cargo/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index 2da4310de29..e47b6eabde9 100644 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -206,14 +206,14 @@ pub fn display_warning_with_error(warning: &str, err: &Error, shell: &mut Shell) _display_error(err, shell, false); } -fn _display_error(err: &Error, shell: &mut Shell, as_err: bool) -> bool { +fn _display_error(err: &Error, shell: &mut Shell, as_err: bool) { for (i, err) in err.chain().enumerate() { // If we're not in verbose mode then only print cause chain until one // marked as `VerboseError` appears. // // Generally the top error shouldn't be verbose, but check it anyways. if shell.verbosity() != Verbose && err.is::() { - return true; + break; } if err.is::() { break; @@ -229,5 +229,4 @@ fn _display_error(err: &Error, shell: &mut Shell, as_err: bool) -> bool { drop(write!(shell.err(), "{}", indented_lines(&err.to_string()))); } } - false } From 4d341c99255a6bbb4d4bd0c91fc95bf0d57ca3c6 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 3 Sep 2025 20:17:04 -0500 Subject: [PATCH 2/2] refactor(cli): Pull out error chain iteration --- src/cargo/lib.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index e47b6eabde9..cee6ce4dec2 100644 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -140,6 +140,7 @@ //! [Cargo Contributor Guide]: https://doc.crates.io/contrib/ use crate::core::Shell; +use crate::core::shell::Verbosity; use crate::core::shell::Verbosity::Verbose; use anyhow::Error; use tracing::debug; @@ -206,18 +207,21 @@ pub fn display_warning_with_error(warning: &str, err: &Error, shell: &mut Shell) _display_error(err, shell, false); } +fn error_chain(err: &Error, verbosity: Verbosity) -> impl Iterator { + err.chain() + .take_while(move |err| { + // If we're not in verbose mode then only print cause chain until one + // marked as `VerboseError` appears. + // + // Generally the top error shouldn't be verbose, but check it anyways. + verbosity == Verbose || !err.is::() + }) + .take_while(|err| !err.is::()) + .map(|err| err as &dyn std::fmt::Display) +} + fn _display_error(err: &Error, shell: &mut Shell, as_err: bool) { - for (i, err) in err.chain().enumerate() { - // If we're not in verbose mode then only print cause chain until one - // marked as `VerboseError` appears. - // - // Generally the top error shouldn't be verbose, but check it anyways. - if shell.verbosity() != Verbose && err.is::() { - break; - } - if err.is::() { - break; - } + for (i, err) in error_chain(err, shell.verbosity()).enumerate() { if i == 0 { if as_err { drop(shell.error(&err));