Skip to content

Commit d5bc35d

Browse files
committed
refactor(lints): Keep workspace and package lints separate
1 parent 2655b06 commit d5bc35d

File tree

3 files changed

+83
-36
lines changed

3 files changed

+83
-36
lines changed

src/cargo/core/workspace.rs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,21 @@ impl<'gctx> Workspace<'gctx> {
11801180
}
11811181

11821182
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+
11831198
let mut error_count = 0;
11841199
let toml_lints = pkg
11851200
.manifest()
@@ -1197,9 +1212,30 @@ impl<'gctx> Workspace<'gctx> {
11971212
.map(|(name, lint)| (name.replace('-', "_"), lint))
11981213
.collect();
11991214

1200-
check_im_a_teapot(pkg, &path, &normalized_lints, &mut error_count, self.gctx)?;
1201-
check_implicit_features(pkg, &path, &normalized_lints, &mut error_count, self.gctx)?;
1202-
unused_dependencies(pkg, &path, &normalized_lints, &mut error_count, self.gctx)?;
1215+
check_im_a_teapot(
1216+
pkg,
1217+
&path,
1218+
&normalized_lints,
1219+
&ws_cargo_lints,
1220+
&mut error_count,
1221+
self.gctx,
1222+
)?;
1223+
check_implicit_features(
1224+
pkg,
1225+
&path,
1226+
&normalized_lints,
1227+
&ws_cargo_lints,
1228+
&mut error_count,
1229+
self.gctx,
1230+
)?;
1231+
unused_dependencies(
1232+
pkg,
1233+
&path,
1234+
&normalized_lints,
1235+
&ws_cargo_lints,
1236+
&mut error_count,
1237+
self.gctx,
1238+
)?;
12031239
if error_count > 0 {
12041240
Err(crate::util::errors::AlreadyPrintedError::new(anyhow!(
12051241
"encountered {error_count} errors(s) while running lints"

src/cargo/util/lints.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,25 @@ pub struct Lint {
8585
}
8686

8787
impl Lint {
88-
pub fn level(&self, lints: &TomlToolLints, edition: Edition) -> LintLevel {
88+
pub fn level(
89+
&self,
90+
pkg_lints: &TomlToolLints,
91+
ws_lints: &TomlToolLints,
92+
edition: Edition,
93+
) -> LintLevel {
8994
self.groups
9095
.iter()
9196
.map(|g| {
9297
(
9398
g.name,
94-
level_priority(g.name, g.default_level, g.edition_lint_opts, lints, edition),
99+
level_priority(
100+
g.name,
101+
g.default_level,
102+
g.edition_lint_opts,
103+
pkg_lints,
104+
ws_lints,
105+
edition,
106+
),
95107
)
96108
})
97109
.chain(std::iter::once((
@@ -100,7 +112,8 @@ impl Lint {
100112
self.name,
101113
self.default_level,
102114
self.edition_lint_opts,
103-
lints,
115+
pkg_lints,
116+
ws_lints,
104117
edition,
105118
),
106119
)))
@@ -155,7 +168,8 @@ fn level_priority(
155168
name: &str,
156169
default_level: LintLevel,
157170
edition_lint_opts: Option<(Edition, LintLevel)>,
158-
lints: &TomlToolLints,
171+
pkg_lints: &TomlToolLints,
172+
ws_lints: &TomlToolLints,
159173
edition: Edition,
160174
) -> (LintLevel, i8) {
161175
let unspecified_level = if let Some(level) = edition_lint_opts
@@ -172,7 +186,9 @@ fn level_priority(
172186
return (unspecified_level, 0);
173187
}
174188

175-
if let Some(defined_level) = lints.get(name) {
189+
if let Some(defined_level) = pkg_lints.get(name) {
190+
(defined_level.level().into(), defined_level.priority())
191+
} else if let Some(defined_level) = ws_lints.get(name) {
176192
(defined_level.level().into(), defined_level.priority())
177193
} else {
178194
(unspecified_level, 0)
@@ -190,12 +206,13 @@ const IM_A_TEAPOT: Lint = Lint {
190206
pub fn check_im_a_teapot(
191207
pkg: &Package,
192208
path: &Path,
193-
lints: &TomlToolLints,
209+
pkg_lints: &TomlToolLints,
210+
ws_lints: &TomlToolLints,
194211
error_count: &mut usize,
195212
gctx: &GlobalContext,
196213
) -> CargoResult<()> {
197214
let manifest = pkg.manifest();
198-
let lint_level = IM_A_TEAPOT.level(lints, manifest.edition());
215+
let lint_level = IM_A_TEAPOT.level(pkg_lints, ws_lints, manifest.edition());
199216
if lint_level == LintLevel::Allow {
200217
return Ok(());
201218
}
@@ -258,7 +275,8 @@ const IMPLICIT_FEATURES: Lint = Lint {
258275
pub fn check_implicit_features(
259276
pkg: &Package,
260277
path: &Path,
261-
lints: &TomlToolLints,
278+
pkg_lints: &TomlToolLints,
279+
ws_lints: &TomlToolLints,
262280
error_count: &mut usize,
263281
gctx: &GlobalContext,
264282
) -> CargoResult<()> {
@@ -269,7 +287,7 @@ pub fn check_implicit_features(
269287
return Ok(());
270288
}
271289

272-
let lint_level = IMPLICIT_FEATURES.level(lints, edition);
290+
let lint_level = IMPLICIT_FEATURES.level(pkg_lints, ws_lints, edition);
273291
if lint_level == LintLevel::Allow {
274292
return Ok(());
275293
}
@@ -341,7 +359,8 @@ const UNUSED_OPTIONAL_DEPENDENCY: Lint = Lint {
341359
pub fn unused_dependencies(
342360
pkg: &Package,
343361
path: &Path,
344-
lints: &TomlToolLints,
362+
pkg_lints: &TomlToolLints,
363+
ws_lints: &TomlToolLints,
345364
error_count: &mut usize,
346365
gctx: &GlobalContext,
347366
) -> CargoResult<()> {
@@ -351,7 +370,7 @@ pub fn unused_dependencies(
351370
return Ok(());
352371
}
353372

354-
let lint_level = UNUSED_OPTIONAL_DEPENDENCY.level(lints, edition);
373+
let lint_level = UNUSED_OPTIONAL_DEPENDENCY.level(pkg_lints, ws_lints, edition);
355374
if lint_level == LintLevel::Allow {
356375
return Ok(());
357376
}

src/cargo/util/toml/mod.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,7 @@ fn resolve_toml(
411411
}
412412
resolved_toml.target = (!resolved_target.is_empty()).then_some(resolved_target);
413413

414-
let resolved_lints = original_toml
415-
.lints
416-
.clone()
417-
.map(|value| lints_inherit_with(value, || inherit()?.lints()))
418-
.transpose()?;
419-
resolved_toml.lints = resolved_lints.map(|lints| manifest::InheritableLints {
420-
workspace: false,
421-
lints,
422-
});
414+
resolved_toml.lints = original_toml.lints.clone();
423415

424416
let resolved_badges = original_toml
425417
.badges
@@ -803,7 +795,7 @@ impl InheritableFields {
803795
}
804796

805797
/// Gets the field `workspace.lint`.
806-
fn lints(&self) -> CargoResult<manifest::TomlLints> {
798+
pub fn lints(&self) -> CargoResult<manifest::TomlLints> {
807799
let Some(val) = &self.lints else {
808800
bail!("`workspace.lints` was not defined");
809801
};
@@ -1284,18 +1276,18 @@ fn to_real_manifest(
12841276
}
12851277
}
12861278

1287-
verify_lints(
1288-
resolved_toml.resolved_lints().expect("previously resolved"),
1289-
gctx,
1290-
warnings,
1291-
)?;
1292-
let default = manifest::TomlLints::default();
1293-
let rustflags = lints_to_rustflags(
1294-
resolved_toml
1295-
.resolved_lints()
1296-
.expect("previously resolved")
1297-
.unwrap_or(&default),
1298-
);
1279+
let resolved_lints = resolved_toml
1280+
.lints
1281+
.clone()
1282+
.map(|value| {
1283+
lints_inherit_with(value, || {
1284+
load_inheritable_fields(gctx, manifest_file, &workspace_config)?.lints()
1285+
})
1286+
})
1287+
.transpose()?;
1288+
1289+
verify_lints(resolved_lints.as_ref(), gctx, warnings)?;
1290+
let rustflags = lints_to_rustflags(&resolved_lints.unwrap_or_default());
12991291

13001292
let metadata = ManifestMetadata {
13011293
description: resolved_package

0 commit comments

Comments
 (0)