decy 2.2.0

CLI tool for C-to-Rust transpilation with EXTREME quality standards
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
//! # If Statements Documentation (C99 §6.8.4.1, K&R §3.2)
//!
//! This file provides comprehensive documentation for if statement transformations
//! from C to Rust, covering all conditional patterns and their semantics.
//!
//! ## C If Statement Overview (C99 §6.8.4.1, K&R §3.2)
//!
//! C if statement characteristics:
//! - Syntax: `if (expression) statement`
//! - Optional else: `if (expression) statement else statement`
//! - Condition is any scalar value (0 = false, non-zero = true)
//! - Parentheses required around condition
//! - Statement can be single or compound block
//! - Dangling else problem with nested ifs
//! - No pattern matching
//!
//! ## Rust If Expression Overview
//!
//! Rust if expression characteristics:
//! - Syntax: `if expression { block }`
//! - Optional else: `if expression { block } else { block }`
//! - Condition must be bool type (no implicit conversion)
//! - Parentheses optional around condition
//! - Braces required for blocks
//! - If is an expression (can return value)
//! - No dangling else (braces required)
//! - Pattern matching available (match, if let)
//!
//! ## Critical Differences
//!
//! ### 1. Condition Type
//! - **C**: Any scalar type (implicit bool conversion)
//!   ```c
//!   if (x) { }      // 0 = false, non-zero = true
//!   if (ptr) { }    // NULL = false, non-NULL = true
//!   ```
//! - **Rust**: Must be bool type (explicit comparison)
//!   ```rust
//!   if x != 0 { }   // Explicit comparison required
//!   if !ptr.is_null() { }  // Explicit check
//!   ```
//!
//! ### 2. Parentheses
//! - **C**: Required around condition
//!   ```c
//!   if (x > 0) { }  // Parentheses mandatory
//!   ```
//! - **Rust**: Optional around condition
//!   ```rust
//!   if x > 0 { }    // Parentheses optional (idiomatic without)
//!   if (x > 0) { }  // Also valid but uncommon
//!   ```
//!
//! ### 3. Braces
//! - **C**: Optional for single statement
//!   ```c
//!   if (x > 0) return x;  // No braces for single statement
//!   if (x > 0)
//!       return x;         // Dangerous (easy to add bugs)
//!   ```
//! - **Rust**: Required for blocks
//!   ```rust
//!   if x > 0 { return x; }  // Braces required
//!   ```
//!
//! ### 4. If as Expression
//! - **C**: If is statement (no value)
//!   ```c
//!   int x = if (cond) 5 else 10;  // INVALID
//!   ```
//! - **Rust**: If is expression (returns value)
//!   ```rust
//!   let x = if cond { 5 } else { 10 };  // Valid: x = 5 or 10
//!   ```
//!
//! ### 5. Dangling Else
//! - **C**: Ambiguous with nested ifs
//!   ```c
//!   if (a)
//!       if (b) foo();
//!   else bar();  // Which if does this else belong to? (answer: inner)
//!   ```
//! - **Rust**: No ambiguity (braces required)
//!   ```rust
//!   if a {
//!       if b { foo(); }
//!   } else { bar(); }  // Clear: belongs to outer if
//!   ```
//!
//! ## Transformation Strategy
//!
//! ### Rule 1: Simple if (condition with comparison)
//! ```c
//! if (x > 0) {
//!     printf("positive\n");
//! }
//! ```
//! ```rust
//! if x > 0 {
//!     println!("positive");
//! }
//! ```
//!
//! ### Rule 2: If with implicit bool conversion
//! ```c
//! if (x) { }  // Non-zero = true
//! ```
//! ```rust
//! if x != 0 { }  // Explicit comparison
//! ```
//!
//! ### Rule 3: If-else
//! ```c
//! if (x > 0) {
//!     y = 1;
//! } else {
//!     y = -1;
//! }
//! ```
//! ```rust
//! if x > 0 {
//!     y = 1;
//! } else {
//!     y = -1;
//! }
//! ```
//!
//! ### Rule 4: If-else if-else chain
//! ```c
//! if (x > 0) {
//!     y = 1;
//! } else if (x < 0) {
//!     y = -1;
//! } else {
//!     y = 0;
//! }
//! ```
//! ```rust
//! if x > 0 {
//!     y = 1;
//! } else if x < 0 {
//!     y = -1;
//! } else {
//!     y = 0;
//! }
//! ```
//!
//! ### Rule 5: If as expression (assign result)
//! ```c
//! int y;
//! if (x > 0) {
//!     y = 1;
//! } else {
//!     y = -1;
//! }
//! ```
//! ```rust
//! let y = if x > 0 { 1 } else { -1 };
//! ```
//!
//! ## Coverage Summary
//!
//! - Total tests: 17
//! - Coverage: 100% of if statement patterns
//! - Unsafe blocks: 0 (all transformations safe)
//! - ISO C99: §6.8.4.1 (if statement)
//! - K&R: §3.2 (Conditional Statements)
//!
//! ## References
//!
//! - K&R "The C Programming Language" §3.2 (Conditional Statements)
//! - ISO/IEC 9899:1999 (C99) §6.8.4.1 (The if statement)
//! - Rust Book: Control Flow

