Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions crates/cf-clones/src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,14 @@ pub fn compute_metrics_from_report(report: &Report) -> ComputedMetrics {

/// Extracts the clone pairs from a report (handling the array-of-maps shape
/// produced by [`Analyzer::build_report`]).
fn extract_clone_pairs(report: &Report) -> Vec<ClonePair> {
///
/// `None` when the report carries no `clone_pairs` list (the reference
/// `extractClonePairs` returns a `nil` slice → `null`); `Some` — possibly
/// empty — when the key holds a list (the reference builds a non-nil slice
/// via `make([]ClonePair, 0, …)` → `[]`).
fn extract_clone_pairs(report: &Report) -> Option<Vec<ClonePair>> {
let Some(GoValue::Array(items)) = cf_reportutil::get(report, KEY_CLONE_PAIRS) else {
return Vec::new();
return None;
};

let mut pairs = Vec::with_capacity(items.len());
Expand All @@ -268,7 +273,7 @@ fn extract_clone_pairs(report: &Report) -> Vec<ClonePair> {
pairs.push(clone_pair_from_map(m));
}
}
pairs
Some(pairs)
}

/// Extracts a [`ClonePair`] from a map value.
Expand Down Expand Up @@ -386,9 +391,10 @@ mod tests {
assert!((cf_reportutil::get_float64(&report, KEY_CLONE_RATIO) - 1.0).abs() < 1e-12);

let metrics = compute_metrics_from_report(&report);
assert_eq!(metrics.clone_pairs.len(), 1);
assert_eq!(metrics.clone_pairs[0].clone_type, "Type-1");
assert!((metrics.clone_pairs[0].similarity - 1.0).abs() < 1e-9);
let pairs = metrics.clone_pairs.as_deref().expect("pairs list present");
assert_eq!(pairs.len(), 1);
assert_eq!(pairs[0].clone_type, "Type-1");
assert!((pairs[0].similarity - 1.0).abs() < 1e-9);
}

#[test]
Expand Down
36 changes: 18 additions & 18 deletions crates/cf-clones/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ pub struct ComputedMetrics {
/// `None` omits the key entirely (`omitempty` on an absent map).
pub clone_type_dist: Option<CloneTypeCounts>,
/// The detected clone pairs (`clone_pairs`).
pub clone_pairs: Vec<ClonePair>,
///
/// `None` mirrors a Go `nil` slice (the report carried no `clone_pairs`
/// key — e.g. the analyzer's empty result) and renders `null`; `Some`
/// mirrors the non-nil slice the aggregator extraction builds
/// (`make([]ClonePair, 0, …)`), so an empty walk result renders `[]`.
pub clone_pairs: Option<Vec<ClonePair>>,
/// Human-readable summary (`message`).
pub message: String,
}
Expand All @@ -154,8 +159,10 @@ impl ComputedMetrics {
/// Builds the struct-origin [`GoValue`] tree for this metrics struct
/// (report-format contract): fields emit in declaration order,
/// `clone_type_distribution` is omitted when `None` (`omitempty`), and
/// `clone_pairs` emits `null` for an empty slice (the contract's rendering
/// of an absent list without `omitempty`).
/// `clone_pairs` — which has no `omitempty` — renders a Go `nil` slice
/// (`None`) as `null` and a non-nil slice (`Some`) as a JSON array, so the
/// aggregator's empty-but-present list renders `[]` exactly as the
/// reference binary emits it.
#[must_use]
pub fn to_go_value(&self) -> GoValue {
let mut m = GoMap::new_struct();
Expand All @@ -166,19 +173,12 @@ impl ComputedMetrics {
// omitempty: a present map is emitted even if all counts are zero.
m.push("clone_type_distribution", clone_type_dist_map(counts));
}
// `clone_pairs` has no omitempty: an empty list renders as `null`.
if self.clone_pairs.is_empty() {
m.push("clone_pairs", GoValue::Null);
} else {
m.push(
match &self.clone_pairs {
None => m.push("clone_pairs", GoValue::Null),
Some(pairs) => m.push(
"clone_pairs",
GoValue::Array(
self.clone_pairs
.iter()
.map(ClonePair::to_go_value)
.collect(),
),
);
GoValue::Array(pairs.iter().map(ClonePair::to_go_value).collect()),
),
}
m.push("message", GoValue::Str(self.message.clone()));
GoValue::Object(m)
Expand Down Expand Up @@ -251,7 +251,7 @@ mod tests {
total_clone_pairs: 0,
clone_ratio: 0.0,
clone_type_dist: None,
clone_pairs: Vec::new(),
clone_pairs: None,
message: "No code clones detected".into(),
};
let json = Encoder::marshal().encode_to_string(&cm.to_go_value());
Expand All @@ -272,12 +272,12 @@ mod tests {
type2: 1,
type3: 0,
}),
clone_pairs: vec![ClonePair {
clone_pairs: Some(vec![ClonePair {
func_a: "A".into(),
func_b: "B".into(),
similarity: 0.9,
clone_type: CLONE_TYPE2.into(),
}],
}]),
message: "Low duplication - few clone pairs detected".into(),
};
let json = Encoder::marshal().encode_to_string(&cm.to_go_value());
Expand Down
4 changes: 4 additions & 0 deletions crates/cf-commands/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,7 @@ cf-mcp = { path = "../cf-mcp", optional = true }

[dev-dependencies]
tempfile = "3"
# Test fixtures build tiny multi-language git repositories in memory-backed
# tempdirs (history-filter oracle tests) — write access comes straight from
# git2, the same libgit2 vendoring cf-gitlib is pinned to.
git2 = { workspace = true }
Loading
Loading