rust-diff-analyzer 2.0.1

Semantic analyzer for Rust PR diffs that distinguishes production code from test code
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
// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
// SPDX-License-Identifier: MIT

use proc_macro2::{Span, TokenStream, TokenTree};
use syn::{
    Attribute, File, ImplItem, ItemConst, ItemEnum, ItemFn, ItemImpl, ItemMacro, ItemMod,
    ItemStatic, ItemStruct, ItemTrait, ItemType, TraitItem, Visibility as SynVisibility,
    spanned::Spanned, visit::Visit,
};

use crate::types::{LineSpan, SemanticUnit, SemanticUnitKind, Visibility};

/// Checks whether a `cfg` predicate token stream enables the item for test
/// builds.
///
/// Matches a bare `test` ident at any nesting level except inside `not(...)`,
/// so `#[cfg(test)]` and `#[cfg(any(test, unix))]` match while
/// `#[cfg(not(test))]` and `#[cfg(feature = "latest")]` do not.
fn cfg_predicate_enables_test(tokens: TokenStream, inside_not: bool) -> bool {
    let mut last_ident: Option<String> = None;

    for tree in tokens {
        match tree {
            TokenTree::Ident(ident) => {
                let name = ident.to_string();
                if name == "test" && !inside_not {
                    return true;
                }
                last_ident = Some(name);
            }
            TokenTree::Group(group) => {
                let inner_not = inside_not || last_ident.as_deref() == Some("not");
                if cfg_predicate_enables_test(group.stream(), inner_not) {
                    return true;
                }
                last_ident = None;
            }
            _ => {}
        }
    }

    false
}

/// Collects `feature = "name"` values from a `cfg` predicate token stream,
/// skipping features negated via `not(...)`.
fn collect_cfg_features(tokens: TokenStream, inside_not: bool, out: &mut Vec<String>) {
    let mut last_ident: Option<String> = None;
    let mut after_feature_eq = false;

    for tree in tokens {
        match tree {
            TokenTree::Ident(ident) => {
                last_ident = Some(ident.to_string());
                after_feature_eq = false;
            }
            TokenTree::Punct(punct) => {
                after_feature_eq =
                    punct.as_char() == '=' && last_ident.as_deref() == Some("feature");
            }
            TokenTree::Literal(literal) => {
                if after_feature_eq && !inside_not {
                    let raw = literal.to_string();
                    out.push(raw.trim_matches('"').to_string());
                }
                after_feature_eq = false;
                last_ident = None;
            }
            TokenTree::Group(group) => {
                let inner_not = inside_not || last_ident.as_deref() == Some("not");
                collect_cfg_features(group.stream(), inner_not, out);
                last_ident = None;
                after_feature_eq = false;
            }
        }
    }
}

/// Visitor for extracting semantic units from Rust AST
pub struct SemanticUnitVisitor {
    units: Vec<SemanticUnit>,
    in_test_module: bool,
    current_impl_name: Option<String>,
    current_trait_visibility: Option<Visibility>,
}

impl SemanticUnitVisitor {
    /// Creates a new semantic unit visitor
    ///
    /// # Returns
    ///
    /// A new SemanticUnitVisitor instance
    ///
    /// # Examples
    ///
    /// ```
    /// use rust_diff_analyzer::analysis::ast_visitor::SemanticUnitVisitor;
    ///
    /// let visitor = SemanticUnitVisitor::new();
    /// ```
    pub fn new() -> Self {
        Self {
            units: Vec::new(),
            in_test_module: false,
            current_impl_name: None,
            current_trait_visibility: None,
        }
    }

    /// Extracts semantic units from a parsed AST
    ///
    /// # Arguments
    ///
    /// * `file` - Parsed syn File
    ///
    /// # Returns
    ///
    /// Vector of extracted semantic units
    ///
    /// # Examples
    ///
    /// ```
    /// use rust_diff_analyzer::analysis::ast_visitor::SemanticUnitVisitor;
    ///
    /// let code = "fn main() {}";
    /// let file = syn::parse_file(code).unwrap();
    /// let units = SemanticUnitVisitor::extract(&file);
    /// assert_eq!(units.len(), 1);
    /// ```
    pub fn extract(file: &File) -> Vec<SemanticUnit> {
        let mut visitor = Self::new();
        visitor.visit_file(file);
        visitor.units
    }

