Skip to content

Commit 3b924c8

Browse files
committed
feat: Expose TomlLints as Manifest::rustflags
1 parent 133da05 commit 3b924c8

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/cargo/core/manifest.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub struct Manifest {
6363
default_run: Option<String>,
6464
metabuild: Option<Vec<String>>,
6565
resolve_behavior: Option<ResolveBehavior>,
66+
rustflags: Vec<String>,
6667
}
6768

6869
/// When parsing `Cargo.toml`, some warnings should silenced
@@ -405,6 +406,7 @@ impl Manifest {
405406
original: Rc<TomlManifest>,
406407
metabuild: Option<Vec<String>>,
407408
resolve_behavior: Option<ResolveBehavior>,
409+
rustflags: Vec<String>,
408410
) -> Manifest {
409411
Manifest {
410412
summary,
@@ -430,6 +432,7 @@ impl Manifest {
430432
default_run,
431433
metabuild,
432434
resolve_behavior,
435+
rustflags,
433436
}
434437
}
435438

@@ -514,6 +517,11 @@ impl Manifest {
514517
self.resolve_behavior
515518
}
516519

520+
/// Package-wide RUSTFLAGS
521+
pub fn rustflags(&self) -> &[String] {
522+
self.rustflags.as_slice()
523+
}
524+
517525
pub fn map_source(self, to_replace: SourceId, replace_with: SourceId) -> Manifest {
518526
Manifest {
519527
summary: self.summary.map_source(to_replace, replace_with),

src/cargo/util/toml/mod.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,6 +2316,34 @@ impl TomlManifest {
23162316
.map(|mw| mw.resolve("lints", || inherit()?.lints()))
23172317
.transpose()?;
23182318
verify_lints(lints.as_ref(), &features)?;
2319+
let default = TomlLints::default();
2320+
let mut rustflags = lints
2321+
.as_ref()
2322+
.unwrap_or(&default)
2323+
.iter()
2324+
.flat_map(|(tool, lints)| {
2325+
lints.iter().map(move |(name, config)| {
2326+
let flag = config.level().flag();
2327+
let option = if tool == "rust" {
2328+
format!("{flag}={name}")
2329+
} else {
2330+
format!("{flag}={tool}::{name}")
2331+
};
2332+
(
2333+
config.priority(),
2334+
// Since the most common group will be `all`, put it last so people are more
2335+
// likely to notice that they need to use `priority`.
2336+
std::cmp::Reverse(name),
2337+
option,
2338+
)
2339+
})
2340+
})
2341+
.collect::<Vec<_>>();
2342+
rustflags.sort();
2343+
let rustflags = rustflags
2344+
.into_iter()
2345+
.map(|(_, _, option)| option)
2346+
.collect::<Vec<_>>();
23192347

23202348
let mut target: BTreeMap<String, TomlPlatform> = BTreeMap::new();
23212349
for (name, platform) in me.target.iter().flatten() {
@@ -2639,6 +2667,7 @@ impl TomlManifest {
26392667
Rc::new(resolved_toml),
26402668
package.metabuild.clone().map(|sov| sov.0),
26412669
resolve_behavior,
2670+
rustflags,
26422671
);
26432672
if package.license_file.is_some() && package.license.is_some() {
26442673
manifest.warnings_mut().add_warning(
@@ -3490,6 +3519,22 @@ pub enum TomlLint {
34903519
Config(TomlLintConfig),
34913520
}
34923521

3522+
impl TomlLint {
3523+
fn level(&self) -> TomlLintLevel {
3524+
match self {
3525+
Self::Level(level) => *level,
3526+
Self::Config(config) => config.level,
3527+
}
3528+
}
3529+
3530+
fn priority(&self) -> i8 {
3531+
match self {
3532+
Self::Level(_) => 0,
3533+
Self::Config(config) => config.priority,
3534+
}
3535+
}
3536+
}
3537+
34933538
#[derive(Serialize, Deserialize, Debug, Clone)]
34943539
#[serde(rename_all = "kebab-case")]
34953540
pub struct TomlLintConfig {
@@ -3506,3 +3551,14 @@ pub enum TomlLintLevel {
35063551
Warn,
35073552
Allow,
35083553
}
3554+
3555+
impl TomlLintLevel {
3556+
fn flag(&self) -> &'static str {
3557+
match self {
3558+
Self::Forbid => "--forbid",
3559+
Self::Deny => "--deny",
3560+
Self::Warn => "--warn",
3561+
Self::Allow => "--allow",
3562+
}
3563+
}
3564+
}

0 commit comments

Comments
 (0)