Skip to content

Commit 1b731ec

Browse files
authored
Merge pull request #117 from dtolnay/binaryattr
Insert parentheses around binary operation with attribute
2 parents d5239de + b947e90 commit 1b731ec

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/expr.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,16 @@ impl Printer {
211211

212212
self.outer_attrs(&expr.attrs);
213213
self.ibox(0);
214+
if !expr.attrs.is_empty() {
215+
self.word("(");
216+
}
214217
self.subexpr(&expr.left, left_prec <= Precedence::Range, left_fixup);
215218
self.word(" = ");
216219
self.neverbreak();
217220
self.expr(&expr.right, right_fixup);
221+
if !expr.attrs.is_empty() {
222+
self.word(")");
223+
}
218224
self.end();
219225
}
220226

@@ -292,12 +298,18 @@ impl Printer {
292298
self.outer_attrs(&expr.attrs);
293299
self.ibox(INDENT);
294300
self.ibox(-INDENT);
301+
if !expr.attrs.is_empty() {
302+
self.word("(");
303+
}
295304
self.subexpr(&expr.left, left_needs_group, left_fixup);
296305
self.end();
297306
self.space();
298307
self.binary_operator(&expr.op);
299308
self.nbsp();
300309
self.subexpr(&expr.right, right_needs_group, right_fixup);
310+
if !expr.attrs.is_empty() {
311+
self.word(")");
312+
}
301313
self.end();
302314
}
303315

@@ -375,11 +387,17 @@ impl Printer {
375387
self.outer_attrs(&expr.attrs);
376388
self.ibox(INDENT);
377389
self.ibox(-INDENT);
390+
if !expr.attrs.is_empty() {
391+
self.word("(");
392+
}
378393
self.subexpr(&expr.expr, left_prec < Precedence::Cast, left_fixup);
379394
self.end();
380395
self.space();
381396
self.word("as ");
382397
self.ty(&expr.ty);
398+
if !expr.attrs.is_empty() {
399+
self.word(")");
400+
}
383401
self.end();
384402
}
385403

src/fixup.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ fn scan_right(
421421
}
422422
match expr {
423423
#![cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
424-
Expr::Assign(e) => {
424+
Expr::Assign(e) if e.attrs.is_empty() => {
425425
if match fixup.next_operator {
426426
Precedence::Unambiguous => fail_offset >= 2,
427427
_ => bailout_offset >= 1,
@@ -447,7 +447,7 @@ fn scan_right(
447447
Scan::Bailout
448448
}
449449
}
450-
Expr::Binary(e) => {
450+
Expr::Binary(e) if e.attrs.is_empty() => {
451451
if match fixup.next_operator {
452452
Precedence::Unambiguous => {
453453
fail_offset >= 2
@@ -628,8 +628,10 @@ fn scan_right(
628628
}
629629
Expr::Group(e) => scan_right(&e.expr, fixup, precedence, fail_offset, bailout_offset),
630630
Expr::Array(_)
631+
| Expr::Assign(_)
631632
| Expr::Async(_)
632633
| Expr::Await(_)
634+
| Expr::Binary(_)
633635
| Expr::Block(_)
634636
| Expr::Call(_)
635637
| Expr::Cast(_)

0 commit comments

Comments
 (0)