File: test_unparenthesize.rs

package info (click to toggle)
rust-syn 2.0.98-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,700 kB
  • sloc: makefile: 2
file content (69 lines) | stat: -rw-r--r-- 1,918 bytes parent folder | download
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
#![cfg(not(miri))]
#![recursion_limit = "1024"]
#![feature(rustc_private)]
#![allow(
    clippy::manual_assert,
    clippy::match_like_matches_macro,
    clippy::needless_lifetimes,
    clippy::uninlined_format_args
)]

use crate::common::visit::{AsIfPrinted, FlattenParens};
use quote::ToTokens as _;
use std::fs;
use std::panic;
use std::path::Path;
use std::sync::atomic::{AtomicUsize, Ordering};
use syn::visit_mut::VisitMut as _;

#[macro_use]
mod macros;

mod common;
mod repo;

/*#[test]
fn test_unparenthesize() {
    repo::rayon_init();
    repo::clone_rust();

    let failed = AtomicUsize::new(0);

    repo::for_each_rust_file(|path| test(path, &failed));

    let failed = failed.into_inner();
    if failed > 0 {
        panic!("{} failures", failed);
    }
}*/

fn test(path: &Path, failed: &AtomicUsize) {
    let content = fs::read_to_string(path).unwrap();

    match panic::catch_unwind(|| -> syn::Result<()> {
        let mut before = syn::parse_file(&content)?;
        FlattenParens.visit_file_mut(&mut before);
        let printed = before.to_token_stream();
        let mut after = syn::parse2::<syn::File>(printed.clone())?;
        FlattenParens.visit_file_mut(&mut after);
        // Normalize features that we expect Syn not to print.
        AsIfPrinted.visit_file_mut(&mut before);
        if before != after {
            errorf!("=== {}\n", path.display());
            if failed.fetch_add(1, Ordering::Relaxed) == 0 {
                errorf!("BEFORE:\n{:#?}\nAFTER:\n{:#?}\n", before, after);
            }
        }
        Ok(())
    }) {
        Err(_) => {
            errorf!("=== {}: syn panic\n", path.display());
            failed.fetch_add(1, Ordering::Relaxed);
        }
        Ok(Err(msg)) => {
            errorf!("=== {}: syn failed to parse\n{:?}\n", path.display(), msg);
            failed.fetch_add(1, Ordering::Relaxed);
        }
        Ok(Ok(())) => {}
    }
}