normalize-languages 0.3.2

Tree-sitter language support and dynamic grammar loading
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
//! Rust language support.

use std::path::{Path, PathBuf};

use crate::{
    ContainerBody, Import, ImportSpec, Language, LanguageSymbols, ModuleId, ModuleResolver,
    Resolution, ResolverConfig, Visibility,
};
use tree_sitter::Node;

/// Rust language support.
pub struct Rust;

impl Language for Rust {
    fn name(&self) -> &'static str {
        "Rust"
    }
    fn extensions(&self) -> &'static [&'static str] {
        &["rs"]
    }
    fn grammar_name(&self) -> &'static str {
        "rust"
    }

    fn as_symbols(&self) -> Option<&dyn LanguageSymbols> {
        Some(self)
    }

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

    fn extract_docstring(&self, node: &Node, content: &str) -> Option<String> {
        extract_docstring(node, content)
    }

    fn extract_attributes(&self, node: &Node, content: &str) -> Vec<String> {
        extract_attributes(node, content)
    }

    fn extract_implements(&self, node: &Node, content: &str) -> crate::ImplementsInfo {
        if node.kind() == "impl_item" {
            let type_node = match node.child_by_field_name("type") {
                Some(n) => n,
                None => return crate::ImplementsInfo::default(),
            };
            let _ = &content[type_node.byte_range()]; // used below
            let is_interface = node.child_by_field_name("trait").is_some();
            let implements = if let Some(trait_node) = node.child_by_field_name("trait") {
                vec![content[trait_node.byte_range()].to_string()]
            } else {
                Vec::new()
            };
            crate::ImplementsInfo {
                is_interface,
                implements,
            }
        } else {
            crate::ImplementsInfo::default()
        }
    }

    fn refine_kind(
        &self,
        node: &Node,
        _content: &str,
        tag_kind: crate::SymbolKind,
    ) -> crate::SymbolKind {
        match node.kind() {
            "struct_item" => crate::SymbolKind::Struct,
            "enum_item" => crate::SymbolKind::Enum,
            "type_item" => crate::SymbolKind::Type,
            "union_item" => crate::SymbolKind::Struct,
            "trait_item" => crate::SymbolKind::Trait,
            _ => tag_kind,
        }
    }

    fn build_signature(&self, node: &Node, content: &str) -> String {
        match node.kind() {
            "function_item" | "function_signature_item" => {
                let name = match self.node_name(node, content) {
                    Some(n) => n,
                    None => {
                        return content[node.byte_range()]
                            .lines()
                            .next()
                            .unwrap_or("")
                            .trim()
                            .to_string();
                    }
                };
                let vis = self.extract_visibility_prefix(node, content);
                let params = node
                    .child_by_field_name("parameters")
                    .map(|p| content[p.byte_range()].to_string())
                    .unwrap_or_else(|| "()".to_string());
                let return_type = node
                    .child_by_field_name("return_type")
                    .map(|r| format!(" -> {}", &content[r.byte_range()]))
                    .unwrap_or_default();
                format!("{}fn {}{}{}", vis, name, params, return_type)
            }
            "impl_item" => {
                let type_node = node.child_by_field_name("type");
                let type_name = type_node
                    .map(|n| content[n.byte_range()].to_string())
                    .unwrap_or_default();
                if let Some(trait_node) = node.child_by_field_name("trait") {
                    let trait_name = &content[trait_node.byte_range()];
                    format!("impl {} for {}", trait_name, type_name)
                } else {
                    format!("impl {}", type_name)
                }
            }
            "trait_item" => {
                let name = self.node_name(node, content).unwrap_or("");
                let vis = self.extract_visibility_prefix(node, content);
                format!("{}trait {}", vis, name)
            }
            "mod_item" => {
                let name = self.node_name(node, content).unwrap_or("");
                let vis = self.extract_visibility_prefix(node, content);
                format!("{}mod {}", vis, name)
            }
            "struct_item" => {
                let name = self.node_name(node, content).unwrap_or("");
                let vis = self.extract_visibility_prefix(node, content);
                format!("{}struct {}", vis, name)
            }
            "enum_item" => {
                let name = self.node_name(node, content).unwrap_or("");
                let vis = self.extract_visibility_prefix(node, content);
                format!("{}enum {}", vis, name)
            }
            "type_item" => {
                let name = self.node_name(node, content).unwrap_or("");
                let vis = self.extract_visibility_prefix(node, content);
                format!("{}type {}", vis, name)
            }
            _ => {
                let text = &content[node.byte_range()];
                text.lines().next().unwrap_or(text).trim().to_string()
            }
        }
    }

    fn extract_imports(&self, node: &Node, content: &str) -> Vec<Import> {
        if node.kind() != "use_declaration" {
            return Vec::new();
        }

        let line = node.start_position().row + 1;
        let text = &content[node.byte_range()];
        let module = text.trim_start_matches("use ").trim_end_matches(';').trim();

        // Check for braced imports: use foo::{bar, baz}
        let mut names = Vec::new();
        let is_relative = module.starts_with("crate")
            || module.starts_with("self")
            || module.starts_with("super");

        if let Some(brace_start) = module.find('{') {
            let prefix = module[..brace_start].trim_end_matches("::");
            // Find matching closing brace using depth counter to handle nested groups
            // like `use std::{io::{Read, Write}, fs}`.
            let brace_end = {
                let mut depth = 0u32;
                let mut end = None;
                for (i, c) in module[brace_start..].char_indices() {
                    match c {
                        '{' => depth += 1,
                        '}' => {
                            depth -= 1;
                            if depth == 0 {
                                end = Some(brace_start + i);
                                break;
                            }
                        }
                        _ => {}
                    }
                }
                end
            };
            if let Some(brace_end) = brace_end {
                let items = &module[brace_start + 1..brace_end];
                for item in items.split(',') {
                    let trimmed = item.trim();
                    if !trimmed.is_empty() {
                        names.push(trimmed.to_string());
                    }
                }
            }
            vec![Import {
                module: prefix.to_string(),
                names,
                alias: None,
                is_wildcard: false,
                is_relative,
                line,
            }]
        } else {
            // Simple import: use foo::bar or use foo::bar as baz
            let (module_part, alias) = if let Some(as_pos) = module.find(" as ") {
                (&module[..as_pos], Some(module[as_pos + 4..].to_string()))
            } else {
                (module, None)
            };

            vec![Import {
                module: module_part.to_string(),
                names: Vec::new(),
                alias,
                is_wildcard: module_part.ends_with("::*"),
                is_relative,
                line,
            }]
        }
    }

    fn format_import(&self, import: &Import, names: Option<&[&str]>) -> String {
        let names_to_use: Vec<&str> = names
            .map(|n| n.to_vec())
            .unwrap_or_else(|| import.names.iter().map(|s| s.as_str()).collect());

        if import.is_wildcard {
            // Module already contains ::* from parsing
            format!("use {};", import.module)
        } else if names_to_use.is_empty() {
            format!("use {};", import.module)
        } else if names_to_use.len() == 1 {
            format!("use {}::{};", import.module, names_to_use[0])
        } else {
            format!("use {}::{{{}}};", import.module, names_to_use.join(", "))
        }
    }

    fn get_visibility(&self, node: &Node, content: &str) -> Visibility {
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            if child.kind() == "visibility_modifier" {
                let vis = &content[child.byte_range()];
                if vis == "pub" {
                    return Visibility::Public;
                } else if vis.starts_with("pub(crate)") {
                    return Visibility::Internal;
                } else if vis.starts_with("pub(super)") || vis.starts_with("pub(in") {
                    return Visibility::Protected;
                }
            }
        }
        Visibility::Private
    }

    fn is_test_symbol(&self, symbol: &crate::Symbol) -> bool {
        let in_attrs = symbol
            .attributes
            .iter()
            .any(|a| a.contains("#[test]") || a.contains("#[cfg(test)]"));
        let in_sig =
            symbol.signature.contains("#[test]") || symbol.signature.contains("#[cfg(test)]");
        if in_attrs || in_sig {
            return true;
        }
        match symbol.kind {
            crate::SymbolKind::Function | crate::SymbolKind::Method => {
                symbol.name.starts_with("test_")
            }
            crate::SymbolKind::Module => symbol.name == "tests",
            _ => false,
        }
    }

    fn test_file_globs(&self) -> &'static [&'static str] {
        &[
            "**/tests/**",
            "**/test_*.rs",
            "**/*_test.rs",
            "**/*_tests.rs",
        ]
    }

    fn container_body<'a>(&self, node: &'a Node<'a>) -> Option<Node<'a>> {
        node.child_by_field_name("body")
    }

    fn analyze_container_body(
        &self,
        body_node: &Node,
        content: &str,
        inner_indent: &str,
    ) -> Option<ContainerBody> {
        crate::body::analyze_brace_body(body_node, content, inner_indent)
    }

    fn node_name<'a>(&self, node: &Node, content: &'a str) -> Option<&'a str> {
        // impl_item uses "type" field; trait_item and mod_item use "name"
        let name_node = node
            .child_by_field_name("name")
            .or_else(|| node.child_by_field_name("type"))?;
        Some(&content[name_node.byte_range()])
    }

    fn extract_module_doc(&self, src: &str) -> Option<String> {
        extract_rust_module_doc(src)
    }

    fn module_resolver(&self) -> Option<&dyn ModuleResolver> {
        static RESOLVER: RustModuleResolver = RustModuleResolver;
        Some(&RESOLVER)
    }
}

