Skip to content

Commit bdaa048

Browse files
Rollup merge of #153445 - ia0:overflow_try, r=ytmimi
Consider try blocks as block-like for overflowed expr The tracking issue for `try_blocks` is #31436. The tracking issue for `try_blocks_heterogeneous` is #149488. Fixes rust-lang/rustfmt#6799.
2 parents 06b411c + 07a3da1 commit bdaa048

5 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/tools/rustfmt/src/expr.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,10 @@ pub(crate) fn can_be_overflowed_expr(
15531553
}
15541554

15551555
// Handle always block-like expressions
1556-
ast::ExprKind::Gen(..) | ast::ExprKind::Block(..) | ast::ExprKind::Closure(..) => true,
1556+
ast::ExprKind::Gen(..)
1557+
| ast::ExprKind::Block(..)
1558+
| ast::ExprKind::Closure(..)
1559+
| ast::ExprKind::TryBlock(..) => true,
15571560

15581561
// Handle `[]` and `{}`-like expressions
15591562
ast::ExprKind::Array(..) | ast::ExprKind::Struct(..) => {

src/tools/rustfmt/tests/source/try_block.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,13 @@ fn baz() -> Option<i32> {
2727

2828
let x: Option<i32> = try { baz()?; baz()?; baz()?; 7 };
2929

30+
let _ = overflowed_expr(
31+
x,
32+
try {
33+
foo()?;
34+
bar()?;
35+
},
36+
);
37+
3038
return None;
3139
}

src/tools/rustfmt/tests/source/try_blocks_heterogeneous.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,15 @@ fn baz() -> Option<i32> {
3535

3636
let x = try bikeshed Foo<Bar, Bar, Bar, Bar, Bar, Bar, Bar, Bar, Bar, Bar, Bar, Bar, Bar, Bar> {};
3737

38+
let x = try bikeshed Result<VeryVeryVeryVeryVeryLongTypeForSuccess, VeryVeryVeryVeryVeryLongTypeForFailure> {};
39+
40+
let _ = overflowed_expr(
41+
x,
42+
try bikeshed Option<_> {
43+
foo()?;
44+
bar()?;
45+
},
46+
);
47+
3848
return None;
3949
}

src/tools/rustfmt/tests/target/try_block.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,10 @@ fn baz() -> Option<i32> {
2626
7
2727
};
2828

29+
let _ = overflowed_expr(x, try {
30+
foo()?;
31+
bar()?;
32+
});
33+
2934
return None;
3035
}

src/tools/rustfmt/tests/target/try_blocks_heterogeneous.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,16 @@ fn baz() -> Option<i32> {
3737
let x =
3838
try bikeshed Foo<Bar, Bar, Bar, Bar, Bar, Bar, Bar, Bar, Bar, Bar, Bar, Bar, Bar, Bar> {};
3939

40+
let x = try bikeshed Result<
41+
VeryVeryVeryVeryVeryLongTypeForSuccess,
42+
VeryVeryVeryVeryVeryLongTypeForFailure,
43+
> {
44+
};
45+
46+
let _ = overflowed_expr(x, try bikeshed Option<_> {
47+
foo()?;
48+
bar()?;
49+
});
50+
4051
return None;
4152
}

0 commit comments

Comments
 (0)