about summary refs log tree commit diff
diff options
context:
space:
mode:
authoronur-ozkan <work@onurozkan.dev>2024-02-24 13:48:48 +0300
committeronur-ozkan <work@onurozkan.dev>2024-03-18 00:10:23 +0300
commitead18f43f817dee6737137c68e8f6b3126efa98b (patch)
tree3f6f2623e05159ba89ceff49839b47add370ce65
parent35dfc67d94c47a6c6ae28c46e7dc1c547f772485 (diff)
downloadrust-ead18f43f817dee6737137c68e8f6b3126efa98b.tar.gz
rust-ead18f43f817dee6737137c68e8f6b3126efa98b.zip
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>
-rw-r--r--src/bootstrap/src/core/build_steps/check.rs35
1 files 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 args(builder: &Builder<'_>) -> Vec<String> {
             }
         }
 
+        let all_args = std::env::args().collect::<Vec<_>>();
+
         args.extend(strings(&["--", "--cap-lints", "warn"]));
         args.extend(ignored_lints.iter().map(|lint| format!("-Aclippy::{}", lint)));
-        let mut clippy_lint_levels: Vec<String> = 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 args(builder: &Builder<'_>) -> Vec<String> {
     }
 }
 
+/// 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 {
     match kind {
         Kind::Check => "check",