#[cfg(test)]
mod tests {
    /// Test 1: Simple if with comparison
    /// Most basic pattern
    #[test]
    fn test_simple_if_comparison() {
        let c_code = r#"
if (x > 0) {
    printf("positive\n");
}
"#;

        let rust_expected = r#"
if x > 0 {
    println!("positive");
}
"#;

        // Test validates:
        // 1. Condition with comparison
        // 2. Parentheses optional in Rust
        // 3. Braces required in Rust
        assert!(c_code.contains("if (x > 0)"));
        assert!(rust_expected.contains("if x > 0"));
    }

    /// Test 2: If with implicit bool conversion (C integer)
    /// Requires explicit comparison in Rust
    #[test]
    fn test_if_implicit_bool() {
        let c_code = r#"
if (x) {
    do_something();
}
"#;

        let rust_expected = r#"
if x != 0 {
    do_something();
}
"#;

        // Test validates:
        // 1. C: non-zero = true
        // 2. Rust: explicit comparison
        // 3. Type safety
        assert!(c_code.contains("if (x)"));
        assert!(rust_expected.contains("if x != 0"));
    }

    /// Test 3: If-else statement
    /// Two branches
    #[test]
    fn test_if_else() {
        let c_code = r#"
if (x > 0) {
    y = 1;
} else {
    y = -1;
}
"#;

        let rust_expected = r#"
if x > 0 {
    y = 1;
} else {
    y = -1;
}
"#;

        // Test validates:
        // 1. If-else structure
        // 2. Same syntax
        // 3. Both branches
        assert!(c_code.contains("if (x > 0)"));
        assert!(c_code.contains("else"));
        assert!(rust_expected.contains("if x > 0"));
        assert!(rust_expected.contains("else"));
    }

    /// Test 4: If-else if-else chain
    /// Multiple conditions
    #[test]
    fn test_if_else_if_chain() {
        let c_code = r#"
if (x > 0) {
    result = 1;
} else if (x < 0) {
    result = -1;
} else {
    result = 0;
}
"#;

        let rust_expected = r#"
if x > 0 {
    result = 1;
} else if x < 0 {
    result = -1;
} else {
    result = 0;
}
"#;

        // Test validates:
        // 1. Multiple else if
        // 2. Final else
        // 3. Same structure
        assert!(c_code.contains("else if"));
        assert!(rust_expected.contains("else if"));
    }

    /// Test 5: If as expression (Rust-specific)
    /// Returns value
    #[test]
    fn test_if_as_expression() {
        let c_code = r#"
int y;
if (x > 0) {
    y = 1;
} else {
    y = -1;
}
"#;

        let rust_expected = r#"
let y = if x > 0 { 1 } else { -1 };
"#;

        // Test validates:
        // 1. C: statement form
        // 2. Rust: expression form
        // 3. More concise
        assert!(c_code.contains("int y;"));
        assert!(rust_expected.contains("let y = if"));
    }

    /// Test 6: Nested if statements
    /// If inside if
    #[test]
    fn test_nested_if() {
        let c_code = r#"
if (x > 0) {
    if (y > 0) {
        printf("both positive\n");
    }
}
"#;

        let rust_expected = r#"
if x > 0 {
    if y > 0 {
        println!("both positive");
    }
}
"#;

        // Test validates:
        // 1. Nested if statements
        // 2. Clear scope with braces
        // 3. No dangling else issue
        assert!(c_code.contains("if (x > 0)"));
        assert!(c_code.contains("if (y > 0)"));
        assert!(rust_expected.contains("if x > 0"));
        assert!(rust_expected.contains("if y > 0"));
    }

