debtmap 0.16.3

Code complexity and technical debt analyzer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use super::{
    BoundingStrategy, GrowthPattern, ResourceDetector, ResourceImpact, ResourceManagementIssue,
    SourceLocation,
};
use std::path::Path;
use syn::{visit::Visit, Expr, ExprMethodCall, Fields, ItemImpl, ItemStruct, Type};

pub struct UnboundedCollectionDetector {
    growth_analyzer: CollectionGrowthAnalyzer,
}

impl Default for UnboundedCollectionDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl UnboundedCollectionDetector {
    pub fn new() -> Self {
        Self {
            growth_analyzer: CollectionGrowthAnalyzer::new(),
        }
    }

    fn analyze_collection_usage(&self, visitor: &CollectionVisitor) -> Vec<CollectionUsage> {
        let mut usages = Vec::new();

        for field in &visitor.collection_fields {
            let growth_analysis = self.growth_analyzer.analyze_growth_pattern(
                field,
                &visitor.insertions,
                &visitor.removals,
            );

            if growth_analysis.has_unbounded_growth {
                usages.push(CollectionUsage {
                    name: field.name.clone(),
                    collection_type: field.type_name.clone(),
                    is_unbounded: true,
                    growth_pattern: growth_analysis.pattern,
                    insert_sites: growth_analysis.insert_sites,
                    remove_sites: growth_analysis.remove_sites,
                });
            }
        }

        usages
    }

    fn suggest_bounding_strategy(&self, usage: &CollectionUsage) -> BoundingStrategy {
        match usage.growth_pattern {
            GrowthPattern::UnboundedInsertion => {
                if usage.collection_type.contains("Cache") {
                    BoundingStrategy::LruEviction
                } else {
                    BoundingStrategy::SizeLimit
                }
            }
            GrowthPattern::NoEviction => BoundingStrategy::TimeBasedEviction,
            GrowthPattern::MemoryAccumulation => BoundingStrategy::CapacityCheck,
            GrowthPattern::RecursiveGrowth => BoundingStrategy::SizeLimit,
        }
    }
}

impl ResourceDetector for UnboundedCollectionDetector {
    fn detect_issues(&self, file: &syn::File, _path: &Path) -> Vec<ResourceManagementIssue> {
        let mut visitor = CollectionVisitor::new();
        visitor.visit_file(file);

        let collection_usage = self.analyze_collection_usage(&visitor);

        let mut issues = Vec::new();
        for usage in collection_usage {
            if usage.is_unbounded {
                let bounding_strategy = self.suggest_bounding_strategy(&usage);
                issues.push(ResourceManagementIssue::UnboundedCollection {
                    collection_name: usage.name,
                    collection_type: usage.collection_type,
                    growth_pattern: usage.growth_pattern,
                    bounding_strategy,
                    location: SourceLocation {
                        file: String::new(),
                        line: 1,
                        column: 0,
                    }, // TODO: Extract actual location
                });
            }
        }

        issues
    }

    fn detector_name(&self) -> &'static str {
        "UnboundedCollectionDetector"
    }

    fn assess_resource_impact(&self, issue: &ResourceManagementIssue) -> ResourceImpact {
        match issue {
            ResourceManagementIssue::UnboundedCollection { growth_pattern, .. } => {
                match growth_pattern {
                    GrowthPattern::RecursiveGrowth => ResourceImpact::Critical,
                    GrowthPattern::UnboundedInsertion => ResourceImpact::High,
                    GrowthPattern::NoEviction => ResourceImpact::High,
                    GrowthPattern::MemoryAccumulation => ResourceImpact::Medium,
                }
            }
            _ => ResourceImpact::Medium,
        }
    }
}

struct CollectionVisitor {
    collection_fields: Vec<FieldDefinition>,
    insertions: Vec<MethodCall>,
    removals: Vec<MethodCall>,
    current_struct: Option<String>,
}

impl CollectionVisitor {
    fn new() -> Self {
        Self {
            collection_fields: Vec::new(),
            insertions: Vec::new(),
            removals: Vec::new(),
            current_struct: None,
        }
    }