impl LanguageSymbols for Rust {}

/// Module resolver for Rust (Cargo workspace).
pub struct RustModuleResolver;

impl ModuleResolver for RustModuleResolver {
    fn workspace_config(&self, root: &Path) -> ResolverConfig {
        let cargo_toml = root.join("Cargo.toml");
        let mut path_mappings: Vec<(String, PathBuf)> = Vec::new();

        if let Ok(content) = std::fs::read_to_string(&cargo_toml) {
            // Try workspace members
            if let Ok(val) = content.parse::<toml::Value>() {
                if let Some(members) = val
                    .get("workspace")
                    .and_then(|w| w.get("members"))
                    .and_then(|m| m.as_array())
                {
                    for member in members {
                        if let Some(member_str) = member.as_str() {
                            // Expand glob patterns (simple case: no actual glob, just list)
                            let member_path = root.join(member_str);
                            let member_cargo = member_path.join("Cargo.toml");
                            if let Ok(mc) = std::fs::read_to_string(&member_cargo)
                                && let Ok(mv) = mc.parse::<toml::Value>()
                                && let Some(name) = mv
                                    .get("package")
                                    .and_then(|p| p.get("name"))
                                    .and_then(|n| n.as_str())
                            {
                                path_mappings.push((name.to_string(), member_path));
                            }
                        }
                    }
                }

                // Single-crate: read package name from root Cargo.toml
                if path_mappings.is_empty()
                    && let Some(name) = val
                        .get("package")
                        .and_then(|p| p.get("name"))
                        .and_then(|n| n.as_str())
                {
                    path_mappings.push((name.to_string(), root.to_path_buf()));
                }
            }
        }

        ResolverConfig {
            workspace_root: root.to_path_buf(),
            path_mappings,
            search_roots: Vec::new(),
        }
    }