    fn span_to_line_span(&self, span: Span) -> LineSpan {
        let start = span.start();
        let end = span.end();
        LineSpan::new(start.line, end.line)
    }

    fn convert_visibility(&self, vis: &SynVisibility) -> Visibility {
        match vis {
            SynVisibility::Public(_) => Visibility::Public,
            SynVisibility::Restricted(r) => {
                if r.path.is_ident("crate") {
                    Visibility::Crate
                } else {
                    Visibility::Restricted
                }
            }
            SynVisibility::Inherited => Visibility::Private,
        }
    }

    fn extract_attributes(&self, attrs: &[Attribute]) -> Vec<String> {
        let mut attributes = Vec::new();

        for attr in attrs {
            let path = attr.path();
            let name = path
                .segments
                .iter()
                .map(|s| s.ident.to_string())
                .collect::<Vec<_>>()
                .join("::");
            if !name.is_empty() {
                attributes.push(name);
            }

            if path.is_ident("cfg")
                && let Ok(meta) = attr.meta.require_list()
            {
                let mut features = Vec::new();
                collect_cfg_features(meta.tokens.clone(), false, &mut features);
                for feature in features {
                    attributes.push(format!("cfg_feature:{}", feature));
                }
            }
        }

        attributes
    }

    fn has_test_attribute(&self, attrs: &[Attribute]) -> bool {
        attrs.iter().any(|attr| {
            let path = attr.path();
            let last_is_test = path
                .segments
                .last()
                .map(|s| s.ident == "test" || s.ident == "bench")
                .unwrap_or(false);
            if last_is_test && !path.is_ident("cfg") {
                return true;
            }
            if path.is_ident("cfg")
                && let Ok(meta) = attr.meta.require_list()
            {
                return cfg_predicate_enables_test(meta.tokens.clone(), false);
            }
            false
        })
    }

    fn is_test_module(&self, attrs: &[Attribute]) -> bool {
        attrs.iter().any(|attr| {
            if attr.path().is_ident("cfg")
                && let Ok(meta) = attr.meta.require_list()
            {
                return cfg_predicate_enables_test(meta.tokens.clone(), false);
            }
            false
        })
    }

    fn add_unit(
        &mut self,
        kind: SemanticUnitKind,
        name: String,
        visibility: Visibility,
        span: Span,
        attrs: &[Attribute],
    ) {
        let mut attributes = self.extract_attributes(attrs);

        if self.in_test_module && !attributes.iter().any(|a| a == "cfg_test") {
            attributes.push("cfg_test".to_string());
        }

        if self.has_test_attribute(attrs) && !attributes.iter().any(|a| a == "test") {
            attributes.push("test".to_string());
        }

        let unit = match &self.current_impl_name {
            Some(impl_name) => SemanticUnit::with_impl(
                kind,
                name,
                impl_name.clone(),
                visibility,
                self.span_to_line_span(span),
                attributes,
            ),
            None => SemanticUnit::new(
                kind,
                name,
                visibility,
                self.span_to_line_span(span),
                attributes,
            ),
        };
        self.units.push(unit);
    }
}

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

impl<'ast> Visit<'ast> for SemanticUnitVisitor {
    fn visit_item_fn(&mut self, node: &'ast ItemFn) {
        self.add_unit(
            SemanticUnitKind::Function,
            node.sig.ident.to_string(),
            self.convert_visibility(&node.vis),
            node.span(),
            &node.attrs,
        );
        syn::visit::visit_item_fn(self, node);
    }

    fn visit_item_struct(&mut self, node: &'ast ItemStruct) {
        self.add_unit(
            SemanticUnitKind::Struct,
            node.ident.to_string(),
            self.convert_visibility(&node.vis),
            node.span(),
            &node.attrs,
        );
        syn::visit::visit_item_struct(self, node);
    }

