Skip to content

Commit 9e59969

Browse files
pietroalbiniklensy
authored andcommitted
store the lint levels in the clippy structs themselves
1 parent 432972c commit 9e59969

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

src/bootstrap/src/core/build_steps/clippy.rs

+40-20
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ const IGNORED_RULES_FOR_STD_AND_RUSTC: &[&str] = &[
2020
"wrong_self_convention",
2121
];
2222

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

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

@@ -53,12 +51,12 @@ fn lint_args(builder: &Builder<'_>, ignored_rules: &[&str]) -> Vec<String> {
5351

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

56-
if deny.is_empty() && forbid.is_empty() {
54+
if config.deny.is_empty() && config.forbid.is_empty() {
5755
args.extend(strings(&["--cap-lints", "warn"]));
5856
}
5957

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

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

8072
for (prefix, item) in
81-
[("-A", allow_rules), ("-D", deny_rules), ("-W", warn_rules), ("-F", forbid_rules)]
73+
[("-A", &config.allow), ("-D", &config.deny), ("-W", &config.warn), ("-F", &config.forbid)]
8274
{
8375
item.iter().for_each(|v| {
8476
let rule = format!("{prefix}{v}");
85-
let position = all_args.iter().position(|t| t == &rule).unwrap();
77+
// Arguments added by bootstrap in LintConfig won't show up in the all_args list, so
78+
// put them at the end of the command line.
79+
let position = all_args.iter().position(|t| t == &rule).unwrap_or(usize::MAX);
8680
result.push((position, rule));
8781
});
8882
}
@@ -91,9 +85,29 @@ pub(crate) fn get_clippy_rules_in_order(
9185
result.into_iter().map(|v| v.1).collect()
9286
}
9387

88+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
89+
struct LintConfig {
90+
allow: Vec<String>,
91+
warn: Vec<String>,
92+
deny: Vec<String>,
93+
forbid: Vec<String>,
94+
}
95+
96+
impl LintConfig {
97+
fn new(builder: &Builder<'_>) -> Self {
98+
match builder.config.cmd.clone() {
99+
Subcommand::Clippy { allow, deny, warn, forbid, .. } => {
100+
Self { allow, warn, deny, forbid }
101+
}
102+
_ => unreachable!("LintConfig can only be called from `clippy` subcommands."),
103+
}
104+
}
105+
}
106+
94107
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
95108
pub struct Std {
96109
pub target: TargetSelection,
110+
config: LintConfig,
97111
/// Whether to lint only a subset of crates.
98112
crates: Vec<String>,
99113
}
@@ -108,7 +122,8 @@ impl Step for Std {
108122

109123
fn make_run(run: RunConfig<'_>) {
110124
let crates = std_crates_for_run_make(&run);
111-
run.builder.ensure(Std { target: run.target, crates });
125+
let config = LintConfig::new(run.builder);
126+
run.builder.ensure(Std { target: run.target, config, crates });
112127
}
113128

114129
fn run(self, builder: &Builder<'_>) {
@@ -138,7 +153,7 @@ impl Step for Std {
138153
run_cargo(
139154
builder,
140155
cargo,
141-
lint_args(builder, IGNORED_RULES_FOR_STD_AND_RUSTC),
156+
lint_args(builder, &self.config, IGNORED_RULES_FOR_STD_AND_RUSTC),
142157
&libstd_stamp(builder, compiler, target),
143158
vec![],
144159
true,
@@ -150,6 +165,7 @@ impl Step for Std {
150165
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
151166
pub struct Rustc {
152167
pub target: TargetSelection,
168+
config: LintConfig,
153169
/// Whether to lint only a subset of crates.
154170
crates: Vec<String>,
155171
}
@@ -165,7 +181,8 @@ impl Step for Rustc {
165181

166182
fn make_run(run: RunConfig<'_>) {
167183
let crates = run.make_run_crates(Alias::Compiler);
168-
run.builder.ensure(Rustc { target: run.target, crates });
184+
let config = LintConfig::new(run.builder);
185+
run.builder.ensure(Rustc { target: run.target, config, crates });
169186
}
170187

171188
/// Lints the compiler.
@@ -212,7 +229,7 @@ impl Step for Rustc {
212229
run_cargo(
213230
builder,
214231
cargo,
215-
lint_args(builder, IGNORED_RULES_FOR_STD_AND_RUSTC),
232+
lint_args(builder, &self.config, IGNORED_RULES_FOR_STD_AND_RUSTC),
216233
&librustc_stamp(builder, compiler, target),
217234
vec![],
218235
true,
@@ -232,6 +249,7 @@ macro_rules! lint_any {
232249
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
233250
pub struct $name {
234251
pub target: TargetSelection,
252+
config: LintConfig,
235253
}
236254

237255
impl Step for $name {
@@ -243,8 +261,10 @@ macro_rules! lint_any {
243261
}
244262

245263
fn make_run(run: RunConfig<'_>) {
264+
let config = LintConfig::new(run.builder);
246265
run.builder.ensure($name {
247266
target: run.target,
267+
config,
248268
});
249269
}
250270

@@ -281,7 +301,7 @@ macro_rules! lint_any {
281301
run_cargo(
282302
builder,
283303
cargo,
284-
lint_args(builder, &[]),
304+
lint_args(builder, &self.config, &[]),
285305
&stamp,
286306
vec![],
287307
true,

0 commit comments

Comments
 (0)