Skip to content

Commit 1464f97

Browse files
committed
refactor(lint): wrapper for lints against both pkg and workspace
1 parent 192475a commit 1464f97

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

src/cargo/core/workspace.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,13 @@ impl<'gctx> Workspace<'gctx> {
12971297
self.gctx,
12981298
)?;
12991299
check_im_a_teapot(pkg, &path, &cargo_lints, &mut error_count, self.gctx)?;
1300-
implicit_minimum_version_req(pkg, &path, &cargo_lints, &mut error_count, self.gctx)?;
1300+
implicit_minimum_version_req(
1301+
pkg.into(),
1302+
&path,
1303+
&cargo_lints,
1304+
&mut error_count,
1305+
self.gctx,
1306+
)?;
13011307
}
13021308

13031309
if error_count > 0 {

src/cargo/util/lints/implicit_minimum_version_req.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::util::OptVersionReq;
1818
use crate::util::lints::Lint;
1919
use crate::util::lints::LintLevel;
2020
use crate::util::lints::LintLevelReason;
21+
use crate::util::lints::ManifestFor;
2122
use crate::util::lints::get_key_value;
2223
use crate::util::lints::rel_cwd_manifest_path;
2324

@@ -79,26 +80,39 @@ serde = "1.0.219"
7980
};
8081

8182
pub fn implicit_minimum_version_req(
82-
pkg: &Package,
83+
manifest: ManifestFor<'_>,
8384
manifest_path: &Path,
8485
cargo_lints: &TomlToolLints,
8586
error_count: &mut usize,
8687
gctx: &GlobalContext,
8788
) -> CargoResult<()> {
88-
let manifest = pkg.manifest();
89-
let (lint_level, reason) = LINT.level(
90-
cargo_lints,
91-
manifest.edition(),
92-
manifest.unstable_features(),
93-
);
89+
let (lint_level, reason) = manifest.lint_level(cargo_lints, LINT);
9490

9591
if lint_level == LintLevel::Allow {
9692
return Ok(());
9793
}
9894

95+
let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
96+
97+
match manifest {
98+
ManifestFor::Package(pkg) => {
99+
lint_package(pkg, manifest_path, lint_level, reason, error_count, gctx)
100+
}
101+
}
102+
}
103+
104+
pub fn lint_package(
105+
pkg: &Package,
106+
manifest_path: String,
107+
lint_level: LintLevel,
108+
reason: LintLevelReason,
109+
error_count: &mut usize,
110+
gctx: &GlobalContext,
111+
) -> CargoResult<()> {
112+
let manifest = pkg.manifest();
113+
99114
let document = manifest.document();
100115
let contents = manifest.contents();
101-
let manifest_path = rel_cwd_manifest_path(manifest_path, gctx);
102116
let target_key_for_platform = target_key_for_platform(&manifest);
103117

104118
for dep in manifest.dependencies().iter() {

src/cargo/util/lints/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,30 @@ pub const LINTS: &[Lint] = &[
1919
UNKNOWN_LINTS,
2020
];
2121

22+
/// Scope at which a lint runs: package-level or workspace-level.
23+
pub enum ManifestFor<'a> {
24+
/// Lint runs for a specific package.
25+
Package(&'a Package),
26+
}
27+
28+
impl ManifestFor<'_> {
29+
fn lint_level(&self, pkg_lints: &TomlToolLints, lint: Lint) -> (LintLevel, LintLevelReason) {
30+
match self {
31+
ManifestFor::Package(p) => lint.level(
32+
pkg_lints,
33+
p.manifest().edition(),
34+
p.manifest().unstable_features(),
35+
),
36+
}
37+
}
38+
}
39+
40+
impl<'a> From<&'a Package> for ManifestFor<'a> {
41+
fn from(value: &'a Package) -> ManifestFor<'a> {
42+
ManifestFor::Package(value)
43+
}
44+
}
45+
2246
pub fn analyze_cargo_lints_table(
2347
pkg: &Package,
2448
path: &Path,

0 commit comments

Comments
 (0)