Skip to content

Commit 026bb3c

Browse files
committed
Discard paren attrs in unparenthesize test
1 parent 217dd62 commit 026bb3c

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

tests/common/visit.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,23 @@ use std::mem;
33
use syn::visit_mut::{self, VisitMut};
44
use syn::{Expr, File, Generics, LifetimeParam, MacroDelimiter, Stmt, StmtMacro, TypeParam};
55

6-
pub struct FlattenParens;
6+
pub struct FlattenParens {
7+
discard_paren_attrs: bool,
8+
}
79

810
impl FlattenParens {
11+
pub fn discard_attrs() -> Self {
12+
FlattenParens {
13+
discard_paren_attrs: true,
14+
}
15+
}
16+
17+
pub fn combine_attrs() -> Self {
18+
FlattenParens {
19+
discard_paren_attrs: false,
20+
}
21+
}
22+
923
pub fn visit_token_stream_mut(tokens: &mut TokenStream) {
1024
*tokens = mem::take(tokens)
1125
.into_iter()
@@ -32,7 +46,7 @@ impl VisitMut for FlattenParens {
3246
while let Expr::Paren(paren) = e {
3347
let paren_attrs = mem::take(&mut paren.attrs);
3448
*e = mem::replace(&mut *paren.expr, Expr::PLACEHOLDER);
35-
if !paren_attrs.is_empty() {
49+
if !paren_attrs.is_empty() && !self.discard_paren_attrs {
3650
let nested_attrs = match e {
3751
Expr::Assign(e) => &mut e.attrs,
3852
Expr::Binary(e) => &mut e.attrs,

tests/test_expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ fn test_fixup() {
838838
let original: Expr = syn::parse2(tokens).unwrap();
839839

840840
let mut flat = original.clone();
841-
FlattenParens.visit_expr_mut(&mut flat);
841+
FlattenParens::combine_attrs().visit_expr_mut(&mut flat);
842842
let reconstructed: Expr = match syn::parse2(flat.to_token_stream()) {
843843
Ok(reconstructed) => reconstructed,
844844
Err(err) => panic!("failed to parse `{}`: {}", flat.to_token_stream(), err),
@@ -1615,7 +1615,7 @@ fn test_permutations() -> ExitCode {
16151615
);
16161616
};
16171617
AsIfPrinted.visit_expr_mut(&mut original);
1618-
FlattenParens.visit_expr_mut(&mut parsed);
1618+
FlattenParens::combine_attrs().visit_expr_mut(&mut parsed);
16191619
if original != parsed {
16201620
fail!(
16211621
"before: {}\n{:#?}\nafter: {}\n{:#?}",
@@ -1629,7 +1629,7 @@ fn test_permutations() -> ExitCode {
16291629
FlattenParens::visit_token_stream_mut(&mut tokens_no_paren);
16301630
if tokens.to_string() != tokens_no_paren.to_string() {
16311631
if let Ok(mut parsed2) = syn::parse2::<Expr>(tokens_no_paren) {
1632-
FlattenParens.visit_expr_mut(&mut parsed2);
1632+
FlattenParens::combine_attrs().visit_expr_mut(&mut parsed2);
16331633
if original == parsed2 {
16341634
fail!("redundant parens: {}", tokens);
16351635
}

tests/test_unparenthesize.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ fn test(path: &Path, failed: &AtomicUsize) {
4343

4444
match panic::catch_unwind(|| -> syn::Result<()> {
4545
let mut before = syn::parse_file(&content)?;
46-
FlattenParens.visit_file_mut(&mut before);
46+
FlattenParens::discard_attrs().visit_file_mut(&mut before);
4747
let printed = before.to_token_stream();
4848
let mut after = syn::parse2::<syn::File>(printed.clone())?;
49-
FlattenParens.visit_file_mut(&mut after);
49+
FlattenParens::discard_attrs().visit_file_mut(&mut after);
5050
// Normalize features that we expect Syn not to print.
5151
AsIfPrinted.visit_file_mut(&mut before);
5252
if before != after {

0 commit comments

Comments
 (0)