Skip to content

Commit cf197fc

Browse files
committed
fix(cargo-lints): Don't always inherit workspace lints
1 parent c3b104e commit cf197fc

File tree

3 files changed

+41
-33
lines changed

3 files changed

+41
-33
lines changed

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
)?;

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<()> {

tests/testsuite/lints_table.rs

-7
Original file line numberDiff line numberDiff line change
@@ -1016,13 +1016,6 @@ im-a-teapot = true
10161016
.masquerade_as_nightly_cargo(&["cargo-lints"])
10171017
.with_stderr(
10181018
"\
1019-
warning: `im_a_teapot` is specified
1020-
--> foo/Cargo.toml:9:1
1021-
|
1022-
9 | im-a-teapot = true
1023-
| ------------------
1024-
|
1025-
= note: `cargo::im_a_teapot` is set to `warn` in `[workspace.lints]`
10261019
[CHECKING] foo v0.0.1 ([CWD]/foo)
10271020
[FINISHED] [..]
10281021
",

0 commit comments

Comments
 (0)