Skip to content

Commit eee4ea2

Browse files
committed
Auto merge of #13812 - Muscraft:dont-always-inherit-workspace-lints, r=epage
fix(cargo-lints): Don't always inherit workspace lints When working on changes for #13805, I noticed that we always passed the contents of `[workspace.lints.cargo]` into the currently implemented lints, even if `[lints]` was not specified or did not contain `workspace = true`. This PR makes it so we only pass in the workspace cargo lints if `[lints]` contains `workspace = true`. You can verify this change by looking at the first commit, where I added a test showing the current behavior, and looking at the second commit and seeing the test output no longer shows a warning about specifying `im-a-teapot`.
2 parents c4e19cc + cf197fc commit eee4ea2

File tree

3 files changed

+81
-26
lines changed

3 files changed

+81
-26
lines changed

Diff for: src/cargo/core/workspace.rs

+35-20
Original file line numberDiff line numberDiff line change
@@ -1147,11 +1147,26 @@ impl<'gctx> Workspace<'gctx> {
11471147
}
11481148

11491149
pub fn emit_warnings(&self) -> CargoResult<()> {
1150+
let ws_lints = self
1151+
.root_maybe()
1152+
.workspace_config()
1153+
.inheritable()
1154+
.and_then(|i| i.lints().ok())
1155+
.unwrap_or_default();
1156+
1157+
let ws_cargo_lints = ws_lints
1158+
.get("cargo")
1159+
.cloned()
1160+
.unwrap_or_default()
1161+
.into_iter()
1162+
.map(|(k, v)| (k.replace('-', "_"), v))
1163+
.collect();
1164+
11501165
for (path, maybe_pkg) in &self.packages.packages {
11511166
let path = path.join("Cargo.toml");
11521167
if let MaybePackage::Package(pkg) = maybe_pkg {
11531168
if self.gctx.cli_unstable().cargo_lints {
1154-
self.emit_lints(pkg, &path)?
1169+
self.emit_lints(pkg, &path, &ws_cargo_lints)?
11551170
}
11561171
}
11571172
let warnings = match maybe_pkg {
@@ -1179,22 +1194,12 @@ impl<'gctx> Workspace<'gctx> {
11791194
Ok(())
11801195
}
11811196

1182-
pub fn emit_lints(&self, pkg: &Package, path: &Path) -> CargoResult<()> {
1183-
let ws_lints = self
1184-
.root_maybe()
1185-
.workspace_config()
1186-
.inheritable()
1187-
.and_then(|i| i.lints().ok())
1188-
.unwrap_or_default();
1189-
1190-
let ws_cargo_lints = ws_lints
1191-
.get("cargo")
1192-
.cloned()
1193-
.unwrap_or_default()
1194-
.into_iter()
1195-
.map(|(k, v)| (k.replace('-', "_"), v))
1196-
.collect();
1197-
1197+
pub fn emit_lints(
1198+
&self,
1199+
pkg: &Package,
1200+
path: &Path,
1201+
ws_cargo_lints: &manifest::TomlToolLints,
1202+
) -> CargoResult<()> {
11981203
let mut error_count = 0;
11991204
let toml_lints = pkg
12001205
.manifest()
@@ -1212,27 +1217,37 @@ impl<'gctx> Workspace<'gctx> {
12121217
.map(|(name, lint)| (name.replace('-', "_"), lint))
12131218
.collect();
12141219

1220+
// We should only be using workspace lints if the `[lints]` table is
1221+
// present in the manifest, and `workspace` is set to `true`
1222+
let ws_cargo_lints = pkg
1223+
.manifest()
1224+
.resolved_toml()
1225+
.lints
1226+
.as_ref()
1227+
.is_some_and(|l| l.workspace)
1228+
.then(|| ws_cargo_lints);
1229+
12151230
check_im_a_teapot(
12161231
pkg,
12171232
&path,
12181233
&normalized_lints,
1219-
&ws_cargo_lints,
1234+
ws_cargo_lints,
12201235
&mut error_count,
12211236
self.gctx,
12221237
)?;
12231238
check_implicit_features(
12241239
pkg,
12251240
&path,
12261241
&normalized_lints,
1227-
&ws_cargo_lints,
1242+
ws_cargo_lints,
12281243
&mut error_count,
12291244
self.gctx,
12301245
)?;
12311246
unused_dependencies(
12321247
pkg,
12331248
&path,
12341249
&normalized_lints,
1235-
&ws_cargo_lints,
1250+
ws_cargo_lints,
12361251
&mut error_count,
12371252
self.gctx,
12381253
)?;

Diff for: src/cargo/util/lints.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl Lint {
8888
pub fn level(
8989
&self,
9090
pkg_lints: &TomlToolLints,
91-
ws_lints: &TomlToolLints,
91+
ws_lints: Option<&TomlToolLints>,
9292
edition: Edition,
9393
) -> (LintLevel, LintLevelReason) {
9494
self.groups
@@ -188,7 +188,7 @@ fn level_priority(
188188
default_level: LintLevel,
189189
edition_lint_opts: Option<(Edition, LintLevel)>,
190190
pkg_lints: &TomlToolLints,
191-
ws_lints: &TomlToolLints,
191+
ws_lints: Option<&TomlToolLints>,
192192
edition: Edition,
193193
) -> (LintLevel, LintLevelReason, i8) {
194194
let (unspecified_level, reason) = if let Some(level) = edition_lint_opts
@@ -211,7 +211,7 @@ fn level_priority(
211211
LintLevelReason::Package,
212212
defined_level.priority(),
213213
)
214-
} else if let Some(defined_level) = ws_lints.get(name) {
214+
} else if let Some(defined_level) = ws_lints.and_then(|l| l.get(name)) {
215215
(
216216
defined_level.level().into(),
217217
LintLevelReason::Workspace,
@@ -234,7 +234,7 @@ pub fn check_im_a_teapot(
234234
pkg: &Package,
235235
path: &Path,
236236
pkg_lints: &TomlToolLints,
237-
ws_lints: &TomlToolLints,
237+
ws_lints: Option<&TomlToolLints>,
238238
error_count: &mut usize,
239239
gctx: &GlobalContext,
240240
) -> CargoResult<()> {
@@ -306,7 +306,7 @@ pub fn check_implicit_features(
306306
pkg: &Package,
307307
path: &Path,
308308
pkg_lints: &TomlToolLints,
309-
ws_lints: &TomlToolLints,
309+
ws_lints: Option<&TomlToolLints>,
310310
error_count: &mut usize,
311311
gctx: &GlobalContext,
312312
) -> CargoResult<()> {
@@ -390,7 +390,7 @@ pub fn unused_dependencies(
390390
pkg: &Package,
391391
path: &Path,
392392
pkg_lints: &TomlToolLints,
393-
ws_lints: &TomlToolLints,
393+
ws_lints: Option<&TomlToolLints>,
394394
error_count: &mut usize,
395395
gctx: &GlobalContext,
396396
) -> CargoResult<()> {

Diff for: tests/testsuite/lints_table.rs

+40
Original file line numberDiff line numberDiff line change
@@ -982,3 +982,43 @@ error: `im_a_teapot` is specified
982982
)
983983
.run();
984984
}
985+
986+
#[cargo_test]
987+
fn dont_always_inherit_workspace_lints() {
988+
let p = project()
989+
.file(
990+
"Cargo.toml",
991+
r#"
992+
[workspace]
993+
members = ["foo"]
994+
995+
[workspace.lints.cargo]
996+
im-a-teapot = "warn"
997+
"#,
998+
)
999+
.file(
1000+
"foo/Cargo.toml",
1001+
r#"
1002+
cargo-features = ["test-dummy-unstable"]
1003+
1004+
[package]
1005+
name = "foo"
1006+
version = "0.0.1"
1007+
edition = "2015"
1008+
authors = []
1009+
im-a-teapot = true
1010+
"#,
1011+
)
1012+
.file("foo/src/lib.rs", "")
1013+
.build();
1014+
1015+
p.cargo("check -Zcargo-lints")
1016+
.masquerade_as_nightly_cargo(&["cargo-lints"])
1017+
.with_stderr(
1018+
"\
1019+
[CHECKING] foo v0.0.1 ([CWD]/foo)
1020+
[FINISHED] [..]
1021+
",
1022+
)
1023+
.run();
1024+
}

0 commit comments

Comments
 (0)