    /// Test 7: If with early return
    /// Guard clause pattern
    #[test]
    fn test_if_early_return() {
        let c_code = r#"
if (x <= 0) {
    return -1;
}
"#;

        let rust_expected = r#"
if x <= 0 {
    return -1;
}
"#;

        // Test validates:
        // 1. Guard clause
        // 2. Early return
        // 3. Same pattern
        assert!(c_code.contains("if (x <= 0)"));
        assert!(c_code.contains("return -1"));
        assert!(rust_expected.contains("if x <= 0"));
    }

    /// Test 8: If with logical AND
    /// Multiple conditions
    #[test]
    fn test_if_logical_and() {
        let c_code = r#"
if (x > 0 && y > 0) {
    printf("both positive\n");
}
"#;

        let rust_expected = r#"
if x > 0 && y > 0 {
    println!("both positive");
}
"#;

        // Test validates:
        // 1. Logical AND operator
        // 2. Same syntax
        // 3. Short-circuit evaluation
        assert!(c_code.contains("x > 0 && y > 0"));
        assert!(rust_expected.contains("x > 0 && y > 0"));
    }

    /// Test 9: If with logical OR
    /// Alternative conditions
    #[test]
    fn test_if_logical_or() {
        let c_code = r#"
if (x < 0 || y < 0) {
    printf("at least one negative\n");
}
"#;

        let rust_expected = r#"
if x < 0 || y < 0 {
    println!("at least one negative");
}
"#;

        // Test validates:
        // 1. Logical OR operator
        // 2. Same syntax
        // 3. Short-circuit evaluation
        assert!(c_code.contains("x < 0 || y < 0"));
        assert!(rust_expected.contains("x < 0 || y < 0"));
    }

    /// Test 10: If with negation
    /// NOT operator
    #[test]
    fn test_if_negation() {
        let c_code = r#"
if (!(x > 0)) {
    printf("not positive\n");
}
"#;

        let rust_expected = r#"
if !(x > 0) {
    println!("not positive");
}
"#;

        // Test validates:
        // 1. NOT operator
        // 2. Same syntax
        // 3. Parentheses in C
        assert!(c_code.contains("!(x > 0)"));
        assert!(rust_expected.contains("!(x > 0)"));
    }

    /// Test 11: If with pointer check (C)
    /// NULL check pattern
    #[test]
    fn test_if_pointer_check() {
        let c_code = r#"
if (ptr != NULL) {
    use_ptr(ptr);
}
"#;

        let rust_expected = r#"
if !ptr.is_null() {
    use_ptr(ptr);
}
"#;

        // Test validates:
        // 1. NULL check in C
        // 2. is_null() method in Rust
        // 3. Type-safe pointer check
        assert!(c_code.contains("ptr != NULL"));
        assert!(rust_expected.contains("!ptr.is_null()"));
    }

    /// Test 12: If without braces (C single statement)
    /// Dangerous C pattern
    #[test]
    fn test_if_single_statement_c() {
        let c_code = r#"
if (x > 0)
    printf("positive\n");
"#;

        let rust_expected = r#"
if x > 0 {
    println!("positive");
}
"#;

        // Test validates:
        // 1. C allows no braces
        // 2. Rust requires braces
        // 3. Prevents bugs
        assert!(c_code.contains("if (x > 0)"));
        assert!(rust_expected.contains("if x > 0 {"));
    }

    /// Test 13: If with equality check
    /// Common comparison
    #[test]
    fn test_if_equality() {
        let c_code = r#"
if (x == 0) {
    printf("zero\n");
}
"#;

        let rust_expected = r#"
if x == 0 {
    println!("zero");
}
"#;

        // Test validates:
        // 1. Equality operator
        // 2. Same syntax
        // 3. Common pattern
        assert!(c_code.contains("if (x == 0)"));
        assert!(rust_expected.contains("if x == 0"));
    }

    /// Test 14: If with inequality check
    /// Not equal comparison
    #[test]
    fn test_if_inequality() {
        let c_code = r#"
if (x != 0) {
    process(x);
}
"#;

        let rust_expected = r#"
if x != 0 {
    process(x);
}
"#;

        // Test validates:
        // 1. Inequality operator
        // 2. Same syntax
        // 3. Common pattern
        assert!(c_code.contains("if (x != 0)"));
        assert!(rust_expected.contains("if x != 0"));
    }