    fn is_collection_type(&self, type_name: &str) -> bool {
        COLLECTION_TYPES.iter().any(|ct| type_name.contains(ct))
    }
}

impl<'ast> Visit<'ast> for CollectionVisitor {
    fn visit_item_struct(&mut self, node: &'ast ItemStruct) {
        self.current_struct = Some(node.ident.to_string());

        match &node.fields {
            Fields::Named(named) => {
                for field in &named.named {
                    if let Some(ident) = &field.ident {
                        let type_name = extract_type_name(&field.ty);
                        if self.is_collection_type(&type_name) {
                            self.collection_fields.push(FieldDefinition {
                                name: ident.to_string(),
                                type_name,
                                struct_name: self.current_struct.clone(),
                            });
                        }
                    }
                }
            }
            Fields::Unnamed(unnamed) => {
                for (idx, field) in unnamed.unnamed.iter().enumerate() {
                    let type_name = extract_type_name(&field.ty);
                    if self.is_collection_type(&type_name) {
                        self.collection_fields.push(FieldDefinition {
                            name: format!("{}", idx),
                            type_name,
                            struct_name: self.current_struct.clone(),
                        });
                    }
                }
            }
            _ => {}
        }

        syn::visit::visit_item_struct(self, node);
    }

    fn visit_item_impl(&mut self, node: &'ast ItemImpl) {
        if let Type::Path(type_path) = &*node.self_ty {
            if let Some(segment) = type_path.path.segments.last() {
                self.current_struct = Some(segment.ident.to_string());
            }
        }

        syn::visit::visit_item_impl(self, node);
    }

    fn visit_expr_method_call(&mut self, node: &'ast ExprMethodCall) {
        let method_name = node.method.to_string();

        // Track insertions
        if INSERTION_METHODS.contains(&method_name.as_str()) {
            self.insertions.push(MethodCall {
                method_name: method_name.clone(),
                receiver: extract_receiver(&node.receiver),
                line: 0, // Would need span info for accurate line numbers
            });
        }

        // Track removals
        if REMOVAL_METHODS.contains(&method_name.as_str()) {
            self.removals.push(MethodCall {
                method_name,
                receiver: extract_receiver(&node.receiver),
                line: 0,
            });
        }

        syn::visit::visit_expr_method_call(self, node);
    }
}

pub struct CollectionGrowthAnalyzer;

impl CollectionGrowthAnalyzer {
    pub fn new() -> Self {
        Self
    }

    fn analyze_growth_pattern(
        &self,
        field: &FieldDefinition,
        insertions: &[MethodCall],
        removals: &[MethodCall],
    ) -> GrowthAnalysis {
        let mut analysis = GrowthAnalysis::default();

        // Find insertions/removals for this specific field
        let field_insertions: Vec<_> = insertions
            .iter()
            .filter(|i| self.targets_field(&i.receiver, &field.name))
            .collect();

        let field_removals: Vec<_> = removals
            .iter()
            .filter(|r| self.targets_field(&r.receiver, &field.name))
            .collect();

        analysis.insert_sites = field_insertions
            .iter()
            .map(|i| SourceLocation {
                file: String::new(),
                line: i.line,
                column: 0,
            })
            .collect();

        analysis.remove_sites = field_removals
            .iter()
            .map(|r| SourceLocation {
                file: String::new(),
                line: r.line,
                column: 0,
            })
            .collect();

        // Determine if growth is bounded
        if !field_insertions.is_empty() && field_removals.is_empty() {
            analysis.has_unbounded_growth = true;
            analysis.pattern = GrowthPattern::NoEviction;
        } else if field_insertions.len() > field_removals.len() * 2 {
            // Significantly more insertions than removals
            analysis.has_unbounded_growth = true;
            analysis.pattern = GrowthPattern::UnboundedInsertion;
        } else if self.has_recursive_pattern(field) {
            analysis.has_unbounded_growth = true;
            analysis.pattern = GrowthPattern::RecursiveGrowth;
        } else if self.accumulates_without_bounds(&field_insertions, &field_removals) {
            analysis.has_unbounded_growth = true;
            analysis.pattern = GrowthPattern::MemoryAccumulation;
        }

        analysis
    }

