Skip to content

Commit 6b846cc

Browse files
committed
create new build step clippy
Signed-off-by: onur-ozkan <work@onurozkan.dev>
1 parent c86f3ac commit 6b846cc

File tree

5 files changed

+335
-100
lines changed

5 files changed

+335
-100
lines changed

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

Lines changed: 14 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ 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 | Kind::Clippy => "check",
17+
Kind::Fix => "fix",
18+
_ => unreachable!(),
19+
}
20+
}
21+
1422
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1523
pub struct Std {
1624
pub target: TargetSelection,
@@ -22,97 +30,6 @@ pub struct Std {
2230
crates: Vec<String>,
2331
}
2432

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-
11633
impl Std {
11734
pub fn new(target: TargetSelection) -> Self {
11835
Self { target, crates: vec![] }
@@ -164,7 +81,7 @@ impl Step for Std {
16481
run_cargo(
16582
builder,
16683
cargo,
167-
args(builder),
84+
builder.config.free_args.clone(),
16885
&libstd_stamp(builder, compiler, target),
16986
vec![],
17087
true,
@@ -221,7 +138,7 @@ impl Step for Std {
221138
run_cargo(
222139
builder,
223140
cargo,
224-
args(builder),
141+
builder.config.free_args.clone(),
225142
&libstd_test_stamp(builder, compiler, target),
226143
vec![],
227144
true,
@@ -318,7 +235,7 @@ impl Step for Rustc {
318235
run_cargo(
319236
builder,
320237
cargo,
321-
args(builder),
238+
builder.config.free_args.clone(),
322239
&librustc_stamp(builder, compiler, target),
323240
vec![],
324241
true,
@@ -384,7 +301,7 @@ impl Step for CodegenBackend {
384301
run_cargo(
385302
builder,
386303
cargo,
387-
args(builder),
304+
builder.config.free_args.clone(),
388305
&codegen_backend_stamp(builder, compiler, target, backend),
389306
vec![],
390307
true,
@@ -450,7 +367,7 @@ impl Step for RustAnalyzer {
450367
run_cargo(
451368
builder,
452369
cargo,
453-
args(builder),
370+
builder.config.free_args.clone(),
454371
&stamp(builder, compiler, target),
455372
vec![],
456373
true,
@@ -513,7 +430,7 @@ macro_rules! tool_check_step {
513430
run_cargo(
514431
builder,
515432
cargo,
516-
args(builder),
433+
builder.config.free_args.clone(),
517434
&stamp(builder, compiler, target),
518435
vec![],
519436
true,

0 commit comments

Comments
 (0)