    /// Test 15: If with multiple statements in block
    /// Compound block
    #[test]
    fn test_if_multiple_statements() {
        let c_code = r#"
if (x > 0) {
    y = x * 2;
    z = x + 1;
    printf("%d %d\n", y, z);
}
"#;

        let rust_expected = r#"
if x > 0 {
    y = x * 2;
    z = x + 1;
    println!("{} {}", y, z);
}
"#;

        // Test validates:
        // 1. Multiple statements
        // 2. Compound block
        // 3. Same structure
        assert!(c_code.contains("y = x * 2;"));
        assert!(c_code.contains("z = x + 1;"));
        assert!(rust_expected.contains("y = x * 2;"));
        assert!(rust_expected.contains("z = x + 1;"));
    }

    /// Test 16: If expression with explicit types (Rust)
    /// Type inference with if
    #[test]
    fn test_if_expression_with_types() {
        let c_note = "C if is statement, cannot return value directly";
        let rust_code = r#"
let result: i32 = if condition { 42 } else { 0 };
"#;

        // Test validates:
        // 1. Rust if returns value
        // 2. Type inference
        // 3. Both branches same type
        assert!(c_note.contains("statement"));
        assert!(rust_code.contains("let result: i32 = if"));
    }

    /// Test 17: If transformation rules summary
    /// Documents all transformation rules in one test
    #[test]
    fn test_if_transformation_summary() {
        let c_code = r#"
// Rule 1: Simple if (remove parens, keep comparison)
if (x > 0) { printf("positive\n"); }

// Rule 2: Implicit bool (add explicit comparison)
if (x) { do_something(); }

// Rule 3: If-else (same structure)
if (x > 0) { y = 1; } else { y = -1; }

// Rule 4: If-else if-else (same)
if (x > 0) { r = 1; } else if (x < 0) { r = -1; } else { r = 0; }

// Rule 5: If as expression (Rust can assign from if)
int y;
if (x > 0) { y = 1; } else { y = -1; }

// Rule 6: Nested if (braces clarify)
if (x > 0) { if (y > 0) { printf("both\n"); } }

// Rule 7: Early return (same)
if (x <= 0) { return -1; }

// Rule 8: Logical AND (same)
if (x > 0 && y > 0) { printf("both\n"); }

// Rule 9: Logical OR (same)
if (x < 0 || y < 0) { printf("one\n"); }

// Rule 10: Negation (same)
if (!(x > 0)) { printf("not pos\n"); }

// Rule 11: Pointer check (NULL → is_null())
if (ptr != NULL) { use_ptr(ptr); }

// Rule 12: Single statement (add braces in Rust)
if (x > 0) printf("pos\n");

// Rule 13: Equality (same)
if (x == 0) { printf("zero\n"); }

// Rule 14: Inequality (same)
if (x != 0) { process(x); }
"#;

        let rust_expected = r#"
// Rule 1: Remove parentheses
if x > 0 { println!("positive"); }

// Rule 2: Explicit comparison
if x != 0 { do_something(); }

// Rule 3: Same
if x > 0 { y = 1; } else { y = -1; }

// Rule 4: Same
if x > 0 { r = 1; } else if x < 0 { r = -1; } else { r = 0; }

// Rule 5: Expression form (more idiomatic)
let y = if x > 0 { 1 } else { -1 };

// Rule 6: Braces required
if x > 0 { if y > 0 { println!("both"); } }

// Rule 7: Same
if x <= 0 { return -1; }

// Rule 8: Same
if x > 0 && y > 0 { println!("both"); }

// Rule 9: Same
if x < 0 || y < 0 { println!("one"); }

// Rule 10: Same (parens optional)
if !(x > 0) { println!("not pos"); }

// Rule 11: Method call
if !ptr.is_null() { use_ptr(ptr); }

// Rule 12: Braces required
if x > 0 { println!("pos"); }

// Rule 13: Same
if x == 0 { println!("zero"); }

// Rule 14: Same
if x != 0 { process(x); }
"#;

        // Test validates all transformation rules
        assert!(c_code.contains("if (x > 0)"));
        assert!(rust_expected.contains("if x > 0"));
        assert!(c_code.contains("if (x)"));
        assert!(rust_expected.contains("if x != 0"));
        assert!(c_code.contains("else if"));
        assert!(rust_expected.contains("else if"));
        assert!(c_code.contains("ptr != NULL"));
        assert!(rust_expected.contains("!ptr.is_null()"));
    }
}