    fn module_of_file(&self, _root: &Path, file: &Path, cfg: &ResolverConfig) -> Vec<ModuleId> {
        // Find the crate this file belongs to
        for (crate_name, crate_dir) in &cfg.path_mappings {
            let src_dir = crate_dir.join("src");
            if let Ok(rel) = file.strip_prefix(&src_dir) {
                let components: Vec<&str> = rel
                    .components()
                    .filter_map(|c| {
                        if let std::path::Component::Normal(s) = c {
                            s.to_str()
                        } else {
                            None
                        }
                    })
                    .collect();

                if components.is_empty() {
                    continue;
                }

                let last = *components.last().unwrap();
                let module_path =
                    if components.len() == 1 && (last == "lib.rs" || last == "main.rs") {
                        // Crate root
                        crate_name.clone()
                    } else {
                        // Build module path from components
                        let mut parts = vec![crate_name.as_str()];
                        for c in &components[..components.len() - 1] {
                            parts.push(c);
                        }
                        // Last component: strip .rs, handle mod.rs
                        let stem = if last == "mod.rs" {
                            // mod.rs is the module named by its parent directory
                            // (already included in parts above)
                            None
                        } else {
                            last.strip_suffix(".rs")
                        };
                        if let Some(s) = stem {
                            parts.push(s);
                        }
                        parts.join("::")
                    };

                return vec![ModuleId {
                    canonical_path: module_path,
                }];
            }
        }
        Vec::new()
    }

