Skip to content

Commit

Permalink
Merge pull request #1839 from dtolnay/chainedcompare
Browse files Browse the repository at this point in the history
Fix parenthesization of chained comparisons containing bailout
  • Loading branch information
dtolnay authored Jan 10, 2025
2 parents 9872bef + 4944362 commit 1f096d3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/fixup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,9 @@ fn scan_right(
return Scan::Consume;
}
let binop_prec = Precedence::of_binop(&e.op);
if binop_prec == Precedence::Compare && fixup.next_operator == Precedence::Compare {
return Scan::Consume;
}
let right_fixup = fixup.rightmost_subexpression_fixup(false, false, binop_prec);
let scan = scan_right(
&e.right,
Expand Down Expand Up @@ -639,10 +642,13 @@ fn scan_right(
Scan::Fail
}
}
None => match fixup.next_operator {
Precedence::Range => Scan::Consume,
_ => Scan::Fail,
},
None => {
if fixup.next_operator_can_begin_expr {
Scan::Consume
} else {
Scan::Fail
}
}
},
Expr::Break(e) => match &e.expr {
Some(value) => {
Expand Down
1 change: 1 addition & 0 deletions tests/test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ fn test_fixup() {
quote! { (1 + 1).abs() },
quote! { (lo..hi)[..] },
quote! { (a..b)..(c..d) },
quote! { (x > ..) > x },
quote! { (&mut fut).await },
quote! { &mut (x as i32) },
quote! { -(x as i32) },
Expand Down

0 comments on commit 1f096d3

Please sign in to comment.