    fn visit_item_enum(&mut self, node: &'ast ItemEnum) {
        self.add_unit(
            SemanticUnitKind::Enum,
            node.ident.to_string(),
            self.convert_visibility(&node.vis),
            node.span(),
            &node.attrs,
        );
        syn::visit::visit_item_enum(self, node);
    }

    fn visit_item_trait(&mut self, node: &'ast ItemTrait) {
        let visibility = self.convert_visibility(&node.vis);
        self.add_unit(
            SemanticUnitKind::Trait,
            node.ident.to_string(),
            visibility.clone(),
            node.span(),
            &node.attrs,
        );

        let previous_visibility = self.current_trait_visibility.replace(visibility);
        syn::visit::visit_item_trait(self, node);
        self.current_trait_visibility = previous_visibility;
    }

    fn visit_item_impl(&mut self, node: &'ast ItemImpl) {
        let impl_name = if let Some((_, path, _)) = &node.trait_ {
            format!(
                "{} for {}",
                path.segments
                    .last()
                    .map(|s| s.ident.to_string())
                    .unwrap_or_default(),
                type_to_string(&node.self_ty)
            )
        } else {
            type_to_string(&node.self_ty)
        };

        self.add_unit(
            SemanticUnitKind::Impl,
            impl_name.clone(),
            Visibility::Private,
            node.span(),
            &node.attrs,
        );

        let previous_impl_name = self.current_impl_name.take();
        self.current_impl_name = Some(impl_name);

        let was_in_test = self.in_test_module;
        if self.is_test_module(&node.attrs) {
            self.in_test_module = true;
        }

        for item in &node.items {
            match item {
                ImplItem::Fn(method) => {
                    self.add_unit(
                        SemanticUnitKind::Function,
                        method.sig.ident.to_string(),
                        self.convert_visibility(&method.vis),
                        method.span(),
                        &method.attrs,
                    );
                }
                ImplItem::Const(c) => {
                    self.add_unit(
                        SemanticUnitKind::Const,
                        c.ident.to_string(),
                        self.convert_visibility(&c.vis),
                        c.span(),
                        &c.attrs,
                    );
                }
                ImplItem::Type(t) => {
                    self.add_unit(
                        SemanticUnitKind::TypeAlias,
                        t.ident.to_string(),
                        self.convert_visibility(&t.vis),
                        t.span(),
                        &t.attrs,
                    );
                }
                _ => {}
            }
        }

        self.in_test_module = was_in_test;
        self.current_impl_name = previous_impl_name;
    }

    fn visit_item_const(&mut self, node: &'ast ItemConst) {
        self.add_unit(
            SemanticUnitKind::Const,
            node.ident.to_string(),
            self.convert_visibility(&node.vis),
            node.span(),
            &node.attrs,
        );
    }

    fn visit_item_static(&mut self, node: &'ast ItemStatic) {
        self.add_unit(
            SemanticUnitKind::Static,
            node.ident.to_string(),
            self.convert_visibility(&node.vis),
            node.span(),
            &node.attrs,
        );
    }

    fn visit_item_type(&mut self, node: &'ast ItemType) {
        self.add_unit(
            SemanticUnitKind::TypeAlias,
            node.ident.to_string(),
            self.convert_visibility(&node.vis),
            node.span(),
            &node.attrs,
        );
    }

    fn visit_item_macro(&mut self, node: &'ast ItemMacro) {
        if let Some(ident) = &node.ident {
            self.add_unit(
                SemanticUnitKind::Macro,
                ident.to_string(),
                Visibility::Private,
                node.span(),
                &node.attrs,
            );
        }
    }

