Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Only include rules with diagnostics in SARIF metadata #13268

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions crates/ruff_linter/src/message/sarif.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::collections::HashSet;
use std::io::Write;

use anyhow::Result;
use serde::{Serialize, Serializer};
use serde_json::json;
use strum::IntoEnumIterator;

use ruff_source_file::OneIndexed;

Expand All @@ -27,6 +27,10 @@ impl Emitter for SarifEmitter {
.map(SarifResult::from_message)
.collect::<Result<Vec<_>>>()?;

let unique_rules: HashSet<_> = results.iter().filter_map(|result| result.rule).collect();
let mut rules: Vec<SarifRule> = 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",
"version": "2.1.0",
Expand All @@ -35,7 +39,7 @@ impl Emitter for SarifEmitter {
"driver": {
"name": "ruff",
"informationUri": "https://github.com/astral-sh/ruff",
"rules": Rule::iter().map(SarifRule::from).collect::<Vec<_>>(),
"rules": rules,
"version": VERSION.to_string(),
}
},
Expand Down Expand Up @@ -216,9 +220,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<_>>(),
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<_>>(),
vec!["F401", "F821", "F841"],
);
assert!(rules.len() > 3);
}
}
Loading