use std::collections::HashMap;
use super::FunctionHashEntry;
use crate::config::sections::DuplicatesConfig;
const NEAR_DUP_BUCKET_SIZE: usize = 10;
const MAX_BUCKET_SIZE: usize = 50;
#[derive(Debug, Clone)]
pub struct DuplicateGroup {
pub entries: Vec<DuplicateEntry>,
pub kind: DuplicateKind,
}
#[derive(Debug, Clone)]
pub struct DuplicateEntry {
pub name: String,
pub qualified_name: String,
pub file: String,
pub line: usize,
}
#[derive(Debug, Clone)]
pub enum DuplicateKind {
Exact,
NearDuplicate { similarity: f64 },
}
pub fn detect_duplicates(
parsed: &[(String, String, syn::File)],
config: &DuplicatesConfig,
) -> Vec<DuplicateGroup> {
let entries = super::collect_function_hashes(parsed, config);
let (exact, remaining_indices) = group_exact_duplicates(&entries);
let near = find_near_duplicates(&entries, &remaining_indices, config.similarity_threshold);
merge_groups(exact, near)
}
fn merge_groups(mut exact: Vec<DuplicateGroup>, near: Vec<DuplicateGroup>) -> Vec<DuplicateGroup> {
exact.extend(near);
exact
}
fn group_exact_duplicates(entries: &[FunctionHashEntry]) -> (Vec<DuplicateGroup>, Vec<usize>) {
let mut hash_groups: HashMap<u64, Vec<usize>> = HashMap::new();
for (i, entry) in entries.iter().enumerate() {
hash_groups.entry(entry.hash).or_default().push(i);
}
let mut groups = Vec::new();
let mut remaining = Vec::new();
for indices in hash_groups.values() {
if indices.len() >= 2 {
let group_entries: Vec<DuplicateEntry> = indices
.iter()
.map(|&i| DuplicateEntry {
name: entries[i].name.clone(),
qualified_name: entries[i].qualified_name.clone(),
file: entries[i].file.clone(),
line: entries[i].line,
})
.collect();
groups.push(DuplicateGroup {
entries: group_entries,
kind: DuplicateKind::Exact,
});
} else {
remaining.extend(indices);
}
}
(groups, remaining)
}
fn find_near_duplicates(
entries: &[FunctionHashEntry],
candidate_indices: &[usize],
threshold: f64,
) -> Vec<DuplicateGroup> {
let mut buckets: HashMap<usize, Vec<usize>> = HashMap::new();
for &idx in candidate_indices {
let bucket_key = entries[idx].token_count / NEAR_DUP_BUCKET_SIZE;
buckets.entry(bucket_key).or_default().push(idx);
}
let compute_sim = |a: &[crate::normalize::NormalizedToken],
b: &[crate::normalize::NormalizedToken]|
-> f64 { crate::normalize::jaccard_similarity(a, b) };
let mut groups = Vec::new();
for indices in buckets.values() {
if indices.len() < 2 || indices.len() > MAX_BUCKET_SIZE {
continue;
}
for i in 0..indices.len() {
for j in (i + 1)..indices.len() {
let a = indices[i];
let b = indices[j];
let sim = compute_sim(&entries[a].tokens, &entries[b].tokens);
if sim >= threshold {
groups.push(DuplicateGroup {
entries: vec![
DuplicateEntry {
name: entries[a].name.clone(),
qualified_name: entries[a].qualified_name.clone(),
file: entries[a].file.clone(),
line: entries[a].line,
},
DuplicateEntry {
name: entries[b].name.clone(),
qualified_name: entries[b].qualified_name.clone(),
file: entries[b].file.clone(),
line: entries[b].line,
},
],
kind: DuplicateKind::NearDuplicate { similarity: sim },
});
}
}
}
}
groups
}
#[cfg(test)]
mod tests {
use super::*;
fn parse(code: &str) -> Vec<(String, String, syn::File)> {
let syntax = syn::parse_file(code).expect("parse failed");
vec![("test.rs".to_string(), code.to_string(), syntax)]
}
fn parse_multi(files: &[(&str, &str)]) -> Vec<(String, String, syn::File)> {
files
.iter()
.map(|(name, code)| {
let syntax = syn::parse_file(code).expect("parse failed");
(name.to_string(), code.to_string(), syntax)
})
.collect()
}
const TEST_MIN_TOKENS: usize = 3;
fn low_threshold_config() -> DuplicatesConfig {
DuplicatesConfig {
min_tokens: TEST_MIN_TOKENS,
min_lines: 1,
..DuplicatesConfig::default()
}
}
#[test]
fn test_detect_duplicates_no_functions() {
let parsed = parse("");
let config = low_threshold_config();
let groups = detect_duplicates(&parsed, &config);
assert!(groups.is_empty());
}
#[test]
fn test_detect_duplicates_no_duplicates() {
let code = r#"
fn foo() { let x = 1; let y = x + 2; let z = y * x; }
fn bar() { let a = "hello"; let b = a.len(); if b > 0 { return; } }
"#;
let parsed = parse(code);
let config = low_threshold_config();
let groups = detect_duplicates(&parsed, &config);
assert!(
groups.is_empty(),
"Different functions should not be duplicates"
);
}
#[test]
fn test_detect_exact_duplicates_same_structure() {
let parsed = parse_multi(&[
(
"a.rs",
"fn process_a() { let x = 1; let y = x + 2; let z = y * x; }",
),
(
"b.rs",
"fn process_b() { let a = 1; let b = a + 2; let c = b * a; }",
),
]);
let config = low_threshold_config();
let groups = detect_duplicates(&parsed, &config);
assert_eq!(groups.len(), 1, "Should detect one exact duplicate group");
assert!(matches!(groups[0].kind, DuplicateKind::Exact));
assert_eq!(groups[0].entries.len(), 2);
}
#[test]
fn test_detect_duplicates_different_structure() {
let parsed = parse_multi(&[
(
"a.rs",
"fn add() { let x = 1; let y = x + 2; let z = y + x; }",
),
(
"b.rs",
"fn mul() { let a = 1; let b = a * 2; let c = b * a; }",
),
]);
let config = low_threshold_config();
let groups = detect_duplicates(&parsed, &config);
let exact_groups: Vec<_> = groups
.iter()
.filter(|g| matches!(g.kind, DuplicateKind::Exact))
.collect();
assert!(
exact_groups.is_empty(),
"Different operators should not be exact duplicates"
);
}
#[test]
fn test_detect_duplicates_below_min_tokens_excluded() {
let parsed = parse_multi(&[
("a.rs", "fn tiny_a() { let x = 1; }"),
("b.rs", "fn tiny_b() { let y = 1; }"),
]);
let config = DuplicatesConfig::default(); let groups = detect_duplicates(&parsed, &config);
assert!(
groups.is_empty(),
"Small functions below min_tokens should be excluded"
);
}
#[test]
fn test_detect_duplicates_test_functions_excluded() {
let code = r#"
#[cfg(test)]
mod tests {
fn helper_a() { let x = 1; let y = x + 2; let z = y * x; }
fn helper_b() { let a = 1; let b = a + 2; let c = b * a; }
}
"#;
let parsed = parse(code);
let mut config = low_threshold_config();
config.ignore_tests = true;
let groups = detect_duplicates(&parsed, &config);
assert!(
groups.is_empty(),
"Test functions should be excluded when ignore_tests=true"
);
}
#[test]
fn test_detect_duplicates_test_functions_included() {
let code = r#"
#[cfg(test)]
mod tests {
fn helper_a() { let x = 1; let y = x + 2; let z = y * x; }
fn helper_b() { let a = 1; let b = a + 2; let c = b * a; }
}
"#;
let parsed = parse(code);
let mut config = low_threshold_config();
config.ignore_tests = false;
let groups = detect_duplicates(&parsed, &config);
assert_eq!(
groups.len(),
1,
"Test functions should be included when ignore_tests=false"
);
}
#[test]
fn test_detect_duplicates_three_way() {
let parsed = parse_multi(&[
(
"a.rs",
"fn func_a() { let x = 1; let y = x + 2; let z = y * x; }",
),
(
"b.rs",
"fn func_b() { let a = 1; let b = a + 2; let c = b * a; }",
),
(
"c.rs",
"fn func_c() { let p = 1; let q = p + 2; let r = q * p; }",
),
]);
let config = low_threshold_config();
let groups = detect_duplicates(&parsed, &config);
assert_eq!(groups.len(), 1);
assert_eq!(groups[0].entries.len(), 3, "Should detect 3-way duplicate");
}
#[test]
fn test_detect_duplicates_config_disabled() {
let parsed = parse_multi(&[
(
"a.rs",
"fn func_a() { let x = 1; let y = x + 2; let z = y * x; }",
),
(
"b.rs",
"fn func_b() { let a = 1; let b = a + 2; let c = b * a; }",
),
]);
let mut config = low_threshold_config();
config.enabled = false;
let groups = detect_duplicates(&parsed, &config);
assert!(!groups.is_empty());
}
#[test]
fn test_group_exact_duplicates_returns_remaining() {
let entries = vec![
FunctionHashEntry {
name: "a".into(),
qualified_name: "a".into(),
file: "a.rs".into(),
line: 1,
hash: 100,
token_count: 10,
tokens: vec![],
},
FunctionHashEntry {
name: "b".into(),
qualified_name: "b".into(),
file: "b.rs".into(),
line: 1,
hash: 200,
token_count: 10,
tokens: vec![],
},
FunctionHashEntry {
name: "c".into(),
qualified_name: "c".into(),
file: "c.rs".into(),
line: 1,
hash: 100,
token_count: 10,
tokens: vec![],
},
];
let (groups, remaining) = group_exact_duplicates(&entries);
assert_eq!(groups.len(), 1); assert_eq!(remaining.len(), 1); assert_eq!(remaining[0], 1); }
#[test]
fn test_detect_near_duplicates_high_similarity() {
let parsed = parse_multi(&[
(
"a.rs",
"fn func_a() { let x = 1; let y = x + 2; let z = y * x; let w = z + 1; }",
),
(
"b.rs",
"fn func_b() { let x = 1; let y = x + 2; let z = y * x; let w = z - 1; }",
),
]);
let mut config = low_threshold_config();
config.similarity_threshold = 0.80;
let groups = detect_duplicates(&parsed, &config);
let near_groups: Vec<_> = groups
.iter()
.filter(|g| matches!(g.kind, DuplicateKind::NearDuplicate { .. }))
.collect();
if !near_groups.is_empty() {
let DuplicateKind::NearDuplicate { similarity } = near_groups[0].kind else {
panic!("expected near duplicate");
};
assert!(similarity >= 0.80);
}
}
#[test]
fn test_duplicate_entry_has_file_and_line() {
let parsed = parse_multi(&[
(
"module_a.rs",
"fn func_a() { let x = 1; let y = x + 2; let z = y * x; }",
),
(
"module_b.rs",
"fn func_b() { let a = 1; let b = a + 2; let c = b * a; }",
),
]);
let config = low_threshold_config();
let groups = detect_duplicates(&parsed, &config);
assert!(!groups.is_empty());
for entry in &groups[0].entries {
assert!(!entry.file.is_empty());
assert!(!entry.name.is_empty());
}
}
}