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

Add ./x clippy ci #126321

Closed
wants to merge 2 commits into from
Closed
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
Next Next commit
store the lint levels in the clippy structs themselves
  • Loading branch information
pietroalbini committed Jun 12, 2024
commit a6402af7528417295384bd36ec629287bdf26fb9
60 changes: 40 additions & 20 deletions src/bootstrap/src/core/build_steps/clippy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ const IGNORED_RULES_FOR_STD_AND_RUSTC: &[&str] = &[
"wrong_self_convention",
];

fn lint_args(builder: &Builder<'_>, ignored_rules: &[&str]) -> Vec<String> {
fn lint_args(builder: &Builder<'_>, config: &LintConfig, ignored_rules: &[&str]) -> Vec<String> {
fn strings<'a>(arr: &'a [&str]) -> impl Iterator<Item = String> + 'a {
arr.iter().copied().map(String::from)
}

let Subcommand::Clippy { fix, allow_dirty, allow_staged, allow, deny, warn, forbid } =
&builder.config.cmd
else {
let Subcommand::Clippy { fix, allow_dirty, allow_staged, .. } = &builder.config.cmd else {
unreachable!("clippy::lint_args can only be called from `clippy` subcommands.");
};

Expand All @@ -68,12 +66,12 @@ fn lint_args(builder: &Builder<'_>, ignored_rules: &[&str]) -> Vec<String> {

args.extend(strings(&["--"]));

if deny.is_empty() && forbid.is_empty() {
if config.deny.is_empty() && config.forbid.is_empty() {
args.extend(strings(&["--cap-lints", "warn"]));
}

let all_args = std::env::args().collect::<Vec<_>>();
args.extend(get_clippy_rules_in_order(&all_args, allow, deny, warn, forbid));
args.extend(get_clippy_rules_in_order(&all_args, config));

args.extend(ignored_rules.iter().map(|lint| format!("-Aclippy::{}", lint)));
args.extend(builder.config.free_args.clone());
Expand All @@ -83,21 +81,17 @@ fn lint_args(builder: &Builder<'_>, ignored_rules: &[&str]) -> Vec<String> {
/// We need to keep the order of the given clippy lint rules before passing them.
/// Since clap doesn't offer any useful interface for this purpose out of the box,
/// we have to handle it manually.
pub(crate) fn get_clippy_rules_in_order(
all_args: &[String],
allow_rules: &[String],
deny_rules: &[String],
warn_rules: &[String],
forbid_rules: &[String],
) -> Vec<String> {
fn get_clippy_rules_in_order(all_args: &[String], config: &LintConfig) -> Vec<String> {
let mut result = vec![];

for (prefix, item) in
[("-A", allow_rules), ("-D", deny_rules), ("-W", warn_rules), ("-F", forbid_rules)]
[("-A", &config.allow), ("-D", &config.deny), ("-W", &config.warn), ("-F", &config.forbid)]
{
item.iter().for_each(|v| {
let rule = format!("{prefix}{v}");
let position = all_args.iter().position(|t| t == &rule).unwrap();
// Arguments added by bootstrap in LintConfig won't show up in the all_args list, so
// put them at the end of the command line.
let position = all_args.iter().position(|t| t == &rule).unwrap_or(usize::MAX);
result.push((position, rule));
});
}
Expand All @@ -106,9 +100,29 @@ pub(crate) fn get_clippy_rules_in_order(
result.into_iter().map(|v| v.1).collect()
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct LintConfig {
allow: Vec<String>,
warn: Vec<String>,
deny: Vec<String>,
forbid: Vec<String>,
}

impl LintConfig {
fn new(builder: &Builder<'_>) -> Self {
match builder.config.cmd.clone() {
Subcommand::Clippy { allow, deny, warn, forbid, .. } => {
Self { allow, warn, deny, forbid }
}
_ => unreachable!("LintConfig can only be called from `clippy` subcommands."),
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Std {
pub target: TargetSelection,
config: LintConfig,
/// Whether to lint only a subset of crates.
crates: Vec<String>,
}
Expand All @@ -123,7 +137,8 @@ impl Step for Std {

fn make_run(run: RunConfig<'_>) {
let crates = run.make_run_crates(Alias::Library);
run.builder.ensure(Std { target: run.target, crates });
let config = LintConfig::new(run.builder);
run.builder.ensure(Std { target: run.target, config, crates });
}

fn run(self, builder: &Builder<'_>) {
Expand All @@ -147,7 +162,7 @@ impl Step for Std {
run_cargo(
builder,
cargo,
lint_args(builder, IGNORED_RULES_FOR_STD_AND_RUSTC),
lint_args(builder, &self.config, IGNORED_RULES_FOR_STD_AND_RUSTC),
&libstd_stamp(builder, compiler, target),
vec![],
true,
Expand All @@ -159,6 +174,7 @@ impl Step for Std {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Rustc {
pub target: TargetSelection,
config: LintConfig,
/// Whether to lint only a subset of crates.
crates: Vec<String>,
}
Expand All @@ -174,7 +190,8 @@ impl Step for Rustc {

fn make_run(run: RunConfig<'_>) {
let crates = run.make_run_crates(Alias::Compiler);
run.builder.ensure(Rustc { target: run.target, crates });
let config = LintConfig::new(run.builder);
run.builder.ensure(Rustc { target: run.target, config, crates });
}

/// Lints the compiler.
Expand Down Expand Up @@ -221,7 +238,7 @@ impl Step for Rustc {
run_cargo(
builder,
cargo,
lint_args(builder, IGNORED_RULES_FOR_STD_AND_RUSTC),
lint_args(builder, &self.config, IGNORED_RULES_FOR_STD_AND_RUSTC),
&librustc_stamp(builder, compiler, target),
vec![],
true,
Expand All @@ -241,6 +258,7 @@ macro_rules! lint_any {
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct $name {
pub target: TargetSelection,
config: LintConfig,
}

impl Step for $name {
Expand All @@ -252,8 +270,10 @@ macro_rules! lint_any {
}

fn make_run(run: RunConfig<'_>) {
let config = LintConfig::new(run.builder);
run.builder.ensure($name {
target: run.target,
config,
});
}

Expand Down Expand Up @@ -290,7 +310,7 @@ macro_rules! lint_any {
run_cargo(
builder,
cargo,
lint_args(builder, &[]),
lint_args(builder, &self.config, &[]),
&stamp,
vec![],
true,
Expand Down