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 <work@onurozkan.dev>
This commit is contained in:
onur-ozkan 2024-02-24 13:48:48 +03:00
parent 35dfc67d94
commit ead18f43f8

View File

@ -61,14 +61,11 @@ fn strings<'a>(arr: &'a [&str]) -> impl Iterator<Item = String> + 'a {
} }
} }
let all_args = std::env::args().collect::<Vec<_>>();
args.extend(strings(&["--", "--cap-lints", "warn"])); args.extend(strings(&["--", "--cap-lints", "warn"]));
args.extend(ignored_lints.iter().map(|lint| format!("-Aclippy::{}", lint))); args.extend(ignored_lints.iter().map(|lint| format!("-Aclippy::{}", lint)));
let mut clippy_lint_levels: Vec<String> = Vec::new(); args.extend(get_clippy_rules_in_order(&all_args, allow, deny, warn, forbid));
allow.iter().for_each(|v| clippy_lint_levels.push(format!("-A{}", v)));
deny.iter().for_each(|v| clippy_lint_levels.push(format!("-D{}", v)));
warn.iter().for_each(|v| clippy_lint_levels.push(format!("-W{}", v)));
forbid.iter().for_each(|v| clippy_lint_levels.push(format!("-F{}", v)));
args.extend(clippy_lint_levels);
args.extend(builder.config.free_args.clone()); args.extend(builder.config.free_args.clone());
args args
} else { } else {
@ -76,6 +73,32 @@ fn strings<'a>(arr: &'a [&str]) -> impl Iterator<Item = String> + 'a {
} }
} }
/// 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.
pub(crate) fn get_clippy_rules_in_order(
all_args: &[String],
allow_rules: &[String],
deny_rules: &[String],
warn_rules: &[String],
forbid_rules: &[String],
) -> Vec<String> {
let mut result = vec![];
for (prefix, item) in
[("-A", allow_rules), ("-D", deny_rules), ("-W", warn_rules), ("-F", forbid_rules)]
{
item.iter().for_each(|v| {
let rule = format!("{prefix}{v}");
let position = all_args.iter().position(|t| t == &rule).unwrap();
result.push((position, rule));
});
}
result.sort_by_key(|&(position, _)| position);
result.into_iter().map(|v| v.1).collect()
}
fn cargo_subcommand(kind: Kind) -> &'static str { fn cargo_subcommand(kind: Kind) -> &'static str {
match kind { match kind {
Kind::Check => "check", Kind::Check => "check",