From 1dfa101cd12238f750205e89f05543df572b29b3 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 19 May 2023 13:47:08 -0500 Subject: [PATCH] fix(lints): Tie into manifest warnings This improves the messaging experience (sometimes showing manifest path) and hiding the warning when not relevant). --- src/cargo/util/toml/mod.rs | 33 ++++++++++++++++++++++++--------- tests/testsuite/lints.rs | 2 +- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index dff6b7fe0..e67f3ca78 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -2045,7 +2045,12 @@ impl TomlManifest { let mut inheritable = toml_config.package.clone().unwrap_or_default(); inheritable.update_ws_path(package_root.to_path_buf()); inheritable.update_deps(toml_config.dependencies.clone()); - let lints = parse_unstable_lints(toml_config.lints.clone(), &features, config)?; + let lints = parse_unstable_lints( + toml_config.lints.clone(), + &features, + config, + &mut warnings, + )?; let lints = verify_lints(lints)?; inheritable.update_lints(lints); if let Some(ws_deps) = &inheritable.dependencies { @@ -2311,10 +2316,14 @@ impl TomlManifest { &inherit_cell, )?; - let lints = - parse_unstable_lints::(me.lints.clone(), &features, config)? - .map(|mw| mw.resolve("lints", || inherit()?.lints())) - .transpose()?; + let lints = parse_unstable_lints::( + me.lints.clone(), + &features, + config, + cx.warnings, + )? + .map(|mw| mw.resolve("lints", || inherit()?.lints())) + .transpose()?; let lints = verify_lints(lints)?; let default = TomlLints::default(); let mut rustflags = lints @@ -2774,7 +2783,12 @@ impl TomlManifest { let mut inheritable = toml_config.package.clone().unwrap_or_default(); inheritable.update_ws_path(root.to_path_buf()); inheritable.update_deps(toml_config.dependencies.clone()); - let lints = parse_unstable_lints(toml_config.lints.clone(), &features, config)?; + let lints = parse_unstable_lints( + toml_config.lints.clone(), + &features, + config, + &mut warnings, + )?; let lints = verify_lints(lints)?; inheritable.update_lints(lints); let ws_root_config = WorkspaceRootConfig::new( @@ -2925,18 +2939,19 @@ fn parse_unstable_lints>( lints: Option, features: &Features, config: &Config, + warnings: &mut Vec, ) -> CargoResult> { let Some(lints) = lints else { return Ok(None); }; if !features.is_enabled(Feature::lints()) { - warn_for_feature("lints", config); + warn_for_feature("lints", config, warnings); return Ok(None); } lints.try_into().map(Some).map_err(|err| err.into()) } -fn warn_for_feature(name: &str, config: &Config) { +fn warn_for_feature(name: &str, config: &Config, warnings: &mut Vec) { use std::fmt::Write as _; let mut message = String::new(); @@ -2962,7 +2977,7 @@ switch to nightly channel you can add `cargo-features = [\"{name}\"]` to enable this feature", ); } - let _ = config.shell().warn(&message); + warnings.push(message); } fn verify_lints(lints: Option) -> CargoResult> { diff --git a/tests/testsuite/lints.rs b/tests/testsuite/lints.rs index 3db8ac0fc..ac0df8f55 100644 --- a/tests/testsuite/lints.rs +++ b/tests/testsuite/lints.rs @@ -57,7 +57,7 @@ fn workspace_requires_option() { foo.cargo("check") .with_stderr( "\ -warning: feature `lints` is not supported on this version of Cargo and will be ignored +warning: [CWD]/Cargo.toml: feature `lints` is not supported on this version of Cargo and will be ignored this Cargo does not support nightly features, but if you switch to nightly channel you can add