    fn targets_field(&self, receiver: &str, field_name: &str) -> bool {
        receiver.contains(field_name) || receiver.contains("self") && receiver.contains(field_name)
    }

    fn has_recursive_pattern(&self, field: &FieldDefinition) -> bool {
        // Check if the collection type contains itself (recursive data structure)
        field.type_name.contains("Vec")
            && field
                .struct_name
                .as_ref()
                .is_some_and(|struct_name| field.type_name.contains(struct_name))
    }

    fn accumulates_without_bounds(
        &self,
        insertions: &[&MethodCall],
        removals: &[&MethodCall],
    ) -> bool {
        // Check if there's a pattern of accumulation without proper cleanup
        !insertions.is_empty() && removals.is_empty()
    }
}

fn extract_type_name(ty: &Type) -> String {
    match ty {
        Type::Path(type_path) => {
            type_path
                .path
                .segments
                .iter()
                .map(|s| {
                    let base = s.ident.to_string();
                    // Include generic parameters
                    if let syn::PathArguments::AngleBracketed(args) = &s.arguments {
                        let generics = args
                            .args
                            .iter()
                            .filter_map(|arg| {
                                if let syn::GenericArgument::Type(ty) = arg {
                                    Some(extract_type_name(ty))
                                } else {
                                    None
                                }
                            })
                            .collect::<Vec<_>>()
                            .join(", ");
                        if !generics.is_empty() {
                            format!("{}<{}>", base, generics)
                        } else {
                            base
                        }
                    } else {
                        base
                    }
                })
                .collect::<Vec<_>>()
                .join("::")
        }
        Type::Reference(reference) => extract_type_name(&reference.elem),
        _ => "Unknown".to_string(),
    }
}

fn extract_receiver(expr: &Expr) -> String {
    match expr {
        Expr::Path(path) => path
            .path
            .segments
            .iter()
            .map(|s| s.ident.to_string())
            .collect::<Vec<_>>()
            .join("::"),
        Expr::Field(field) => {
            format!(
                "{}.{}",
                extract_receiver(&field.base),
                match &field.member {
                    syn::Member::Named(ident) => ident.to_string(),
                    syn::Member::Unnamed(index) => index.index.to_string(),
                }
            )
        }
        _ => "unknown".to_string(),
    }
}

#[derive(Debug, Clone)]
struct FieldDefinition {
    name: String,
    type_name: String,
    struct_name: Option<String>,
}

#[derive(Debug)]
struct CollectionUsage {
    name: String,
    collection_type: String,
    is_unbounded: bool,
    growth_pattern: GrowthPattern,
    #[allow(dead_code)]
    insert_sites: Vec<SourceLocation>,
    #[allow(dead_code)]
    remove_sites: Vec<SourceLocation>,
}

#[derive(Debug, Clone)]
struct MethodCall {
    #[allow(dead_code)]
    method_name: String,
    receiver: String,
    line: usize,
}

#[derive(Debug, Default)]
struct GrowthAnalysis {
    has_unbounded_growth: bool,
    pattern: GrowthPattern,
    insert_sites: Vec<SourceLocation>,
    remove_sites: Vec<SourceLocation>,
}

const COLLECTION_TYPES: &[&str] = &[
    "Vec",
    "HashMap",
    "BTreeMap",
    "HashSet",
    "BTreeSet",
    "VecDeque",
    "LinkedList",
    "BinaryHeap",
    "Cache",
    "Buffer",
    "Queue",
    "Stack",
];

const INSERTION_METHODS: &[&str] = &[
    "push",
    "insert",
    "add",
    "put",
    "append",
    "extend",
    "push_back",
    "push_front",
];

const REMOVAL_METHODS: &[&str] = &[
    "pop",
    "remove",
    "clear",
    "drain",
    "take",
    "pop_back",
    "pop_front",
    "retain",
];