From ead18f43f817dee6737137c68e8f6b3126efa98b Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Sat, 24 Feb 2024 13:48:48 +0300 Subject: [PATCH] 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 --- src/bootstrap/src/core/build_steps/check.rs | 35 +++++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/check.rs b/src/bootstrap/src/core/build_steps/check.rs index a90139a070a..f582d5ada3e 100644 --- a/src/bootstrap/src/core/build_steps/check.rs +++ b/src/bootstrap/src/core/build_steps/check.rs @@ -61,14 +61,11 @@ fn strings<'a>(arr: &'a [&str]) -> impl Iterator + 'a { } } + let all_args = std::env::args().collect::>(); + args.extend(strings(&["--", "--cap-lints", "warn"])); args.extend(ignored_lints.iter().map(|lint| format!("-Aclippy::{}", lint))); - let mut clippy_lint_levels: Vec = Vec::new(); - 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(get_clippy_rules_in_order(&all_args, allow, deny, warn, forbid)); args.extend(builder.config.free_args.clone()); args } else { @@ -76,6 +73,32 @@ fn strings<'a>(arr: &'a [&str]) -> impl Iterator + '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 { + 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 { match kind { Kind::Check => "check",