From 9e8e6b95b2753c141b970440383430831e9196a4 Mon Sep 17 00:00:00 2001 From: RussellLuo Date: Fri, 6 Sep 2024 19:40:49 +0800 Subject: [PATCH 1/3] Remove unnecessary SARIF rules --- crates/ruff_linter/src/message/sarif.rs | 33 ++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/crates/ruff_linter/src/message/sarif.rs b/crates/ruff_linter/src/message/sarif.rs index 9cc10caf91198..b06b6a0db6327 100644 --- a/crates/ruff_linter/src/message/sarif.rs +++ b/crates/ruff_linter/src/message/sarif.rs @@ -1,3 +1,4 @@ +use std::collections::HashSet; use std::io::Write; use anyhow::Result; @@ -27,6 +28,13 @@ impl Emitter for SarifEmitter { .map(SarifResult::from_message) .collect::>>()?; + let mut rule_ids = HashSet::new(); + for result in results.iter() { + if let Some(rule) = result.rule { + rule_ids.insert(rule.noqa_code().to_string()); + } + } + let output = json!({ "$schema": "https://json.schemastore.org/sarif-2.1.0.json", "version": "2.1.0", @@ -35,7 +43,10 @@ impl Emitter for SarifEmitter { "driver": { "name": "ruff", "informationUri": "https://github.com/astral-sh/ruff", - "rules": Rule::iter().map(SarifRule::from).collect::>(), + "rules": Rule::iter() + .filter(|rule| rule_ids.contains(&rule.noqa_code().to_string())) + .map(SarifRule::from) + .collect::>(), "version": VERSION.to_string(), } }, @@ -216,9 +227,23 @@ mod tests { let results = sarif["runs"][0]["results"].as_array().unwrap(); assert_eq!(results.len(), 3); assert_eq!( - results[0]["message"]["text"].as_str().unwrap(), - "`os` imported but unused" + results + .iter() + .map(|r| r["message"]["text"].as_str().unwrap()) + .collect::>(), + vec![ + "`os` imported but unused", + "Local variable `x` is assigned to but never used", + "Undefined name `a`", + ] + ); + assert_eq!(rules.len(), 3); + assert_eq!( + rules + .iter() + .map(|r| r["id"].as_str().unwrap()) + .collect::>(), + vec!["F401", "F821", "F841"], ); - assert!(rules.len() > 3); } } From 325b534a2409e1bb985eee3afd95742fba256664 Mon Sep 17 00:00:00 2001 From: RussellLuo Date: Fri, 6 Sep 2024 20:07:32 +0800 Subject: [PATCH 2/3] Tweaks --- crates/ruff_linter/src/message/sarif.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ruff_linter/src/message/sarif.rs b/crates/ruff_linter/src/message/sarif.rs index b06b6a0db6327..05ac2102cb9da 100644 --- a/crates/ruff_linter/src/message/sarif.rs +++ b/crates/ruff_linter/src/message/sarif.rs @@ -29,7 +29,7 @@ impl Emitter for SarifEmitter { .collect::>>()?; let mut rule_ids = HashSet::new(); - for result in results.iter() { + for result in &results { if let Some(rule) = result.rule { rule_ids.insert(rule.noqa_code().to_string()); } From c5d7f1d72a60aa1ff4376de7ddac48afcfaf7d15 Mon Sep 17 00:00:00 2001 From: RussellLuo Date: Mon, 9 Sep 2024 20:09:52 +0800 Subject: [PATCH 3/3] Simplify code as suggested by Micha --- crates/ruff_linter/src/message/sarif.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/crates/ruff_linter/src/message/sarif.rs b/crates/ruff_linter/src/message/sarif.rs index 05ac2102cb9da..8a354de12b0e4 100644 --- a/crates/ruff_linter/src/message/sarif.rs +++ b/crates/ruff_linter/src/message/sarif.rs @@ -4,7 +4,6 @@ use std::io::Write; use anyhow::Result; use serde::{Serialize, Serializer}; use serde_json::json; -use strum::IntoEnumIterator; use ruff_source_file::OneIndexed; @@ -28,12 +27,9 @@ impl Emitter for SarifEmitter { .map(SarifResult::from_message) .collect::>>()?; - let mut rule_ids = HashSet::new(); - for result in &results { - if let Some(rule) = result.rule { - rule_ids.insert(rule.noqa_code().to_string()); - } - } + let unique_rules: HashSet<_> = results.iter().filter_map(|result| result.rule).collect(); + let mut rules: Vec = unique_rules.into_iter().map(SarifRule::from).collect(); + rules.sort_by(|a, b| a.code.cmp(&b.code)); let output = json!({ "$schema": "https://json.schemastore.org/sarif-2.1.0.json", @@ -43,10 +39,7 @@ impl Emitter for SarifEmitter { "driver": { "name": "ruff", "informationUri": "https://github.com/astral-sh/ruff", - "rules": Rule::iter() - .filter(|rule| rule_ids.contains(&rule.noqa_code().to_string())) - .map(SarifRule::from) - .collect::>(), + "rules": rules, "version": VERSION.to_string(), } },