Skip to content

Commit 8229a34

Browse files
authored
Rollup merge of #122883 - onur-ozkan:clippy-build-step, r=albertlarsan68
refactor clippy in bootstrap Previously, using clippy in bootstrap was not very useful as explained in #122825. In short, regardless of the given path clippy would always check the entire compiler and std tree. This makes it impossible to run clippy on different paths with different set of rules. This PR fixes that by allowing developers to run clippy with specific rules on specific paths (e.g., we can run `x clippy compiler -Aclippy::all -Dclippy::correctness` and `x clippy library/std -Dclippy::all` and none of them will affect each other). Resolves #122825
2 parents 45940fe + 16cf0e6 commit 8229a34

File tree

8 files changed

+395
-103
lines changed

8 files changed

+395
-103
lines changed

src/bootstrap/src/core/build_steps/check.rs

+16-97
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ use crate::core::config::TargetSelection;
1111
use crate::{Compiler, Mode, Subcommand};
1212
use std::path::{Path, PathBuf};
1313

14+
pub fn cargo_subcommand(kind: Kind) -> &'static str {
15+
match kind {
16+
Kind::Check
17+
// We ensure check steps for both std and rustc from build_steps/clippy, so handle `Kind::Clippy` as well.
18+
| Kind::Clippy => "check",
19+
Kind::Fix => "fix",
20+
_ => unreachable!(),
21+
}
22+
}
23+
1424
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1525
pub struct Std {
1626
pub target: TargetSelection,
@@ -22,97 +32,6 @@ pub struct Std {
2232
crates: Vec<String>,
2333
}
2434

25-
/// Returns args for the subcommand itself (not for cargo)
26-
fn args(builder: &Builder<'_>) -> Vec<String> {
27-
fn strings<'a>(arr: &'a [&str]) -> impl Iterator<Item = String> + 'a {
28-
arr.iter().copied().map(String::from)
29-
}
30-
31-
if let Subcommand::Clippy { fix, allow_dirty, allow_staged, allow, deny, warn, forbid } =
32-
&builder.config.cmd
33-
{
34-
// disable the most spammy clippy lints
35-
let ignored_lints = [
36-
"many_single_char_names", // there are a lot in stdarch
37-
"collapsible_if",
38-
"type_complexity",
39-
"missing_safety_doc", // almost 3K warnings
40-
"too_many_arguments",
41-
"needless_lifetimes", // people want to keep the lifetimes
42-
"wrong_self_convention",
43-
];
44-
let mut args = vec![];
45-
if *fix {
46-
#[rustfmt::skip]
47-
args.extend(strings(&[
48-
"--fix", "-Zunstable-options",
49-
// FIXME: currently, `--fix` gives an error while checking tests for libtest,
50-
// possibly because libtest is not yet built in the sysroot.
51-
// As a workaround, avoid checking tests and benches when passed --fix.
52-
"--lib", "--bins", "--examples",
53-
]));
54-
55-
if *allow_dirty {
56-
args.push("--allow-dirty".to_owned());
57-
}
58-
59-
if *allow_staged {
60-
args.push("--allow-staged".to_owned());
61-
}
62-
}
63-
64-
args.extend(strings(&["--"]));
65-
66-
if deny.is_empty() && forbid.is_empty() {
67-
args.extend(strings(&["--cap-lints", "warn"]));
68-
}
69-
70-
let all_args = std::env::args().collect::<Vec<_>>();
71-
args.extend(get_clippy_rules_in_order(&all_args, allow, deny, warn, forbid));
72-
73-
args.extend(ignored_lints.iter().map(|lint| format!("-Aclippy::{}", lint)));
74-
args.extend(builder.config.free_args.clone());
75-
args
76-
} else {
77-
builder.config.free_args.clone()
78-
}
79-
}
80-
81-
/// We need to keep the order of the given clippy lint rules before passing them.
82-
/// Since clap doesn't offer any useful interface for this purpose out of the box,
83-
/// we have to handle it manually.
84-
pub(crate) fn get_clippy_rules_in_order(
85-
all_args: &[String],
86-
allow_rules: &[String],
87-
deny_rules: &[String],
88-
warn_rules: &[String],
89-
forbid_rules: &[String],
90-
) -> Vec<String> {
91-
let mut result = vec![];
92-
93-
for (prefix, item) in
94-
[("-A", allow_rules), ("-D", deny_rules), ("-W", warn_rules), ("-F", forbid_rules)]
95-
{
96-
item.iter().for_each(|v| {
97-
let rule = format!("{prefix}{v}");
98-
let position = all_args.iter().position(|t| t == &rule).unwrap();
99-
result.push((position, rule));
100-
});
101-
}
102-
103-
result.sort_by_key(|&(position, _)| position);
104-
result.into_iter().map(|v| v.1).collect()
105-
}
106-
107-
fn cargo_subcommand(kind: Kind) -> &'static str {
108-
match kind {
109-
Kind::Check => "check",
110-
Kind::Clippy => "clippy",
111-
Kind::Fix => "fix",
112-
_ => unreachable!(),
113-
}
114-
}
115-
11635
impl Std {
11736
pub fn new(target: TargetSelection) -> Self {
11837
Self { target, crates: vec![] }
@@ -164,7 +83,7 @@ impl Step for Std {
16483
run_cargo(
16584
builder,
16685
cargo,
167-
args(builder),
86+
builder.config.free_args.clone(),
16887
&libstd_stamp(builder, compiler, target),
16988
vec![],
17089
true,
@@ -221,7 +140,7 @@ impl Step for Std {
221140
run_cargo(
222141
builder,
223142
cargo,
224-
args(builder),
143+
builder.config.free_args.clone(),
225144
&libstd_test_stamp(builder, compiler, target),
226145
vec![],
227146
true,
@@ -318,7 +237,7 @@ impl Step for Rustc {
318237
run_cargo(
319238
builder,
320239
cargo,
321-
args(builder),
240+
builder.config.free_args.clone(),
322241
&librustc_stamp(builder, compiler, target),
323242
vec![],
324243
true,
@@ -384,7 +303,7 @@ impl Step for CodegenBackend {
384303
run_cargo(
385304
builder,
386305
cargo,
387-
args(builder),
306+
builder.config.free_args.clone(),
388307
&codegen_backend_stamp(builder, compiler, target, backend),
389308
vec![],
390309
true,
@@ -450,7 +369,7 @@ impl Step for RustAnalyzer {
450369
run_cargo(
451370
builder,
452371
cargo,
453-
args(builder),
372+
builder.config.free_args.clone(),
454373
&stamp(builder, compiler, target),
455374
vec![],
456375
true,
@@ -513,7 +432,7 @@ macro_rules! tool_check_step {
513432
run_cargo(
514433
builder,
515434
cargo,
516-
args(builder),
435+
builder.config.free_args.clone(),
517436
&stamp(builder, compiler, target),
518437
vec![],
519438
true,

0 commit comments

Comments
 (0)