    fn resolve(&self, from_file: &Path, spec: &ImportSpec, cfg: &ResolverConfig) -> Resolution {
        // Only handle .rs files
        if from_file.extension().and_then(|e| e.to_str()) != Some("rs") {
            return Resolution::NotApplicable;
        }

        let raw = &spec.raw;

        // Handle super:: and self:: relative paths
        let resolved_raw = if raw.starts_with("super::") || raw.starts_with("self::") {
            // Resolve relative to from_file's module
            let crate_name = self.crate_name_for_file(from_file, cfg);
            if let Some(cn) = crate_name {
                let module_path = self.module_path_for_file(from_file, cfg);
                if let Some(suffix) = raw.strip_prefix("super::") {
                    // Pop one level from module path
                    let parent = module_path
                        .rsplit_once("::")
                        .map(|(p, _)| p.to_string())
                        .unwrap_or_else(|| cn.clone());
                    format!("{}::{}", parent, suffix)
                } else {
                    let suffix = raw.strip_prefix("self::").unwrap_or(raw);
                    format!("{}::{}", module_path, suffix)
                }
            } else {
                return Resolution::NotFound;
            }
        } else {
            raw.clone()
        };

        // Try to find the crate for this path
        let (crate_name, rest) = if let Some(pos) = resolved_raw.find("::") {
            let cn = &resolved_raw[..pos];
            let rest = &resolved_raw[pos + 2..];
            (cn.to_string(), rest.to_string())
        } else {
            (resolved_raw.clone(), String::new())
        };

        // Look up crate in workspace
        let crate_dir = cfg
            .path_mappings
            .iter()
            .find(|(name, _)| name == &crate_name)
            .map(|(_, dir)| dir.clone());

        let crate_dir = match crate_dir {
            Some(d) => d,
            None => {
                // Could be stdlib or external — not resolvable
                return Resolution::NotFound;
            }
        };

        // Convert module path to file path
        let exported_name = if let Some(pos) = rest.rfind("::") {
            rest[pos + 2..].to_string()
        } else {
            rest.clone()
        };

        let module_part = if let Some(pos) = rest.rfind("::") {
            rest[..pos].to_string()
        } else {
            String::new()
        };

        let candidate = self.module_to_file(&crate_dir, &module_part);
        if let Some(path) = candidate {
            Resolution::Resolved(path, exported_name)
        } else {
            Resolution::NotFound
        }
    }
}

impl RustModuleResolver {
    /// Get the crate name for a file.
    fn crate_name_for_file(&self, file: &Path, cfg: &ResolverConfig) -> Option<String> {
        for (crate_name, crate_dir) in &cfg.path_mappings {
            if file.starts_with(crate_dir) {
                return Some(crate_name.clone());
            }
        }
        None
    }

    /// Get the module path string for a file.
    fn module_path_for_file(&self, file: &Path, cfg: &ResolverConfig) -> String {
        for (crate_name, crate_dir) in &cfg.path_mappings {
            let src_dir = crate_dir.join("src");
            if let Ok(rel) = file.strip_prefix(&src_dir) {
                let components: Vec<&str> = rel
                    .components()
                    .filter_map(|c| {
                        if let std::path::Component::Normal(s) = c {
                            s.to_str()
                        } else {
                            None
                        }
                    })
                    .collect();
                if components.is_empty() {
                    return crate_name.clone();
                }
                let last = *components.last().unwrap();
                if components.len() == 1 && (last == "lib.rs" || last == "main.rs") {
                    return crate_name.clone();
                }
                let mut parts = vec![crate_name.as_str()];
                for c in &components[..components.len() - 1] {
                    parts.push(c);
                }
                if last != "mod.rs"
                    && let Some(s) = last.strip_suffix(".rs")
                {
                    parts.push(s);
                }
                return parts.join("::");
            }
        }
        String::new()
    }

