Skip to content

Commit 1b0b034

Browse files
committed
reorder clippy rules to their original order before passing them
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. Signed-off-by: onur-ozkan <[email protected]>
1 parent 3cbb932 commit 1b0b034

File tree

1 file changed

+29
-6
lines changed
  • src/bootstrap/src/core/build_steps

1 file changed

+29
-6
lines changed

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,44 @@ fn args(builder: &Builder<'_>) -> Vec<String> {
6161
}
6262
}
6363

64+
let all_args = std::env::args().collect::<Vec<_>>();
65+
6466
args.extend(strings(&["--", "--cap-lints", "warn"]));
6567
args.extend(ignored_lints.iter().map(|lint| format!("-Aclippy::{}", lint)));
66-
let mut clippy_lint_levels: Vec<String> = Vec::new();
67-
allow.iter().for_each(|v| clippy_lint_levels.push(format!("-A{}", v)));
68-
deny.iter().for_each(|v| clippy_lint_levels.push(format!("-D{}", v)));
69-
warn.iter().for_each(|v| clippy_lint_levels.push(format!("-W{}", v)));
70-
forbid.iter().for_each(|v| clippy_lint_levels.push(format!("-F{}", v)));
71-
args.extend(clippy_lint_levels);
68+
args.extend(get_clippy_rules_in_order(&all_args, allow, deny, warn, forbid));
7269
args.extend(builder.config.free_args.clone());
7370
args
7471
} else {
7572
builder.config.free_args.clone()
7673
}
7774
}
7875

76+
/// We need to keep the order of the given clippy lint rules before passing them.
77+
/// Since clap doesn't offer any useful interface for this purpose out of the box,
78+
/// we have to handle it manually.
79+
pub(crate) fn get_clippy_rules_in_order(
80+
all_args: &[String],
81+
allow_rules: &[String],
82+
deny_rules: &[String],
83+
warn_rules: &[String],
84+
forbid_rules: &[String],
85+
) -> Vec<String> {
86+
let mut result = vec![];
87+
88+
for (prefix, item) in
89+
[("-A", allow_rules), ("-D", deny_rules), ("-W", warn_rules), ("-F", forbid_rules)]
90+
{
91+
item.iter().for_each(|v| {
92+
let rule = format!("{prefix}{v}");
93+
let position = all_args.iter().position(|t| t == &rule).unwrap();
94+
result.push((position, rule));
95+
});
96+
}
97+
98+
result.sort_by_key(|&(position, _)| position);
99+
result.into_iter().map(|v| v.1).collect()
100+
}
101+
79102
fn cargo_subcommand(kind: Kind) -> &'static str {
80103
match kind {
81104
Kind::Check => "check",

0 commit comments

Comments
 (0)