diff --git a/src/cargo/core/compiler/job_queue/mod.rs b/src/cargo/core/compiler/job_queue/mod.rs index 8671190a93d..ad0c3e37844 100644 --- a/src/cargo/core/compiler/job_queue/mod.rs +++ b/src/cargo/core/compiler/job_queue/mod.rs @@ -144,7 +144,7 @@ use crate::core::compiler::future_incompat::{ }; use crate::core::resolver::ResolveBehavior; use crate::core::{PackageId, TargetKind}; -use crate::diagnostics::DiagnosticStats; +use crate::diagnostics::GlobalDiagnosticStats; use crate::diagnostics::rules::unused_dependencies; use crate::util::CargoResult; use crate::util::context::WarningHandling; @@ -844,7 +844,7 @@ impl<'gctx> DrainState<'gctx> { self.progress.clear(); if build_runner.bcx.gctx.cli_unstable().cargo_lints { - let mut global_stats = DiagnosticStats::new(); + let mut global_stats = GlobalDiagnosticStats::new(); drop(unused_dependencies::lint_build_results( build_runner, &mut global_stats, diff --git a/src/cargo/diagnostics/mod.rs b/src/cargo/diagnostics/mod.rs index 3244c7bee55..3840b4bde95 100644 --- a/src/cargo/diagnostics/mod.rs +++ b/src/cargo/diagnostics/mod.rs @@ -44,7 +44,7 @@ //! When a diagnostic requires adding a new pass, keep in mind: //! - Support for `build.warnings` //! - When errors should block further evaluation within the pass -//! - Providing a summary at the end, like what is provided by [`DiagnosticStats::report_summary`] +//! - Providing a summary at the end, like what is provided by [`ScopedDiagnosticStats::report_summary`] //! - Prefer data driven passes to simplify adding rules //! - Ensure the pass' lints are in [`rules::LINTS`], e.g. `ensure_parse_passed_in_lints` //! - Prefer evaluating the lint level within the pass @@ -53,7 +53,6 @@ //! //! [future-incompat lint]: https://rustc-dev-guide.rust-lang.org/diagnostics.html#future-incompatible-lints -use anyhow::bail; use cargo_util_schemas::manifest::RustVersion; use cargo_util_schemas::manifest::TomlToolLints; @@ -72,21 +71,47 @@ pub use lint::{Lint, LintGroup, LintLevel, LintLevelProduct, LintLevelSource}; pub use report::{AsIndex, get_key_value, get_key_value_span, rel_cwd_manifest_path}; pub use rules::{LINT_GROUPS, LINTS}; -pub struct DiagnosticStats { - warning_count: usize, - lint_warning_count: usize, +pub struct GlobalDiagnosticStats { error_count: usize, } -impl DiagnosticStats { +impl GlobalDiagnosticStats { pub fn new() -> Self { - Self { + Self { error_count: 0 } + } + + pub fn scope(&mut self) -> ScopedDiagnosticStats<'_> { + ScopedDiagnosticStats { warning_count: 0, lint_warning_count: 0, error_count: 0, + global: self, } } + pub fn error_count(&self) -> usize { + self.error_count + } + + pub fn ok(&self) -> CargoResult<()> { + if 0 < self.error_count { + Err(crate::Error::new(crate::AlreadyPrintedError::new( + anyhow::format_err!("see above"), + ))) + } else { + Ok(()) + } + } +} + +pub struct ScopedDiagnosticStats<'g> { + warning_count: usize, + lint_warning_count: usize, + error_count: usize, + global: &'g mut GlobalDiagnosticStats, +} + +impl ScopedDiagnosticStats<'_> { pub fn lint_warning_count(&self) -> usize { self.lint_warning_count } @@ -105,6 +130,7 @@ impl DiagnosticStats { pub fn record_error(&mut self) { self.error_count += 1; + self.global.error_count += 1; } pub fn record_lint(&mut self, lint: LintLevel) { @@ -120,6 +146,9 @@ impl DiagnosticStats { } } + /// Print a summary to the user + /// + /// **Note:** be sure to call `GlobalDiagnosticStats::ok` or equivalent to fail the operation pub fn report_summary( &self, action: &str, @@ -142,38 +171,16 @@ impl DiagnosticStats { let name = name .map(|n| format!("`{n}`")) .unwrap_or_else(|| "workspace".to_owned()); - bail!( + gctx.shell().error(format!( "could not {action} {name} (manifest) due to {} previous error{plural}", self.error_count - ) + ))?; } Ok(()) } } -impl std::ops::Add for DiagnosticStats { - type Output = DiagnosticStats; - - fn add(mut self, rhs: Self) -> Self::Output { - self += rhs; - self - } -} - -impl std::ops::AddAssign for DiagnosticStats { - fn add_assign(&mut self, rhs: Self) { - let DiagnosticStats { - warning_count, - lint_warning_count, - error_count, - } = rhs; - self.warning_count += warning_count; - self.lint_warning_count += lint_warning_count; - self.error_count += error_count; - } -} - /// Scope at which a lint runs: package-level or workspace-level. pub enum ManifestFor<'a> { /// Lint runs for a specific package. diff --git a/src/cargo/diagnostics/passes.rs b/src/cargo/diagnostics/passes.rs index c0281f841fe..13f93c63ef9 100644 --- a/src/cargo/diagnostics/passes.rs +++ b/src/cargo/diagnostics/passes.rs @@ -7,11 +7,12 @@ use crate::GlobalContext; use crate::core::MaybePackage; use crate::core::Package; use crate::core::Workspace; -use crate::diagnostics::DiagnosticStats; +use crate::diagnostics::GlobalDiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevel; use crate::diagnostics::LintLevelProduct; use crate::diagnostics::ManifestFor; +use crate::diagnostics::ScopedDiagnosticStats; #[derive(Clone)] pub enum ParsePassRule<'r> { @@ -39,24 +40,29 @@ pub enum ParsePassRule<'r> { } type FnDiagnosticManifest = - fn(ManifestFor<'_>, &Path, &mut DiagnosticStats, &GlobalContext) -> CargoResult<()>; + fn(ManifestFor<'_>, &Path, &mut ScopedDiagnosticStats<'_>, &GlobalContext) -> CargoResult<()>; type FnDiagnosticWorkspace = fn( &Workspace<'_>, &MaybePackage, &Path, - &mut DiagnosticStats, + &mut ScopedDiagnosticStats<'_>, &GlobalContext, ) -> CargoResult<()>; -type FnDiagnosticPackage = - fn(&Workspace<'_>, &Package, &Path, &mut DiagnosticStats, &GlobalContext) -> CargoResult<()>; +type FnDiagnosticPackage = fn( + &Workspace<'_>, + &Package, + &Path, + &mut ScopedDiagnosticStats<'_>, + &GlobalContext, +) -> CargoResult<()>; type FnLintManifest = fn( manifest: ManifestFor<'_>, manifest_path: &Path, LintLevelProduct, - stats: &mut DiagnosticStats, + stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()>; @@ -65,7 +71,7 @@ type FnLintWorkspace = fn( &MaybePackage, &Path, LintLevelProduct, - &mut DiagnosticStats, + &mut ScopedDiagnosticStats<'_>, &GlobalContext, ) -> CargoResult<()>; @@ -74,7 +80,7 @@ type FnLintPackage = fn( &Package, &Path, LintLevelProduct, - &mut DiagnosticStats, + &mut ScopedDiagnosticStats<'_>, &GlobalContext, ) -> CargoResult<()>; @@ -82,28 +88,18 @@ pub fn emit_parse_diagnostics( workspace: &Workspace<'_>, rules: &[ParsePassRule<'_>], ) -> CargoResult<()> { - let mut first_emitted_error = None; + let mut stats = GlobalDiagnosticStats::new(); - if let Err(e) = emit_parse_ws_diagnostics(workspace, rules) { - first_emitted_error = Some(e); - } + emit_parse_ws_diagnostics(workspace, rules, &mut stats)?; for maybe_pkg in workspace.loaded_maybe() { if let MaybePackage::Package(pkg) = maybe_pkg { let path = pkg.manifest_path(); - if let Err(e) = emit_parse_pkg_diagnostics(workspace, pkg, &path, rules) - && first_emitted_error.is_none() - { - first_emitted_error = Some(e); - } + emit_parse_pkg_diagnostics(workspace, pkg, &path, rules, &mut stats)?; } } - if let Some(error) = first_emitted_error { - Err(error) - } else { - Ok(()) - } + stats.ok() } fn emit_parse_pkg_diagnostics( @@ -111,8 +107,9 @@ fn emit_parse_pkg_diagnostics( pkg: &Package, path: &Path, rules: &[ParsePassRule<'_>], + global_stats: &mut GlobalDiagnosticStats, ) -> CargoResult<()> { - let mut pkg_stats = DiagnosticStats::new(); + let mut pkg_stats = global_stats.scope(); let toml_lints = pkg .manifest() @@ -176,8 +173,9 @@ fn emit_parse_pkg_diagnostics( fn emit_parse_ws_diagnostics( workspace: &Workspace<'_>, rules: &[ParsePassRule<'_>], + global_stats: &mut GlobalDiagnosticStats, ) -> CargoResult<()> { - let mut pkg_stats = DiagnosticStats::new(); + let mut pkg_stats = global_stats.scope(); let cargo_lints = match workspace.root_maybe() { MaybePackage::Package(pkg) => { diff --git a/src/cargo/diagnostics/rules/blanket_hint_mostly_unused.rs b/src/cargo/diagnostics/rules/blanket_hint_mostly_unused.rs index 059469a17c2..7534bcb6bca 100644 --- a/src/cargo/diagnostics/rules/blanket_hint_mostly_unused.rs +++ b/src/cargo/diagnostics/rules/blanket_hint_mostly_unused.rs @@ -14,9 +14,9 @@ use crate::CargoResult; use crate::GlobalContext; use crate::core::MaybePackage; use crate::core::Workspace; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevelProduct; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::get_key_value_span; use crate::diagnostics::rel_cwd_manifest_path; @@ -61,7 +61,7 @@ pub(crate) fn lint_workspace( maybe_pkg: &MaybePackage, path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let LintLevelProduct { diff --git a/src/cargo/diagnostics/rules/deferred_parse_diagnostics.rs b/src/cargo/diagnostics/rules/deferred_parse_diagnostics.rs index c0a222b62d2..55d39011f15 100644 --- a/src/cargo/diagnostics/rules/deferred_parse_diagnostics.rs +++ b/src/cargo/diagnostics/rules/deferred_parse_diagnostics.rs @@ -5,15 +5,15 @@ use tracing::instrument; use crate::CargoResult; use crate::GlobalContext; use crate::core::MaybePackage; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::ManifestFor; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::rel_cwd_manifest_path; #[instrument(skip_all)] pub(crate) fn diagnose_manifest( manifest: ManifestFor<'_>, manifest_path: &Path, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let warnings = match &manifest { diff --git a/src/cargo/diagnostics/rules/im_a_teapot.rs b/src/cargo/diagnostics/rules/im_a_teapot.rs index 2dc414f78c9..b5d8542caa2 100644 --- a/src/cargo/diagnostics/rules/im_a_teapot.rs +++ b/src/cargo/diagnostics/rules/im_a_teapot.rs @@ -13,9 +13,9 @@ use crate::GlobalContext; use crate::core::Feature; use crate::core::Package; use crate::core::Workspace; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevelProduct; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::get_key_value_span; use crate::diagnostics::rel_cwd_manifest_path; @@ -35,7 +35,7 @@ pub(crate) fn lint_package( pkg: &Package, path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let manifest = pkg.manifest(); diff --git a/src/cargo/diagnostics/rules/implicit_minimum_version_req.rs b/src/cargo/diagnostics/rules/implicit_minimum_version_req.rs index 83053ac7431..3299467be48 100644 --- a/src/cargo/diagnostics/rules/implicit_minimum_version_req.rs +++ b/src/cargo/diagnostics/rules/implicit_minimum_version_req.rs @@ -19,11 +19,11 @@ use crate::core::Manifest; use crate::core::MaybePackage; use crate::core::Package; use crate::core::Workspace; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevel; use crate::diagnostics::LintLevelProduct; use crate::diagnostics::LintLevelSource; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::get_key_value; use crate::diagnostics::rel_cwd_manifest_path; use crate::util::OptVersionReq; @@ -90,7 +90,7 @@ pub(crate) fn lint_package( pkg: &Package, manifest_path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let LintLevelProduct { @@ -151,7 +151,7 @@ pub(crate) fn lint_workspace( maybe_pkg: &MaybePackage, manifest_path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let LintLevelProduct { diff --git a/src/cargo/diagnostics/rules/missing_lints_features.rs b/src/cargo/diagnostics/rules/missing_lints_features.rs index c7d78614e8a..28257ed2046 100644 --- a/src/cargo/diagnostics/rules/missing_lints_features.rs +++ b/src/cargo/diagnostics/rules/missing_lints_features.rs @@ -12,8 +12,8 @@ use crate::CargoResult; use crate::GlobalContext; use crate::core::Feature; use crate::core::MaybePackage; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::ManifestFor; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::get_key_value_span; use crate::diagnostics::rel_cwd_manifest_path; @@ -21,7 +21,7 @@ use crate::diagnostics::rel_cwd_manifest_path; pub(crate) fn diagnose_manifest( manifest: ManifestFor<'_>, manifest_path: &Path, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let normalized_toml = match &manifest { @@ -64,7 +64,7 @@ fn diagnose_manifest_inner( manifest: &ManifestFor<'_>, manifest_path: &Path, cargo_lints: &manifest::TomlToolLints, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let manifest_path = rel_cwd_manifest_path(manifest_path, gctx); @@ -104,7 +104,7 @@ fn report_feature_not_enabled( feature_gate: &Feature, manifest: &ManifestFor<'_>, manifest_path: &str, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let dash_feature_name = feature_gate.name().replace("_", "-"); diff --git a/src/cargo/diagnostics/rules/missing_lints_inheritance.rs b/src/cargo/diagnostics/rules/missing_lints_inheritance.rs index 7f918e34b2b..a173db6c12f 100644 --- a/src/cargo/diagnostics/rules/missing_lints_inheritance.rs +++ b/src/cargo/diagnostics/rules/missing_lints_inheritance.rs @@ -12,9 +12,9 @@ use crate::CargoResult; use crate::GlobalContext; use crate::core::Package; use crate::core::Workspace; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevelProduct; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::rel_cwd_manifest_path; pub static LINT: &Lint = &Lint { @@ -59,7 +59,7 @@ pub(crate) fn lint_package( pkg: &Package, manifest_path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let LintLevelProduct { diff --git a/src/cargo/diagnostics/rules/non_kebab_case_bins.rs b/src/cargo/diagnostics/rules/non_kebab_case_bins.rs index e73eeee5e2c..57a9907511e 100644 --- a/src/cargo/diagnostics/rules/non_kebab_case_bins.rs +++ b/src/cargo/diagnostics/rules/non_kebab_case_bins.rs @@ -14,11 +14,11 @@ use crate::GlobalContext; use crate::core::Package; use crate::core::Workspace; use crate::diagnostics::AsIndex; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevel; use crate::diagnostics::LintLevelProduct; use crate::diagnostics::LintLevelSource; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::get_key_value_span; use crate::diagnostics::rel_cwd_manifest_path; @@ -69,7 +69,7 @@ pub(crate) fn lint_package( pkg: &Package, manifest_path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let LintLevelProduct { @@ -88,7 +88,7 @@ fn lint_package_inner( manifest_path: &str, lint_level: LintLevel, source: LintLevelSource, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let manifest = pkg.manifest(); diff --git a/src/cargo/diagnostics/rules/non_kebab_case_features.rs b/src/cargo/diagnostics/rules/non_kebab_case_features.rs index 524391fd467..c697467b71b 100644 --- a/src/cargo/diagnostics/rules/non_kebab_case_features.rs +++ b/src/cargo/diagnostics/rules/non_kebab_case_features.rs @@ -13,11 +13,11 @@ use crate::CargoResult; use crate::GlobalContext; use crate::core::Package; use crate::core::Workspace; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevel; use crate::diagnostics::LintLevelProduct; use crate::diagnostics::LintLevelSource; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::get_key_value_span; use crate::diagnostics::rel_cwd_manifest_path; @@ -64,7 +64,7 @@ pub(crate) fn lint_package( pkg: &Package, manifest_path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let LintLevelProduct { @@ -82,7 +82,7 @@ fn lint_package_inner( manifest_path: &str, lint_level: LintLevel, source: LintLevelSource, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { for original_name in pkg.summary().features().keys() { diff --git a/src/cargo/diagnostics/rules/non_kebab_case_packages.rs b/src/cargo/diagnostics/rules/non_kebab_case_packages.rs index 186c49172d1..a64fb05d044 100644 --- a/src/cargo/diagnostics/rules/non_kebab_case_packages.rs +++ b/src/cargo/diagnostics/rules/non_kebab_case_packages.rs @@ -13,11 +13,11 @@ use crate::CargoResult; use crate::GlobalContext; use crate::core::Package; use crate::core::Workspace; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevel; use crate::diagnostics::LintLevelProduct; use crate::diagnostics::LintLevelSource; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::get_key_value_span; use crate::diagnostics::rel_cwd_manifest_path; @@ -64,7 +64,7 @@ pub(crate) fn lint_package( pkg: &Package, manifest_path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let LintLevelProduct { @@ -82,7 +82,7 @@ fn lint_package_inner( manifest_path: &str, lint_level: LintLevel, source: LintLevelSource, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let manifest = pkg.manifest(); diff --git a/src/cargo/diagnostics/rules/non_snake_case_features.rs b/src/cargo/diagnostics/rules/non_snake_case_features.rs index d48a1a01c55..d8f83c73302 100644 --- a/src/cargo/diagnostics/rules/non_snake_case_features.rs +++ b/src/cargo/diagnostics/rules/non_snake_case_features.rs @@ -13,11 +13,11 @@ use crate::CargoResult; use crate::GlobalContext; use crate::core::Package; use crate::core::Workspace; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevel; use crate::diagnostics::LintLevelProduct; use crate::diagnostics::LintLevelSource; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::get_key_value_span; use crate::diagnostics::rel_cwd_manifest_path; @@ -64,7 +64,7 @@ pub(crate) fn lint_package( pkg: &Package, manifest_path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let LintLevelProduct { @@ -82,7 +82,7 @@ fn lint_package_inner( manifest_path: &str, lint_level: LintLevel, source: LintLevelSource, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { for original_name in pkg.summary().features().keys() { diff --git a/src/cargo/diagnostics/rules/non_snake_case_packages.rs b/src/cargo/diagnostics/rules/non_snake_case_packages.rs index 0f24249b654..b7f0f58df69 100644 --- a/src/cargo/diagnostics/rules/non_snake_case_packages.rs +++ b/src/cargo/diagnostics/rules/non_snake_case_packages.rs @@ -13,11 +13,11 @@ use crate::CargoResult; use crate::GlobalContext; use crate::core::Package; use crate::core::Workspace; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevel; use crate::diagnostics::LintLevelProduct; use crate::diagnostics::LintLevelSource; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::get_key_value_span; use crate::diagnostics::rel_cwd_manifest_path; @@ -64,7 +64,7 @@ pub(crate) fn lint_package( pkg: &Package, manifest_path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let LintLevelProduct { @@ -82,7 +82,7 @@ fn lint_package_inner( manifest_path: &str, lint_level: LintLevel, source: LintLevelSource, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let manifest = pkg.manifest(); diff --git a/src/cargo/diagnostics/rules/redundant_homepage.rs b/src/cargo/diagnostics/rules/redundant_homepage.rs index e20e43a9850..f2d834e7ab8 100644 --- a/src/cargo/diagnostics/rules/redundant_homepage.rs +++ b/src/cargo/diagnostics/rules/redundant_homepage.rs @@ -14,11 +14,11 @@ use crate::CargoResult; use crate::GlobalContext; use crate::core::Package; use crate::core::Workspace; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevel; use crate::diagnostics::LintLevelProduct; use crate::diagnostics::LintLevelSource; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::get_key_value_span; use crate::diagnostics::rel_cwd_manifest_path; @@ -68,7 +68,7 @@ pub(crate) fn lint_package( pkg: &Package, manifest_path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let LintLevelProduct { @@ -86,7 +86,7 @@ fn lint_package_inner( manifest_path: &str, lint_level: LintLevel, source: LintLevelSource, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let manifest = pkg.manifest(); diff --git a/src/cargo/diagnostics/rules/redundant_readme.rs b/src/cargo/diagnostics/rules/redundant_readme.rs index 778a435d204..0dd2a1f2b54 100644 --- a/src/cargo/diagnostics/rules/redundant_readme.rs +++ b/src/cargo/diagnostics/rules/redundant_readme.rs @@ -15,11 +15,11 @@ use crate::CargoResult; use crate::GlobalContext; use crate::core::Package; use crate::core::Workspace; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevel; use crate::diagnostics::LintLevelProduct; use crate::diagnostics::LintLevelSource; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::get_key_value_span; use crate::diagnostics::rel_cwd_manifest_path; use crate::util::toml::DEFAULT_README_FILES; @@ -70,7 +70,7 @@ pub(crate) fn lint_package( pkg: &Package, manifest_path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let LintLevelProduct { @@ -88,7 +88,7 @@ fn lint_package_inner( manifest_path: &str, lint_level: LintLevel, source: LintLevelSource, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let manifest = pkg.manifest(); diff --git a/src/cargo/diagnostics/rules/text_direction_codepoint_in_comment.rs b/src/cargo/diagnostics/rules/text_direction_codepoint_in_comment.rs index f6cba469faf..a937b8cd7d1 100644 --- a/src/cargo/diagnostics/rules/text_direction_codepoint_in_comment.rs +++ b/src/cargo/diagnostics/rules/text_direction_codepoint_in_comment.rs @@ -16,10 +16,10 @@ use super::CORRECTNESS; use crate::CargoResult; use crate::GlobalContext; use crate::core::MaybePackage; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevelProduct; use crate::diagnostics::ManifestFor; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::rel_cwd_manifest_path; pub static LINT: &Lint = &Lint { @@ -51,7 +51,7 @@ pub(crate) fn lint_manifest( manifest: ManifestFor<'_>, manifest_path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let LintLevelProduct { diff --git a/src/cargo/diagnostics/rules/text_direction_codepoint_in_literal.rs b/src/cargo/diagnostics/rules/text_direction_codepoint_in_literal.rs index f02b092cff1..62967bae365 100644 --- a/src/cargo/diagnostics/rules/text_direction_codepoint_in_literal.rs +++ b/src/cargo/diagnostics/rules/text_direction_codepoint_in_literal.rs @@ -17,10 +17,10 @@ use super::CORRECTNESS; use crate::CargoResult; use crate::GlobalContext; use crate::core::MaybePackage; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevelProduct; use crate::diagnostics::ManifestFor; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::rel_cwd_manifest_path; pub static LINT: &Lint = &Lint { @@ -52,7 +52,7 @@ pub(crate) fn lint_manifest( manifest: ManifestFor<'_>, manifest_path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let LintLevelProduct { diff --git a/src/cargo/diagnostics/rules/unknown_lints.rs b/src/cargo/diagnostics/rules/unknown_lints.rs index c879bca6b76..50157e3adf5 100644 --- a/src/cargo/diagnostics/rules/unknown_lints.rs +++ b/src/cargo/diagnostics/rules/unknown_lints.rs @@ -15,10 +15,10 @@ use super::find_lint_or_group; use crate::CargoResult; use crate::GlobalContext; use crate::core::MaybePackage; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevelProduct; use crate::diagnostics::ManifestFor; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::get_key_value_span; use crate::diagnostics::rel_cwd_manifest_path; @@ -53,7 +53,7 @@ pub(crate) fn lint_manifest( manifest: ManifestFor<'_>, manifest_path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let normalized_toml = match &manifest { @@ -111,7 +111,7 @@ fn lint_manifest_inner( manifest_path: &Path, level: &LintLevelProduct, cargo_lints: &TomlToolLints, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let LintLevelProduct { diff --git a/src/cargo/diagnostics/rules/unused_dependencies.rs b/src/cargo/diagnostics/rules/unused_dependencies.rs index a4b5042c148..b6b36405e0c 100644 --- a/src/cargo/diagnostics/rules/unused_dependencies.rs +++ b/src/cargo/diagnostics/rules/unused_dependencies.rs @@ -23,10 +23,11 @@ use crate::core::compiler::Unit; use crate::core::compiler::unused_deps::DependenciesState; use crate::core::compiler::unused_deps::UnusedDepState; use crate::core::dependency::DepKind; -use crate::diagnostics::DiagnosticStats; +use crate::diagnostics::GlobalDiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevel; use crate::diagnostics::LintLevelProduct; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::get_key_value_span; use crate::diagnostics::rel_cwd_manifest_path; @@ -99,7 +100,7 @@ pub(crate) fn lint_package( pkg: &Package, manifest_path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let LintLevelProduct { @@ -172,7 +173,7 @@ pub(crate) fn lint_package( #[instrument(skip_all)] pub fn lint_build_results( build_runner: &BuildRunner<'_, '_>, - global_stats: &mut DiagnosticStats, + global_stats: &mut GlobalDiagnosticStats, ) -> CargoResult<()> { for (pkg_id, states) in &build_runner.unused_dep_state.states { let Some(pkg) = get_package(&build_runner.unused_dep_state, pkg_id) else { @@ -207,15 +208,9 @@ pub fn lint_build_results( continue; } - let mut pkg_stats = DiagnosticStats::new(); + let mut pkg_stats = global_stats.scope(); lint_package_build_results(build_runner, pkg, states, level, &mut pkg_stats)?; - // HACK: as other rules are added to this pass, this needs to move up into the pass - if let Err(error) = - pkg_stats.report_summary("finalize", Some(&*pkg.name()), build_runner.bcx.gctx) - { - build_runner.bcx.gctx.shell().error(error)?; - } - *global_stats += pkg_stats; + pkg_stats.report_summary("finalize", Some(&*pkg.name()), build_runner.bcx.gctx)?; } Ok(()) } @@ -225,7 +220,7 @@ fn lint_package_build_results( pkg: &Package, states: &IndexMap, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, ) -> CargoResult<()> { let mut lint_count = 0; let LintLevelProduct { diff --git a/src/cargo/diagnostics/rules/unused_workspace_dependencies.rs b/src/cargo/diagnostics/rules/unused_workspace_dependencies.rs index 4dfafdd7c93..579ec45f8ff 100644 --- a/src/cargo/diagnostics/rules/unused_workspace_dependencies.rs +++ b/src/cargo/diagnostics/rules/unused_workspace_dependencies.rs @@ -15,9 +15,9 @@ use crate::CargoResult; use crate::GlobalContext; use crate::core::MaybePackage; use crate::core::Workspace; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevelProduct; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::get_key_value_span; use crate::diagnostics::rel_cwd_manifest_path; @@ -52,7 +52,7 @@ pub(crate) fn lint_workspace( maybe_pkg: &MaybePackage, manifest_path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let LintLevelProduct { diff --git a/src/cargo/diagnostics/rules/unused_workspace_package_fields.rs b/src/cargo/diagnostics/rules/unused_workspace_package_fields.rs index a924675075c..f85015ac94f 100644 --- a/src/cargo/diagnostics/rules/unused_workspace_package_fields.rs +++ b/src/cargo/diagnostics/rules/unused_workspace_package_fields.rs @@ -14,9 +14,9 @@ use crate::CargoResult; use crate::GlobalContext; use crate::core::MaybePackage; use crate::core::Workspace; -use crate::diagnostics::DiagnosticStats; use crate::diagnostics::Lint; use crate::diagnostics::LintLevelProduct; +use crate::diagnostics::ScopedDiagnosticStats; use crate::diagnostics::get_key_value_span; use crate::diagnostics::rel_cwd_manifest_path; @@ -52,7 +52,7 @@ pub(crate) fn lint_workspace( maybe_pkg: &MaybePackage, manifest_path: &Path, level: LintLevelProduct, - pkg_stats: &mut DiagnosticStats, + pkg_stats: &mut ScopedDiagnosticStats<'_>, gctx: &GlobalContext, ) -> CargoResult<()> { let LintLevelProduct { diff --git a/tests/testsuite/lints/mod.rs b/tests/testsuite/lints/mod.rs index ac31971c995..3f6ea250da7 100644 --- a/tests/testsuite/lints/mod.rs +++ b/tests/testsuite/lints/mod.rs @@ -385,6 +385,7 @@ authors = [] | ^^^^^^^^^^^^^^^^^^^ this is behind `test-dummy-unstable`, which is not enabled | = [HELP] consider adding `cargo-features = ["test-dummy-unstable"]` to the top of the manifest +[ERROR] could not parse workspace (manifest) due to 2 previous errors [WARNING] missing `[lints]` to inherit `[workspace.lints]` --> foo/Cargo.toml = [NOTE] `cargo::missing_lints_inheritance` is set to `warn` by default @@ -400,7 +401,6 @@ authors = [] 8 + [lints] | [WARNING] `foo` (manifest) generated 1 warning -[ERROR] could not parse workspace (manifest) due to 2 previous errors "#]]) .run();