    /// Convert a module path to a file path within a crate directory.
    ///
    /// e.g. "foo::bar" → tries `src/foo/bar.rs` and `src/foo/bar/mod.rs`
    fn module_to_file(&self, crate_dir: &Path, module_path: &str) -> Option<PathBuf> {
        let src_dir = crate_dir.join("src");

        if module_path.is_empty() {
            // Refers to the crate root
            let lib = src_dir.join("lib.rs");
            if lib.exists() {
                return Some(lib);
            }
            let main = src_dir.join("main.rs");
            if main.exists() {
                return Some(main);
            }
            return None;
        }

        let parts: Vec<&str> = module_path.split("::").collect();
        let mut path = src_dir.clone();
        for part in &parts {
            path = path.join(part);
        }

        // Try path.rs first
        let rs_path = path.with_extension("rs");
        if rs_path.exists() {
            return Some(rs_path);
        }

        // Try path/mod.rs
        let mod_path = path.join("mod.rs");
        if mod_path.exists() {
            return Some(mod_path);
        }

        None
    }
}

impl Rust {
    fn extract_visibility_prefix(&self, node: &Node, content: &str) -> String {
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            if child.kind() == "visibility_modifier" {
                return format!("{} ", &content[child.byte_range()]);
            }
        }
        String::new()
    }
}

/// Extract a Rust doc comment from a node's `attributes` child.
///
/// Looks for `line_outer_doc_comment` nodes (`///`) and joins their text.
fn extract_docstring(node: &Node, content: &str) -> Option<String> {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if child.kind() == "attributes" {
            let mut doc_lines = Vec::new();
            let mut attr_cursor = child.walk();
            for attr_child in child.children(&mut attr_cursor) {
                if attr_child.kind() == "line_outer_doc_comment" {
                    let text = &content[attr_child.byte_range()];
                    let doc = text.trim_start_matches("///").trim();
                    if !doc.is_empty() {
                        doc_lines.push(doc.to_string());
                    }
                }
            }
            if !doc_lines.is_empty() {
                return Some(doc_lines.join("\n"));
            }
        }
    }
    None
}

/// Extract Rust `#[...]` attribute items from a node.
///
/// Checks both the `attributes` child field and preceding sibling `attribute_item` nodes.
fn extract_attributes(node: &Node, content: &str) -> Vec<String> {
    let mut attrs = Vec::new();

    // Check for attributes child (e.g., #[test], #[cfg(test)])
    if let Some(attr_node) = node.child_by_field_name("attributes") {
        let mut cursor = attr_node.walk();
        for child in attr_node.children(&mut cursor) {
            if child.kind() == "attribute_item" {
                attrs.push(content[child.byte_range()].to_string());
            }
        }
    }

    // Also check preceding siblings for outer attributes
    let mut prev = node.prev_sibling();
    while let Some(sibling) = prev {
        if sibling.kind() == "attribute_item" {
            // Insert at beginning to maintain order
            attrs.insert(0, content[sibling.byte_range()].to_string());
            prev = sibling.prev_sibling();
        } else {
            break;
        }
    }

    attrs
}

