Debian Codegraph @

third_party/rust/syn/tests/test_unparenthesize.rs

raw
1 #![cfg(not(miri))]
2 #![allow(
3 clippy::manual_assert,
4 clippy::needless_lifetimes,
5 clippy::uninlined_format_args
6 )]
7
8 use quote::ToTokens as _;
9 use std::fs;
10 use std::mem;
11 use std::panic;
12 use std::path::Path;
13 use std::sync::atomic::{AtomicUsize, Ordering};
14 use syn::visit_mut::{self, VisitMut};
15 use syn::Expr;
16
17 #[macro_use]
18 mod macros;
19
20 mod repo;
21
22 #[test]
23 fn test_unparenthesize() {
24 repo::rayon_init();
25 repo::clone_rust();
26
27 let failed = AtomicUsize::new(0);
28
29 repo::for_each_rust_file(|path| test(path, &failed));
30
31 let failed = failed.into_inner();
32 if failed > 0 {
33 panic!("{} failures", failed);
34 }
35 }
36
37 struct FlattenParens;
38
39 impl VisitMut for FlattenParens {
40 fn visit_expr_mut(&mut self, e: &mut Expr) {
41 while let Expr::Paren(paren) = e {
42 *e = mem::replace(&mut *paren.expr, Expr::PLACEHOLDER);
43 }
44 visit_mut::visit_expr_mut(self, e);
45 }
46 }
47
48 fn test(path: &Path, failed: &AtomicUsize) {
49 let content = fs::read_to_string(path).unwrap();
50
51 match panic::catch_unwind(|| -> syn::Result<()> {
52 let mut syntax_tree = syn::parse_file(&content)?;
53 FlattenParens.visit_file_mut(&mut syntax_tree);
54 syn::parse2::<syn::File>(syntax_tree.to_token_stream())?;
55 Ok(())
56 }) {
57 Err(_) => {
58 errorf!("=== {}: syn panic\n", path.display());
59 failed.fetch_add(1, Ordering::Relaxed);
60 }
61 Ok(Err(msg)) => {
62 errorf!("=== {}: syn failed to parse\n{:?}\n", path.display(), msg);
63 failed.fetch_add(1, Ordering::Relaxed);
64 }
65 Ok(Ok(())) => {}
66 }
67 }