Skip to content

Commit 57135bf

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 21033f6 commit 57135bf

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
@@ -63,21 +63,44 @@ fn args(builder: &Builder<'_>) -> Vec<String> {
6363
}
6464
}
6565

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

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

0 commit comments

Comments
 (0)