    fn visit_item_mod(&mut self, node: &'ast ItemMod) {
        let is_test = self.is_test_module(&node.attrs) || node.ident == "tests";

        self.add_unit(
            SemanticUnitKind::Module,
            node.ident.to_string(),
            self.convert_visibility(&node.vis),
            node.span(),
            &node.attrs,
        );

        if let Some((_, items)) = &node.content {
            let was_in_test = self.in_test_module;
            self.in_test_module = is_test || was_in_test;

            for item in items {
                self.visit_item(item);
            }

            self.in_test_module = was_in_test;
        }
    }

    fn visit_trait_item(&mut self, node: &'ast TraitItem) {
        let visibility = self
            .current_trait_visibility
            .clone()
            .unwrap_or(Visibility::Public);

        match node {
            TraitItem::Fn(method) => {
                self.add_unit(
                    SemanticUnitKind::Function,
                    method.sig.ident.to_string(),
                    visibility,
                    method.span(),
                    &method.attrs,
                );
            }
            TraitItem::Const(c) => {
                self.add_unit(
                    SemanticUnitKind::Const,
                    c.ident.to_string(),
                    visibility,
                    c.span(),
                    &c.attrs,
                );
            }
            TraitItem::Type(t) => {
                self.add_unit(
                    SemanticUnitKind::TypeAlias,
                    t.ident.to_string(),
                    visibility,
                    t.span(),
                    &t.attrs,
                );
            }
            _ => {}
        }
        syn::visit::visit_trait_item(self, node);
    }
}

