depyler-analyzer 4.1.1

Static analysis and optimization engine for Depyler
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
pub mod complexity;
pub mod metrics;
pub mod type_flow;

// Re-export complexity functions for easier use
pub use complexity::{
    calculate_cognitive, calculate_cyclomatic, calculate_max_nesting, count_statements,
};

use anyhow::Result;
use depyler_core::hir::{HirFunction, HirModule};
use serde::{Deserialize, Serialize};

#[cfg(test)]
use depyler_annotations::TranspilationAnnotations;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnalysisResult {
    pub module_metrics: ModuleMetrics,
    pub function_metrics: Vec<FunctionMetrics>,
    pub type_coverage: TypeCoverage,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModuleMetrics {
    pub total_functions: usize,
    pub total_lines: usize,
    pub avg_cyclomatic_complexity: f64,
    pub max_cyclomatic_complexity: u32,
    pub avg_cognitive_complexity: f64,
    pub max_cognitive_complexity: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionMetrics {
    pub name: String,
    pub cyclomatic_complexity: u32,
    pub cognitive_complexity: u32,
    pub lines_of_code: usize,
    pub parameters: usize,
    pub max_nesting_depth: usize,
    pub has_type_annotations: bool,
    pub return_type_annotated: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypeCoverage {
    pub total_parameters: usize,
    pub annotated_parameters: usize,
    pub total_functions: usize,
    pub functions_with_return_type: usize,
    pub coverage_percentage: f64,
}

pub struct Analyzer {
    #[allow(dead_code)]
    enable_type_inference: bool,
}

impl Analyzer {
    pub fn new() -> Self {
        Self {
            enable_type_inference: true,
        }
    }

    pub fn analyze(&self, module: &HirModule) -> Result<AnalysisResult> {
        let function_metrics: Vec<FunctionMetrics> = module
            .functions
            .iter()
            .map(|f| self.analyze_function(f))
            .collect::<Result<Vec<_>>>()?;

        let module_metrics = self.calculate_module_metrics(&function_metrics);
        let type_coverage = self.calculate_type_coverage(module);

        Ok(AnalysisResult {
            module_metrics,
            function_metrics,
            type_coverage,
        })
    }

    fn analyze_function(&self, func: &HirFunction) -> Result<FunctionMetrics> {
        let cyclomatic = complexity::calculate_cyclomatic(&func.body);
        let cognitive = complexity::calculate_cognitive(&func.body);
        let max_nesting = complexity::calculate_max_nesting(&func.body);
        let loc = complexity::count_statements(&func.body);

        let has_type_annotations = func
            .params
            .iter()
            .all(|param| !matches!(param.ty, depyler_core::hir::Type::Unknown));
        let return_type_annotated = !matches!(func.ret_type, depyler_core::hir::Type::Unknown);

        Ok(FunctionMetrics {
            name: func.name.clone(),
            cyclomatic_complexity: cyclomatic,
            cognitive_complexity: cognitive,
            lines_of_code: loc,
            parameters: func.params.len(),
            max_nesting_depth: max_nesting,
            has_type_annotations,
            return_type_annotated,
        })
    }

    fn calculate_module_metrics(&self, functions: &[FunctionMetrics]) -> ModuleMetrics {
        let total_functions = functions.len();
        let total_lines: usize = functions.iter().map(|f| f.lines_of_code).sum();

        let avg_cyclomatic = if total_functions > 0 {
            functions
                .iter()
                .map(|f| f.cyclomatic_complexity as f64)
                .sum::<f64>()
                / total_functions as f64
        } else {
            0.0
        };

        let max_cyclomatic = functions
            .iter()
            .map(|f| f.cyclomatic_complexity)
            .max()
            .unwrap_or(0);

        let avg_cognitive = if total_functions > 0 {
            functions
                .iter()
                .map(|f| f.cognitive_complexity as f64)
                .sum::<f64>()
                / total_functions as f64
        } else {
            0.0
        };

        let max_cognitive = functions
            .iter()
            .map(|f| f.cognitive_complexity)
            .max()
            .unwrap_or(0);

        ModuleMetrics {
            total_functions,
            total_lines,
            avg_cyclomatic_complexity: avg_cyclomatic,
            max_cyclomatic_complexity: max_cyclomatic,
            avg_cognitive_complexity: avg_cognitive,
            max_cognitive_complexity: max_cognitive,
        }
    }

    fn calculate_type_coverage(&self, module: &HirModule) -> TypeCoverage {
        let mut total_parameters = 0;
        let mut annotated_parameters = 0;
        let mut functions_with_return_type = 0;

        for func in &module.functions {
            total_parameters += func.params.len();
            annotated_parameters += func
                .params
                .iter()
                .filter(|param| !matches!(param.ty, depyler_core::hir::Type::Unknown))
                .count();

            if !matches!(func.ret_type, depyler_core::hir::Type::Unknown) {
                functions_with_return_type += 1;
            }
        }

        let total_annotations = annotated_parameters + functions_with_return_type;
        let total_possible = total_parameters + module.functions.len();
        let coverage_percentage = if total_possible > 0 {
            (total_annotations as f64 / total_possible as f64) * 100.0
        } else {
            100.0
        };

        TypeCoverage {
            total_parameters,
            annotated_parameters,
            total_functions: module.functions.len(),
            functions_with_return_type,
            coverage_percentage,
        }
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use depyler_core::hir::*;

    fn create_test_function() -> HirFunction {
        use smallvec::smallvec;
        HirFunction {
            name: "test_func".to_string(),
            params: smallvec![
                HirParam {
                    name: Symbol::from("x"),
                    ty: Type::Int,
                    default: None,
                    is_vararg: false,
                },
                HirParam {
                    name: Symbol::from("y"),
                    ty: Type::String,
                    default: None,
                    is_vararg: false,
                }
            ],
            ret_type: Type::Int,
            body: vec![HirStmt::Return(Some(HirExpr::Literal(Literal::Int(42))))],
            properties: FunctionProperties::default(),
            annotations: TranspilationAnnotations::default(),
            docstring: None,
        }
    }

    #[test]
    fn test_analyzer_creation() {
        let analyzer = Analyzer::new();
        assert!(analyzer.enable_type_inference);

        let default_analyzer = Analyzer::default();
        assert!(default_analyzer.enable_type_inference);
    }

    #[test]
    fn test_analyze_empty_module() {
        let analyzer = Analyzer::new();
        let module = HirModule {
            functions: vec![],
            imports: vec![],
            type_aliases: vec![],
            protocols: vec![],
            classes: vec![],
            constants: vec![],
            top_level_stmts: vec![],
        };

        let result = analyzer.analyze(&module).unwrap();
        assert_eq!(result.module_metrics.total_functions, 0);
        assert_eq!(result.function_metrics.len(), 0);
        assert_eq!(result.type_coverage.total_functions, 0);
        assert_eq!(result.type_coverage.coverage_percentage, 100.0);
    }

    #[test]
    fn test_analyze_single_function() {
        let analyzer = Analyzer::new();
        let func = create_test_function();
        let module = HirModule {
            functions: vec![func],
            imports: vec![],
            type_aliases: vec![],
            protocols: vec![],
            classes: vec![],
            constants: vec![],
            top_level_stmts: vec![],
        };

        let result = analyzer.analyze(&module).unwrap();
        assert_eq!(result.module_metrics.total_functions, 1);
        assert_eq!(result.function_metrics.len(), 1);

        let func_metrics = &result.function_metrics[0];
        assert_eq!(func_metrics.name, "test_func");
        assert_eq!(func_metrics.parameters, 2);
        assert!(func_metrics.has_type_annotations);
        assert!(func_metrics.return_type_annotated);
    }

    #[test]
    fn test_type_coverage_calculation() {
        let analyzer = Analyzer::new();
        use smallvec::smallvec;
        let func_with_types = HirFunction {
            name: "typed_func".to_string(),
            params: smallvec![HirParam {
                name: Symbol::from("x"),
                ty: Type::Int,
                default: None,
                is_vararg: false,
            }],
            ret_type: Type::String,
            body: vec![],
            properties: FunctionProperties::default(),
            annotations: TranspilationAnnotations::default(),
            docstring: None,
        };

        let func_without_types = HirFunction {
            name: "untyped_func".to_string(),
            params: smallvec![HirParam {
                name: Symbol::from("y"),
                ty: Type::Unknown,
                default: None,
                is_vararg: false,
            }],
            ret_type: Type::Unknown,
            body: vec![],
            properties: FunctionProperties::default(),
            annotations: TranspilationAnnotations::default(),
            docstring: None,
        };

        let module = HirModule {
            functions: vec![func_with_types, func_without_types],
            imports: vec![],
            type_aliases: vec![],
            protocols: vec![],
            classes: vec![],
            constants: vec![],
            top_level_stmts: vec![],
        };

        let coverage = analyzer.calculate_type_coverage(&module);
        assert_eq!(coverage.total_parameters, 2);
        assert_eq!(coverage.annotated_parameters, 1);
        assert_eq!(coverage.total_functions, 2);
        assert_eq!(coverage.functions_with_return_type, 1);
        assert_eq!(coverage.coverage_percentage, 50.0); // 2 annotations out of 4 possible
    }

    // ========================================================================
    // Additional coverage tests
    // ========================================================================

    #[test]
    fn test_analyze_function_with_unknown_types() {
        use smallvec::smallvec;
        let analyzer = Analyzer::new();
        let func = HirFunction {
            name: "untyped".to_string(),
            params: smallvec![HirParam {
                name: Symbol::from("a"),
                ty: Type::Unknown,
                default: None,
                is_vararg: false,
            }],
            ret_type: Type::Unknown,
            body: vec![HirStmt::Return(None)],
            properties: FunctionProperties::default(),
            annotations: TranspilationAnnotations::default(),
            docstring: None,
        };
        let module = HirModule {
            functions: vec![func],
            imports: vec![],
            type_aliases: vec![],
            protocols: vec![],
            classes: vec![],
            constants: vec![],
            top_level_stmts: vec![],
        };
        let result = analyzer.analyze(&module).unwrap();
        let fm = &result.function_metrics[0];
        assert!(!fm.has_type_annotations);
        assert!(!fm.return_type_annotated);
    }

    #[test]
    fn test_analyze_multiple_functions() {
        use smallvec::smallvec;
        let analyzer = Analyzer::new();
        let func1 = create_test_function();
        let func2 = HirFunction {
            name: "func2".to_string(),
            params: smallvec![],
            ret_type: Type::Bool,
            body: vec![
                HirStmt::If {
                    condition: HirExpr::Literal(Literal::Bool(true)),
                    then_body: vec![HirStmt::Return(Some(HirExpr::Literal(Literal::Bool(
                        true,
                    ))))],
                    else_body: Some(vec![HirStmt::Return(Some(HirExpr::Literal(
                        Literal::Bool(false),
                    )))]),
                },
            ],
            properties: FunctionProperties::default(),
            annotations: TranspilationAnnotations::default(),
            docstring: None,
        };
        let module = HirModule {
            functions: vec![func1, func2],
            imports: vec![],
            type_aliases: vec![],
            protocols: vec![],
            classes: vec![],
            constants: vec![],
            top_level_stmts: vec![],
        };
        let result = analyzer.analyze(&module).unwrap();
        assert_eq!(result.module_metrics.total_functions, 2);
        assert!(result.module_metrics.max_cyclomatic_complexity >= 2);
    }

    #[test]
    fn test_analysis_result_serialization() {
        let result = AnalysisResult {
            module_metrics: ModuleMetrics {
                total_functions: 1,
                total_lines: 10,
                avg_cyclomatic_complexity: 2.0,
                max_cyclomatic_complexity: 2,
                avg_cognitive_complexity: 1.0,
                max_cognitive_complexity: 1,
            },
            function_metrics: vec![FunctionMetrics {
                name: "test".to_string(),
                cyclomatic_complexity: 2,
                cognitive_complexity: 1,
                lines_of_code: 10,
                parameters: 1,
                max_nesting_depth: 1,
                has_type_annotations: true,
                return_type_annotated: true,
            }],
            type_coverage: TypeCoverage {
                total_parameters: 1,
                annotated_parameters: 1,
                total_functions: 1,
                functions_with_return_type: 1,
                coverage_percentage: 100.0,
            },
        };
        let json = serde_json::to_string(&result).unwrap();
        assert!(json.contains("test"));
        let deserialized: AnalysisResult = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.module_metrics.total_functions, 1);
    }

    #[test]
    fn test_module_metrics_debug_clone() {
        let metrics = ModuleMetrics {
            total_functions: 5,
            total_lines: 50,
            avg_cyclomatic_complexity: 3.0,
            max_cyclomatic_complexity: 8,
            avg_cognitive_complexity: 2.5,
            max_cognitive_complexity: 6,
        };
        let debug = format!("{:?}", metrics);
        assert!(debug.contains("ModuleMetrics"));
        let cloned = metrics.clone();
        assert_eq!(cloned.total_functions, 5);
    }

    #[test]
    fn test_function_metrics_debug_clone() {
        let fm = FunctionMetrics {
            name: "my_func".to_string(),
            cyclomatic_complexity: 3,
            cognitive_complexity: 2,
            lines_of_code: 15,
            parameters: 2,
            max_nesting_depth: 3,
            has_type_annotations: false,
            return_type_annotated: true,
        };
        let debug = format!("{:?}", fm);
        assert!(debug.contains("my_func"));
        let cloned = fm.clone();
        assert_eq!(cloned.name, "my_func");
    }

    #[test]
    fn test_type_coverage_debug_clone() {
        let tc = TypeCoverage {
            total_parameters: 10,
            annotated_parameters: 7,
            total_functions: 5,
            functions_with_return_type: 3,
            coverage_percentage: 66.67,
        };
        let debug = format!("{:?}", tc);
        assert!(debug.contains("TypeCoverage"));
        let cloned = tc.clone();
        assert_eq!(cloned.total_parameters, 10);
    }

    #[test]
    fn test_module_metrics_empty_functions() {
        let analyzer = Analyzer::new();
        let empty: Vec<FunctionMetrics> = vec![];
        let metrics = analyzer.calculate_module_metrics(&empty);
        assert_eq!(metrics.total_functions, 0);
        assert_eq!(metrics.total_lines, 0);
        assert_eq!(metrics.avg_cyclomatic_complexity, 0.0);
        assert_eq!(metrics.max_cyclomatic_complexity, 0);
        assert_eq!(metrics.avg_cognitive_complexity, 0.0);
        assert_eq!(metrics.max_cognitive_complexity, 0);
    }

    #[test]
    fn test_type_coverage_all_annotated() {
        use smallvec::smallvec;
        let analyzer = Analyzer::new();
        let func = HirFunction {
            name: "all_typed".to_string(),
            params: smallvec![
                HirParam {
                    name: Symbol::from("a"),
                    ty: Type::Int,
                    default: None,
                    is_vararg: false,
                },
                HirParam {
                    name: Symbol::from("b"),
                    ty: Type::String,
                    default: None,
                    is_vararg: false,
                },
            ],
            ret_type: Type::Bool,
            body: vec![],
            properties: FunctionProperties::default(),
            annotations: TranspilationAnnotations::default(),
            docstring: None,
        };
        let module = HirModule {
            functions: vec![func],
            imports: vec![],
            type_aliases: vec![],
            protocols: vec![],
            classes: vec![],
            constants: vec![],
            top_level_stmts: vec![],
        };
        let coverage = analyzer.calculate_type_coverage(&module);
        assert_eq!(coverage.coverage_percentage, 100.0);
    }

    // ========================================================================
    // S9B7: Additional coverage tests for analyzer edge cases
    // ========================================================================

    #[test]
    fn test_s9b7_analyze_function_with_nested_if() {
        use smallvec::smallvec;
        let analyzer = Analyzer::new();
        let func = HirFunction {
            name: "nested".to_string(),
            params: smallvec![],
            ret_type: Type::Int,
            body: vec![HirStmt::If {
                condition: HirExpr::Literal(Literal::Bool(true)),
                then_body: vec![HirStmt::If {
                    condition: HirExpr::Literal(Literal::Bool(false)),
                    then_body: vec![HirStmt::Return(Some(HirExpr::Literal(Literal::Int(1))))],
                    else_body: None,
                }],
                else_body: Some(vec![HirStmt::Return(Some(HirExpr::Literal(
                    Literal::Int(2),
                )))]),
            }],
            properties: FunctionProperties::default(),
            annotations: TranspilationAnnotations::default(),
            docstring: None,
        };
        let module = HirModule {
            functions: vec![func],
            imports: vec![],
            type_aliases: vec![],
            protocols: vec![],
            classes: vec![],
            constants: vec![],
            top_level_stmts: vec![],
        };
        let result = analyzer.analyze(&module).unwrap();
        let fm = &result.function_metrics[0];
        assert!(fm.cyclomatic_complexity >= 3);
        assert!(fm.max_nesting_depth >= 2);
    }

    #[test]
    fn test_s9b7_type_coverage_no_functions() {
        let analyzer = Analyzer::new();
        let module = HirModule {
            functions: vec![],
            imports: vec![],
            type_aliases: vec![],
            protocols: vec![],
            classes: vec![],
            constants: vec![],
            top_level_stmts: vec![],
        };
        let coverage = analyzer.calculate_type_coverage(&module);
        assert_eq!(coverage.total_parameters, 0);
        assert_eq!(coverage.annotated_parameters, 0);
        assert_eq!(coverage.total_functions, 0);
        assert_eq!(coverage.functions_with_return_type, 0);
        assert_eq!(coverage.coverage_percentage, 100.0);
    }

    #[test]
    fn test_s9b7_analyze_function_empty_body() {
        use smallvec::smallvec;
        let analyzer = Analyzer::new();
        let func = HirFunction {
            name: "empty_body".to_string(),
            params: smallvec![],
            ret_type: Type::None,
            body: vec![],
            properties: FunctionProperties::default(),
            annotations: TranspilationAnnotations::default(),
            docstring: None,
        };
        let result = analyzer.analyze_function(&func).unwrap();
        assert_eq!(result.name, "empty_body");
        assert_eq!(result.lines_of_code, 0);
        assert_eq!(result.parameters, 0);
        assert_eq!(result.max_nesting_depth, 0);
        assert!(result.has_type_annotations);
        assert!(result.return_type_annotated);
    }

    #[test]
    fn test_s9b7_type_coverage_mixed_annotations() {
        use smallvec::smallvec;
        let analyzer = Analyzer::new();
        let func = HirFunction {
            name: "mixed".to_string(),
            params: smallvec![
                HirParam {
                    name: Symbol::from("a"),
                    ty: Type::Int,
                    default: None,
                    is_vararg: false,
                },
                HirParam {
                    name: Symbol::from("b"),
                    ty: Type::Unknown,
                    default: None,
                    is_vararg: false,
                },
                HirParam {
                    name: Symbol::from("c"),
                    ty: Type::Float,
                    default: None,
                    is_vararg: false,
                },
            ],
            ret_type: Type::Unknown,
            body: vec![],
            properties: FunctionProperties::default(),
            annotations: TranspilationAnnotations::default(),
            docstring: None,
        };
        let module = HirModule {
            functions: vec![func],
            imports: vec![],
            type_aliases: vec![],
            protocols: vec![],
            classes: vec![],
            constants: vec![],
            top_level_stmts: vec![],
        };
        let coverage = analyzer.calculate_type_coverage(&module);
        assert_eq!(coverage.total_parameters, 3);
        assert_eq!(coverage.annotated_parameters, 2);
        assert_eq!(coverage.functions_with_return_type, 0);
        // 2 annotated params + 0 return types out of 3 params + 1 function = 2/4 = 50%
        assert!((coverage.coverage_percentage - 50.0).abs() < 0.01);
    }

    #[test]
    fn test_s9b7_module_metrics_single_function() {
        let analyzer = Analyzer::new();
        let metrics = vec![FunctionMetrics {
            name: "solo".to_string(),
            cyclomatic_complexity: 5,
            cognitive_complexity: 3,
            lines_of_code: 10,
            parameters: 2,
            max_nesting_depth: 1,
            has_type_annotations: true,
            return_type_annotated: true,
        }];
        let mm = analyzer.calculate_module_metrics(&metrics);
        assert_eq!(mm.total_functions, 1);
        assert_eq!(mm.total_lines, 10);
        assert_eq!(mm.avg_cyclomatic_complexity, 5.0);
        assert_eq!(mm.max_cyclomatic_complexity, 5);
        assert_eq!(mm.avg_cognitive_complexity, 3.0);
        assert_eq!(mm.max_cognitive_complexity, 3);
    }

    #[test]
    fn test_s9b7_analysis_result_debug_clone() {
        let result = AnalysisResult {
            module_metrics: ModuleMetrics {
                total_functions: 2,
                total_lines: 20,
                avg_cyclomatic_complexity: 3.0,
                max_cyclomatic_complexity: 4,
                avg_cognitive_complexity: 2.0,
                max_cognitive_complexity: 3,
            },
            function_metrics: vec![],
            type_coverage: TypeCoverage {
                total_parameters: 0,
                annotated_parameters: 0,
                total_functions: 0,
                functions_with_return_type: 0,
                coverage_percentage: 100.0,
            },
        };
        let debug = format!("{:?}", result);
        assert!(debug.contains("AnalysisResult"));
        let cloned = result.clone();
        assert_eq!(cloned.module_metrics.total_functions, 2);
    }

    #[test]
    fn test_s9b7_analyze_function_partial_type_annotations() {
        use smallvec::smallvec;
        let analyzer = Analyzer::new();
        let func = HirFunction {
            name: "partial".to_string(),
            params: smallvec![
                HirParam {
                    name: Symbol::from("x"),
                    ty: Type::Int,
                    default: None,
                    is_vararg: false,
                },
                HirParam {
                    name: Symbol::from("y"),
                    ty: Type::Unknown,
                    default: None,
                    is_vararg: false,
                },
            ],
            ret_type: Type::String,
            body: vec![],
            properties: FunctionProperties::default(),
            annotations: TranspilationAnnotations::default(),
            docstring: None,
        };
        let result = analyzer.analyze_function(&func).unwrap();
        // Not all params have type annotations
        assert!(!result.has_type_annotations);
        assert!(result.return_type_annotated);
    }

    #[test]
    fn test_module_metrics_calculation() {
        let analyzer = Analyzer::new();
        let metrics = vec![
            FunctionMetrics {
                name: "func1".to_string(),
                cyclomatic_complexity: 2,
                cognitive_complexity: 3,
                lines_of_code: 5,
                parameters: 1,
                max_nesting_depth: 1,
                has_type_annotations: true,
                return_type_annotated: true,
            },
            FunctionMetrics {
                name: "func2".to_string(),
                cyclomatic_complexity: 4,
                cognitive_complexity: 6,
                lines_of_code: 10,
                parameters: 2,
                max_nesting_depth: 2,
                has_type_annotations: false,
                return_type_annotated: false,
            },
        ];

        let module_metrics = analyzer.calculate_module_metrics(&metrics);
        assert_eq!(module_metrics.total_functions, 2);
        assert_eq!(module_metrics.total_lines, 15);
        assert_eq!(module_metrics.avg_cyclomatic_complexity, 3.0);
        assert_eq!(module_metrics.max_cyclomatic_complexity, 4);
        assert_eq!(module_metrics.avg_cognitive_complexity, 4.5);
        assert_eq!(module_metrics.max_cognitive_complexity, 6);
    }
}