Debian Codegraph @

.cargo/vendor/syn/tests/test_unparenthesize.rs

raw
1 #![cfg(not(miri))]
2 #![recursion_limit = "1024"]
3 #![feature(rustc_private)]
4 #![allow(
5 clippy::elidable_lifetime_names,
6 clippy::manual_assert,
7 clippy::match_like_matches_macro,
8 clippy::needless_lifetimes,
9 clippy::uninlined_format_args
10 )]
11
12 use crate::common::visit::{AsIfPrinted, FlattenParens};
13 use quote::ToTokens as _;
14 use std::fs;
15 use std::panic;
16 use std::path::Path;
17 use std::sync::atomic::{AtomicUsize, Ordering};
18 use syn::visit_mut::VisitMut as _;
19
20 #[macro_use]
21 mod macros;
22
23 mod common;
24 mod repo;
25
26 #[test]
27 fn test_unparenthesize() {
28 repo::rayon_init();
29 repo::clone_rust();
30
31 let failed = AtomicUsize::new(0);
32
33 repo::for_each_rust_file(|path| test(path, &failed));
34
35 let failed = failed.into_inner();
36 if failed > 0 {
37 panic!("{} failures", failed);
38 }
39 }
40
41 fn test(path: &Path, failed: &AtomicUsize) {
42 let content = fs::read_to_string(path).unwrap();
43
44 match panic::catch_unwind(|| -> syn::Result<()> {
45 let mut before = syn::parse_file(&content)?;
46 FlattenParens::discard_attrs().visit_file_mut(&mut before);
47 let printed = before.to_token_stream();
48 let mut after = syn::parse2::<syn::File>(printed.clone())?;
49 FlattenParens::discard_attrs().visit_file_mut(&mut after);
50 // Normalize features that we expect Syn not to print.
51 AsIfPrinted.visit_file_mut(&mut before);
52 if before != after {
53 errorf!("=== {}\n", path.display());
54 if failed.fetch_add(1, Ordering::Relaxed) == 0 {
55 errorf!("BEFORE:\n{:#?}\nAFTER:\n{:#?}\n", before, after);
56 }
57 }
58 Ok(())
59 }) {
60 Err(_) => {
61 errorf!("=== {}: syn panic\n", path.display());
62 failed.fetch_add(1, Ordering::Relaxed);
63 }
64 Ok(Err(msg)) => {
65 errorf!("=== {}: syn failed to parse\n{:?}\n", path.display(), msg);
66 failed.fetch_add(1, Ordering::Relaxed);
67 }
68 Ok(Ok(())) => {}
69 }
70 }