From c3b104e11e28216c49d3e946ac0535bd2701f048 Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Fri, 26 Apr 2024 16:26:36 -0600 Subject: [PATCH 1/2] test(cargo-lints): Show workspace lints always inherited --- tests/testsuite/lints_table.rs | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/testsuite/lints_table.rs b/tests/testsuite/lints_table.rs index 75d0de4ee27..cccb6b55cea 100644 --- a/tests/testsuite/lints_table.rs +++ b/tests/testsuite/lints_table.rs @@ -982,3 +982,50 @@ error: `im_a_teapot` is specified ) .run(); } + +#[cargo_test] +fn dont_always_inherit_workspace_lints() { + let p = project() + .file( + "Cargo.toml", + r#" +[workspace] +members = ["foo"] + +[workspace.lints.cargo] +im-a-teapot = "warn" +"#, + ) + .file( + "foo/Cargo.toml", + r#" +cargo-features = ["test-dummy-unstable"] + +[package] +name = "foo" +version = "0.0.1" +edition = "2015" +authors = [] +im-a-teapot = true +"#, + ) + .file("foo/src/lib.rs", "") + .build(); + + p.cargo("check -Zcargo-lints") + .masquerade_as_nightly_cargo(&["cargo-lints"]) + .with_stderr( + "\ +warning: `im_a_teapot` is specified + --> foo/Cargo.toml:9:1 + | +9 | im-a-teapot = true + | ------------------ + | + = note: `cargo::im_a_teapot` is set to `warn` in `[workspace.lints]` +[CHECKING] foo v0.0.1 ([CWD]/foo) +[FINISHED] [..] +", + ) + .run(); +} From cf197fc499a434f62b7ec80c02d17cc8ea116e89 Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Fri, 26 Apr 2024 16:37:41 -0600 Subject: [PATCH 2/2] fix(cargo-lints): Don't always inherit workspace lints --- src/cargo/core/workspace.rs | 55 +++++++++++++++++++++------------- src/cargo/util/lints.rs | 12 ++++---- tests/testsuite/lints_table.rs | 7 ----- 3 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 2ef3a1a1a75..7fb0b1ca5c0 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -1147,11 +1147,26 @@ impl<'gctx> Workspace<'gctx> { } pub fn emit_warnings(&self) -> CargoResult<()> { + let ws_lints = self + .root_maybe() + .workspace_config() + .inheritable() + .and_then(|i| i.lints().ok()) + .unwrap_or_default(); + + let ws_cargo_lints = ws_lints + .get("cargo") + .cloned() + .unwrap_or_default() + .into_iter() + .map(|(k, v)| (k.replace('-', "_"), v)) + .collect(); + for (path, maybe_pkg) in &self.packages.packages { let path = path.join("Cargo.toml"); if let MaybePackage::Package(pkg) = maybe_pkg { if self.gctx.cli_unstable().cargo_lints { - self.emit_lints(pkg, &path)? + self.emit_lints(pkg, &path, &ws_cargo_lints)? } } let warnings = match maybe_pkg { @@ -1179,22 +1194,12 @@ impl<'gctx> Workspace<'gctx> { Ok(()) } - pub fn emit_lints(&self, pkg: &Package, path: &Path) -> CargoResult<()> { - let ws_lints = self - .root_maybe() - .workspace_config() - .inheritable() - .and_then(|i| i.lints().ok()) - .unwrap_or_default(); - - let ws_cargo_lints = ws_lints - .get("cargo") - .cloned() - .unwrap_or_default() - .into_iter() - .map(|(k, v)| (k.replace('-', "_"), v)) - .collect(); - + pub fn emit_lints( + &self, + pkg: &Package, + path: &Path, + ws_cargo_lints: &manifest::TomlToolLints, + ) -> CargoResult<()> { let mut error_count = 0; let toml_lints = pkg .manifest() @@ -1212,11 +1217,21 @@ impl<'gctx> Workspace<'gctx> { .map(|(name, lint)| (name.replace('-', "_"), lint)) .collect(); + // We should only be using workspace lints if the `[lints]` table is + // present in the manifest, and `workspace` is set to `true` + let ws_cargo_lints = pkg + .manifest() + .resolved_toml() + .lints + .as_ref() + .is_some_and(|l| l.workspace) + .then(|| ws_cargo_lints); + check_im_a_teapot( pkg, &path, &normalized_lints, - &ws_cargo_lints, + ws_cargo_lints, &mut error_count, self.gctx, )?; @@ -1224,7 +1239,7 @@ impl<'gctx> Workspace<'gctx> { pkg, &path, &normalized_lints, - &ws_cargo_lints, + ws_cargo_lints, &mut error_count, self.gctx, )?; @@ -1232,7 +1247,7 @@ impl<'gctx> Workspace<'gctx> { pkg, &path, &normalized_lints, - &ws_cargo_lints, + ws_cargo_lints, &mut error_count, self.gctx, )?; diff --git a/src/cargo/util/lints.rs b/src/cargo/util/lints.rs index f177dac48e4..fd462166aa1 100644 --- a/src/cargo/util/lints.rs +++ b/src/cargo/util/lints.rs @@ -88,7 +88,7 @@ impl Lint { pub fn level( &self, pkg_lints: &TomlToolLints, - ws_lints: &TomlToolLints, + ws_lints: Option<&TomlToolLints>, edition: Edition, ) -> (LintLevel, LintLevelReason) { self.groups @@ -188,7 +188,7 @@ fn level_priority( default_level: LintLevel, edition_lint_opts: Option<(Edition, LintLevel)>, pkg_lints: &TomlToolLints, - ws_lints: &TomlToolLints, + ws_lints: Option<&TomlToolLints>, edition: Edition, ) -> (LintLevel, LintLevelReason, i8) { let (unspecified_level, reason) = if let Some(level) = edition_lint_opts @@ -211,7 +211,7 @@ fn level_priority( LintLevelReason::Package, defined_level.priority(), ) - } else if let Some(defined_level) = ws_lints.get(name) { + } else if let Some(defined_level) = ws_lints.and_then(|l| l.get(name)) { ( defined_level.level().into(), LintLevelReason::Workspace, @@ -234,7 +234,7 @@ pub fn check_im_a_teapot( pkg: &Package, path: &Path, pkg_lints: &TomlToolLints, - ws_lints: &TomlToolLints, + ws_lints: Option<&TomlToolLints>, error_count: &mut usize, gctx: &GlobalContext, ) -> CargoResult<()> { @@ -306,7 +306,7 @@ pub fn check_implicit_features( pkg: &Package, path: &Path, pkg_lints: &TomlToolLints, - ws_lints: &TomlToolLints, + ws_lints: Option<&TomlToolLints>, error_count: &mut usize, gctx: &GlobalContext, ) -> CargoResult<()> { @@ -390,7 +390,7 @@ pub fn unused_dependencies( pkg: &Package, path: &Path, pkg_lints: &TomlToolLints, - ws_lints: &TomlToolLints, + ws_lints: Option<&TomlToolLints>, error_count: &mut usize, gctx: &GlobalContext, ) -> CargoResult<()> { diff --git a/tests/testsuite/lints_table.rs b/tests/testsuite/lints_table.rs index cccb6b55cea..246c8923b31 100644 --- a/tests/testsuite/lints_table.rs +++ b/tests/testsuite/lints_table.rs @@ -1016,13 +1016,6 @@ im-a-teapot = true .masquerade_as_nightly_cargo(&["cargo-lints"]) .with_stderr( "\ -warning: `im_a_teapot` is specified - --> foo/Cargo.toml:9:1 - | -9 | im-a-teapot = true - | ------------------ - | - = note: `cargo::im_a_teapot` is set to `warn` in `[workspace.lints]` [CHECKING] foo v0.0.1 ([CWD]/foo) [FINISHED] [..] ",