fn type_to_string(ty: &syn::Type) -> String {
    match ty {
        syn::Type::Path(p) => p
            .path
            .segments
            .last()
            .map(|s| s.ident.to_string())
            .unwrap_or_else(|| "Unknown".to_string()),
        _ => "Unknown".to_string(),
    }
}

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

    #[test]
    fn test_extract_function() {
        let code = "pub fn hello() {}";
        let file = syn::parse_file(code).expect("parse failed");
        let units = SemanticUnitVisitor::extract(&file);

        assert_eq!(units.len(), 1);
        assert_eq!(units[0].name, "hello");
        assert!(matches!(units[0].kind, SemanticUnitKind::Function));
        assert!(matches!(units[0].visibility, Visibility::Public));
    }

    #[test]
    fn test_extract_struct() {
        let code = "struct Point { x: i32, y: i32 }";
        let file = syn::parse_file(code).expect("parse failed");
        let units = SemanticUnitVisitor::extract(&file);

        assert_eq!(units.len(), 1);
        assert_eq!(units[0].name, "Point");
        assert!(matches!(units[0].kind, SemanticUnitKind::Struct));
    }

    #[test]
    fn test_extract_test_function() {
        let code = r#"
            #[test]
            fn test_something() {}
        "#;
        let file = syn::parse_file(code).expect("parse failed");
        let units = SemanticUnitVisitor::extract(&file);

        assert_eq!(units.len(), 1);
        assert!(units[0].has_attribute("test"));
    }

    #[test]
    fn test_extract_impl_block() {
        let code = r#"
            struct Foo;
            impl Foo {
                pub fn new() -> Self { Foo }
            }
        "#;
        let file = syn::parse_file(code).expect("parse failed");
        let units = SemanticUnitVisitor::extract(&file);

        assert_eq!(units.len(), 3);
        assert!(
            units
                .iter()
                .any(|u| u.name == "Foo" && matches!(u.kind, SemanticUnitKind::Struct))
        );
        assert!(
            units
                .iter()
                .any(|u| u.name == "Foo" && matches!(u.kind, SemanticUnitKind::Impl))
        );
        assert!(
            units
                .iter()
                .any(|u| u.name == "new" && matches!(u.kind, SemanticUnitKind::Function))
        );
    }

    #[test]
    fn test_extract_test_module() {
        let code = r#"
            fn production() {}

            #[cfg(test)]
            mod tests {
                fn helper() {}

                #[test]
                fn test_it() {}
            }
        "#;
        let file = syn::parse_file(code).expect("parse failed");
        let units = SemanticUnitVisitor::extract(&file);

        let prod_fn = units
            .iter()
            .find(|u| u.name == "production")
            .expect("production not found");
        assert!(!prod_fn.has_attribute("cfg_test"));

        let helper_fn = units
            .iter()
            .find(|u| u.name == "helper")
            .expect("helper not found");
        assert!(helper_fn.has_attribute("cfg_test"));

        let test_fn = units
            .iter()
            .find(|u| u.name == "test_it")
            .expect("test_it not found");
        assert!(test_fn.has_attribute("test"));
        assert!(test_fn.has_attribute("cfg_test"));
    }

    #[test]
    fn test_cfg_not_test_is_production() {
        let code = r#"
            #[cfg(not(test))]
            pub fn prod_only() {}
        "#;
        let file = syn::parse_file(code).expect("parse failed");
        let units = SemanticUnitVisitor::extract(&file);

        assert_eq!(units.len(), 1);
        assert!(!units[0].has_attribute("test"));
    }

    #[test]
    fn test_cfg_feature_latest_is_not_test() {
        let code = r#"
            #[cfg(feature = "latest")]
            pub fn latest_api() {}
        "#;
        let file = syn::parse_file(code).expect("parse failed");
        let units = SemanticUnitVisitor::extract(&file);

        assert!(!units[0].has_attribute("test"));
        assert!(units[0].has_attribute("cfg_feature:latest"));
    }

    #[test]
    fn test_cfg_any_test_is_test() {
        let code = r#"
            #[cfg(any(test, feature = "slow"))]
            fn helper() {}
        "#;
        let file = syn::parse_file(code).expect("parse failed");
        let units = SemanticUnitVisitor::extract(&file);

        assert!(units[0].has_attribute("test"));
    }

    #[test]
    fn test_multi_segment_test_attribute() {
        let code = r#"
            #[tokio::test]
            async fn async_test() {}
        "#;
        let file = syn::parse_file(code).expect("parse failed");
        let units = SemanticUnitVisitor::extract(&file);

        assert!(units[0].has_attribute("test"));
        assert!(units[0].has_attribute("tokio::test"));
    }

    #[test]
    fn test_cfg_feature_marker_recorded() {
        let code = r#"
            #[cfg(feature = "mock")]
            pub fn mock_helper() {}
        "#;
        let file = syn::parse_file(code).expect("parse failed");
        let units = SemanticUnitVisitor::extract(&file);

        assert!(units[0].has_attribute("cfg_feature:mock"));
    }

    #[test]
    fn test_cfg_not_feature_marker_skipped() {
        let code = r#"
            #[cfg(not(feature = "mock"))]
            pub fn real_impl() {}
        "#;
        let file = syn::parse_file(code).expect("parse failed");
        let units = SemanticUnitVisitor::extract(&file);

        assert!(!units[0].has_attribute("cfg_feature:mock"));
    }

    #[test]
    fn test_cfg_test_impl_marks_methods() {
        let code = r#"
            struct Foo;

            #[cfg(test)]
            impl Foo {
                fn helper() {}
            }
        "#;
        let file = syn::parse_file(code).expect("parse failed");
        let units = SemanticUnitVisitor::extract(&file);

        let helper = units
            .iter()
            .find(|u| u.name == "helper")
            .expect("helper not found");
        assert!(helper.has_attribute("cfg_test"));

        let foo = units
            .iter()
            .find(|u| matches!(u.kind, SemanticUnitKind::Struct))
            .expect("struct not found");
        assert!(!foo.has_attribute("cfg_test"));
    }

    #[test]
    fn test_private_trait_items_inherit_visibility() {
        let code = r#"
            trait Internal {
                fn hidden(&self);
            }

            pub trait External {
                fn visible(&self);
            }
        "#;
        let file = syn::parse_file(code).expect("parse failed");
        let units = SemanticUnitVisitor::extract(&file);

        let hidden = units
            .iter()
            .find(|u| u.name == "hidden")
            .expect("hidden not found");
        assert!(matches!(hidden.visibility, Visibility::Private));

        let visible = units
            .iter()
            .find(|u| u.name == "visible")
            .expect("visible not found");
        assert!(matches!(visible.visibility, Visibility::Public));
    }
}