Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: lints feature #12148

Merged
merged 19 commits into from
May 22, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(lints): Expose TomlLints as Manifest::rustflags
epage committed May 22, 2023
commit 25c084a3447dad66705c77d11d840676bd666e15
8 changes: 8 additions & 0 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
@@ -63,6 +63,7 @@ pub struct Manifest {
default_run: Option<String>,
metabuild: Option<Vec<String>>,
resolve_behavior: Option<ResolveBehavior>,
rustflags: Vec<String>,
}

/// When parsing `Cargo.toml`, some warnings should silenced
@@ -405,6 +406,7 @@ impl Manifest {
original: Rc<TomlManifest>,
metabuild: Option<Vec<String>>,
resolve_behavior: Option<ResolveBehavior>,
rustflags: Vec<String>,
) -> Manifest {
Manifest {
summary,
@@ -430,6 +432,7 @@ impl Manifest {
default_run,
metabuild,
resolve_behavior,
rustflags,
}
}

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

/// Package-wide RUSTFLAGS
pub fn rustflags(&self) -> &[String] {
self.rustflags.as_slice()
}

pub fn map_source(self, to_replace: SourceId, replace_with: SourceId) -> Manifest {
Manifest {
summary: self.summary.map_source(to_replace, replace_with),
56 changes: 56 additions & 0 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
@@ -2316,6 +2316,34 @@ impl TomlManifest {
.map(|mw| mw.resolve("lints", || inherit()?.lints()))
.transpose()?;
verify_lints(lints.as_ref(), &features)?;
let default = TomlLints::default();
let mut rustflags = lints
.as_ref()
.unwrap_or(&default)
.iter()
.flat_map(|(tool, lints)| {
lints.iter().map(move |(name, config)| {
let flag = config.level().flag();
let option = if tool == "rust" {
format!("{flag}={name}")
} else {
format!("{flag}={tool}::{name}")
};
(
config.priority(),
// Since the most common group will be `all`, put it last so people are more
// likely to notice that they need to use `priority`.
std::cmp::Reverse(name),
option,
)
})
})
.collect::<Vec<_>>();
rustflags.sort();
let rustflags = rustflags
.into_iter()
.map(|(_, _, option)| option)
.collect::<Vec<_>>();

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

impl TomlLint {
fn level(&self) -> TomlLintLevel {
match self {
Self::Level(level) => *level,
Self::Config(config) => config.level,
}
}

fn priority(&self) -> i8 {
match self {
Self::Level(_) => 0,
Self::Config(config) => config.priority,
}
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct TomlLintConfig {
@@ -3506,3 +3551,14 @@ pub enum TomlLintLevel {
Warn,
Allow,
}

impl TomlLintLevel {
fn flag(&self) -> &'static str {
match self {
Self::Forbid => "--forbid",
Self::Deny => "--deny",
Self::Warn => "--warn",
Self::Allow => "--allow",
}
}
}