/// Extract the module-level doc comment from Rust source.
///
/// Collects consecutive `//!` inner-doc comment lines from the top of the file,
/// stopping at the first line that is not a `//!` comment (ignoring blank lines).
fn extract_rust_module_doc(src: &str) -> Option<String> {
    let mut lines = Vec::new();
    for line in src.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with("//!") {
            let text = trimmed.strip_prefix("//!").unwrap_or("").trim_start();
            lines.push(text.to_string());
        } else if trimmed.is_empty() && lines.is_empty() {
            // skip leading blank lines
        } else {
            break;
        }
    }
    if lines.is_empty() {
        return None;
    }
    // Strip trailing empty lines from the collected doc
    while lines.last().map(|l: &String| l.is_empty()).unwrap_or(false) {
        lines.pop();
    }
    if lines.is_empty() {
        None
    } else {
        Some(lines.join("\n"))
    }
}

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

    /// Documents node kinds that exist in the Rust grammar but aren't used in trait methods.
    /// Run `cross_check_node_kinds` in registry.rs to see all potentially useful kinds.
    #[test]
    fn unused_node_kinds_audit() {
        // Categories:
        // - STRUCTURAL: Internal/wrapper nodes
        // - CLAUSE: Sub-parts of larger constructs
        // - EXPRESSION: Expressions (we track statements/definitions)
        // - TYPE: Type-related nodes
        // - MODIFIER: Visibility/async/unsafe modifiers
        // - PATTERN: Pattern matching internals
        // - MACRO: Macro-related nodes
        // - MAYBE: Potentially useful

        #[rustfmt::skip]
        let documented_unused: &[&str] = &[
            // STRUCTURAL
            "block_comment",           // comments        // extern block contents
            "field_declaration",       // struct field
            "field_declaration_list",  // struct body
            "field_expression",        // foo.bar
            "field_identifier",        // field name
            "identifier",              // too common
            "lifetime",                // 'a
            "lifetime_parameter",      // <'a>
            "ordered_field_declaration_list", // tuple struct fields
            "scoped_identifier",       // path::to::thing
            "scoped_type_identifier",  // path::to::Type
            "shorthand_field_identifier", // struct init shorthand
            "type_identifier",         // type names
            "visibility_modifier",     // pub, pub(crate)

            // CLAUSE
            "else_clause",             // part of if
            "enum_variant",            // enum variant
            "enum_variant_list",       // enum body
            "match_block",             // match body
            "match_pattern",           // match arm pattern
            "trait_bounds",            // T: Foo + Bar
            "where_clause",            // where T: Foo

            // EXPRESSION
            "array_expression",        // [1, 2, 3]
            "assignment_expression",   // x = y
            "async_block",             // async { }
            "await_expression",        // foo.await         // foo()
            "generic_function",        // foo::<T>()
            "index_expression",        // arr[i]
            "parenthesized_expression",// (expr)
            "range_expression",        // 0..10
            "reference_expression",    // &x
            "struct_expression",       // Foo { x: 1 }
            "try_expression",          // foo?
            "tuple_expression",        // (a, b)
            "type_cast_expression",    // x as T
            "unary_expression",        // -x, !x
            "unit_expression",         // ()
            "yield_expression",        // yield x

            // TYPE
            "abstract_type",           // impl Trait
            "array_type",              // [T; N]
            "bounded_type",            // T: Foo
            "bracketed_type",          // <T>
            "dynamic_type",            // dyn Trait
            "function_type",           // fn(T) -> U
            "generic_type",            // Vec<T>
            "generic_type_with_turbofish", // Vec::<T>
            "higher_ranked_trait_bound", // for<'a>
            "never_type",              // !
            "pointer_type",            // *const T
            "primitive_type",          // i32, bool
            "qualified_type",          // <T as Trait>::Item
            "reference_type",          // &T
            "removed_trait_bound",     // ?Sized
            "tuple_type",              // (A, B)
            "type_arguments",          // <T, U>
            "type_binding",            // Item = T
            "type_parameter",          // T
            "type_parameters",         // <T, U>
            "unit_type",               // ()
            "unsafe_bound_type",       // unsafe trait bound

            // MODIFIER
            "block_outer_doc_comment", // //!
            "extern_modifier",         // extern "C"
            "function_modifiers",      // async, const, unsafe
            "mutable_specifier",       // mut

            // PATTERN
            "struct_pattern",          // Foo { x, y }
            "tuple_struct_pattern",    // Foo(x, y)

            // MACRO
            "fragment_specifier",      // $x:expr
            "macro_arguments_declaration", // macro args
            "macro_body_v2",           // macro body        // macro_rules!
            "macro_definition_v2",     // macro 2.0

            // OTHER
            "block_expression_with_attribute", // #[attr] { }
            "const_block",             // const { }
            "expression_statement",    // expr;
            "expression_with_attribute", // #[attr] expr
            "extern_crate_declaration",// extern crate
            "foreign_mod_item",        // extern block item
            "function_signature_item", // fn signature in trait
            "gen_block",               // gen { }
            "let_declaration",         // let x = y
            "try_block",               // try { }
            "unsafe_block",            // unsafe { }
            "use_as_clause",           // use foo as bar
            "empty_statement",         // ;
            // control flow — not extracted as symbols
            "closure_expression",
            "continue_expression",
            "match_expression",
            "use_declaration",
            "for_expression",
            "match_arm",
            "break_expression",
            "while_expression",
            "loop_expression",
            "return_expression",
            "if_expression",
            "block",
            "binary_expression",
        ];

        validate_unused_kinds_audit(&Rust, documented_unused)
            .expect("Rust unused node kinds audit failed");
    }
}