prettyplease will generate rust code that does not accurately reflect the Expr tree that goes into it. There is a workaround using to_token_stream.
let mut file = syn::File {
shebang: None,
attrs: vec![],
items: vec![],
};
let mut func: syn::ItemFn = parse_quote!(fn foo(n: i32) -> i32 { });
let lhs: syn:Expr = parse_quote!(n + n);
let rhs: syn:Expr = parse_quote!(n + n);
func.block.stmts.push(
syn::ExprBinary {
attrs: vec![],
op: parse_quote!(*),
left: Box::new(lhs),
right: Box::new(rhs),
}
.into()
);
let pretty_direct = prettyplease::unparse(&file);
// INCORRECT: fn foo(n: i32) -> i32 { n + n * n + n }
let munged: syn::File = syn::parse2(rust.to_token_stream()).unwrap();
let pretty_indirect = prettyplease::unparse(&munged)).unwrap();
// CORRECT: fn foo(n: i32) -> i32 { (n + n) * (n + n) }
Apologies if this doesn't quite compile, but it should give you the general gist of the issue.
prettyplease will generate rust code that does not accurately reflect the Expr tree that goes into it. There is a workaround using
to_token_stream.Apologies if this doesn't quite compile, but it should give you the general gist of the issue.