Source src/diagnostic/dedup.rs
1//! Canonical diagnostic equality and deterministic fingerprints.2use super::model::Finding;3use std::collections::BTreeMap;4fn canonical_bytes(f: &Finding) -> Vec<u8> {5 let mut key = f.clone();6 key.id.clear();7 key.targets.clear();8 key.rendered = None;9 serde_json::to_vec(&key).expect("Normalized findings are JSON serializable")10}11fn merge_with(input: Vec<Finding>, digest: impl Fn(&[u8]) -> String) -> Vec<Finding> {12 let mut groups: BTreeMap<Vec<u8>, Finding> = BTreeMap::new();13 for mut f in input {14 for t in &mut f.targets {15 t.kinds.sort();16 t.kinds.dedup();17 }18 let key = canonical_bytes(&f);19 if let Some(existing) = groups.get_mut(&key) {20 existing.targets.extend(f.targets);21 } else {22 groups.insert(key, f);23 }24 }25 let mut collisions: BTreeMap<String, usize> = BTreeMap::new();26 let mut result = vec![];27 for (key, mut f) in groups {28 let hash = digest(&key);29 let index = collisions.entry(hash.clone()).or_default();30 f.id = if *index == 0 {31 hash32 } else {33 format!("{hash}-{index}")34 };35 *index += 1;36 f.targets.sort();37 f.targets.dedup();38 result.push(f);39 }40 result.sort_by(|a, b| {41 a.package_id42 .cmp(&b.package_id)43 .then_with(|| {44 a.spans45 .first()46 .map(|s| (&s.path, s.line_start, s.column_start))47 .cmp(48 &b.spans49 .first()50 .map(|s| (&s.path, s.line_start, s.column_start)),51 )52 })53 .then(a.code.cmp(&b.code))54 .then(a.level.cmp(&b.level))55 .then(a.id.cmp(&b.id))56 });57 result58}59pub fn merge_findings(input: Vec<Finding>) -> Vec<Finding> {60 merge_with(input, |b| blake3::hash(b).to_hex().to_string())61}62#[cfg(test)]63mod tests {64 use super::*;65 fn sample() -> Finding {66 let cargo_metadata::Message::CompilerMessage(m) =67 serde_json::from_value(super::super::sample()).unwrap()68 else {69 panic!()70 };71 super::super::normalize::normalize(&m, std::path::Path::new("/demo"))72 }73 #[test]74 fn forced_hash_collision_keeps_both() {75 let a = sample();76 let mut b = a.clone();77 b.message = "other".into();78 let r = merge_with(vec![a.clone(), b.clone()], |_| "same".into());79 assert_ne!(r[0].id, r[1].id);80 assert_eq!(r, merge_with(vec![b, a], |_| "same".into()));81 }82 #[test]83 fn merges_targets_only() {84 let a = sample();85 let mut b = a.clone();86 b.targets[0].name = "demo-test".into();87 let r = merge_findings(vec![a, b]);88 assert_eq!(r.len(), 1);89 assert_eq!(r[0].targets.len(), 2);90 }91 #[test]92 fn keeps_package_level_and_suggestion_differences() {93 let a = sample();94 let mut b = a.clone();95 b.package_id = "another".into();96 let mut c = a.clone();97 c.level = "error".into();98 let mut d = a.clone();99 d.children.push(super::super::model::Note {100 level: "help".into(),101 message: "different fix".into(),102 spans: vec![],103 children: vec![],104 });105 assert_eq!(merge_findings(vec![a, b, c, d]).len(), 4);106 }107 #[test]108 fn ordering_and_ids_ignore_message_arrival_order() {109 let a = sample();110 let mut b = a.clone();111 b.message = "other".into();112 let r = merge_findings(vec![a.clone(), b.clone()]);113 let s = merge_findings(vec![b, a]);114 assert_eq!(r, s);115 assert!(!r[0].id.is_empty()); warning: clippy::assert_is_empty116 }117}118Findings in this file§
warning clippy::assert_is_empty §
warning clippy::assert_is_empty §
Targets (1)
cargo-clippy-html (bin)
Primary: src/diagnostic/dedup.rs:115:9
Primary: src/diagnostic/dedup.rs:115:15
Primary: src/diagnostic/dedup.rs:115:17
help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#assert_is_empty
note: `-W clippy::assert-is-empty` implied by `-W clippy::pedantic`
help: to override `-W clippy::pedantic` add `#[allow(clippy::assert_is_empty)]`
help: use `assert_ne!` to show the value on failure
Suggestion: use `assert_ne!` to show the value on failure
--- "a/src/diagnostic/dedup.rs"
+++ "b/src/diagnostic/dedup.rs"
@@ -112,6 +112,6 @@
let r = merge_findings(vec![a.clone(), b.clone()]);
let s = merge_findings(vec![b, a]);
assert_eq!(r, s);
- assert!(!r[0].id.is_empty());
+ assert_ne!(r[0].id, "");
}
}
Rendered diagnostic (text)
warning: used `assert!` to check that a value is not empty
--> src/diagnostic/dedup.rs:115:9
|
115 | assert!(!r[0].id.is_empty());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#assert_is_empty
= note: `-W clippy::assert-is-empty` implied by `-W clippy::pedantic`
= help: to override `-W clippy::pedantic` add `#[allow(clippy::assert_is_empty)]`
help: use `assert_ne!` to show the value on failure
|
115 - assert!(!r[0].id.is_empty());
115 + assert_ne!(r[0].id, "");
|