Skip to content

Commit bdab101

Browse files
committed
Check if function return type fits before rewriting
fixes #6831 This fix is gated behind style_edition=2027 because it would impact stable formatting.
1 parent 0660429 commit bdab101

7 files changed

Lines changed: 273 additions & 1 deletion

src/items.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2675,7 +2675,12 @@ fn rewrite_fn_base(
26752675
.unwrap_or(ret_shape)
26762676
};
26772677

2678-
if multi_line_ret_str || ret_should_indent {
2678+
let exceeds_max_width = last_line_width(&result) + ret_str_len > context.config.max_width();
2679+
2680+
if multi_line_ret_str
2681+
|| ret_should_indent
2682+
|| (context.config.style_edition() >= StyleEdition::Edition2027 && exceeds_max_width)
2683+
{
26792684
// Now that we know the proper indent and width, we need to
26802685
// re-layout the return type.
26812686
let ret_str = fd.output.rewrite_result(context, ret_shape)?;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// rustfmt-style_edition: 2021
2+
// rustfmt-max_width: 100
3+
4+
impl<T: CapnpWrite>
5+
IntoMessage<
6+
capnp::message::TypedReader<
7+
capnp::message::Builder<capnp::message::HeapAllocator>,
8+
T::Capnp,
9+
>,
10+
> for T
11+
{
12+
// Incorrectly places the return type on a single line
13+
fn into_msg(
14+
self,
15+
) -> capnp::message::TypedReader<
16+
capnp::message::Builder<capnp::message::HeapAllocator>, T::Capnp
17+
>
18+
{
19+
todo!()
20+
}
21+
}
22+
23+
impl<T> Foo for T {
24+
fn into_msg_return_type_single_line_99(
25+
self,
26+
) -> some::long::path::to::GenericType<long::path::to::GenericType<some::Type>, some::Type____>
27+
{
28+
todo!()
29+
}
30+
31+
fn into_msg_return_type_single_line_100(
32+
self,
33+
) -> some::long::path::to::GenericType<long::path::to::GenericType<some::Type>, some::Type_____>
34+
{
35+
todo!()
36+
}
37+
38+
// Incorrectly places the return type on a single line
39+
fn into_msg_return_type_single_line_101(
40+
self,
41+
) -> some::long::path::to::GenericType<long::path::to::GenericType<some::Type>, some::Type______>
42+
{
43+
todo!()
44+
}
45+
}
46+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// rustfmt-style_edition: 2024
2+
// rustfmt-max_width: 100
3+
4+
impl<T: CapnpWrite>
5+
IntoMessage<
6+
capnp::message::TypedReader<
7+
capnp::message::Builder<capnp::message::HeapAllocator>,
8+
T::Capnp,
9+
>,
10+
> for T
11+
{
12+
// Incorrectly places the return type on a single line
13+
fn into_msg(
14+
self,
15+
) -> capnp::message::TypedReader<
16+
capnp::message::Builder<capnp::message::HeapAllocator>, T::Capnp
17+
>
18+
{
19+
todo!()
20+
}
21+
}
22+
23+
impl<T> Foo for T {
24+
fn into_msg_return_type_single_line_99(
25+
self,
26+
) -> some::long::path::to::GenericType<long::path::to::GenericType<some::Type>, some::Type____>
27+
{
28+
todo!()
29+
}
30+
31+
fn into_msg_return_type_single_line_100(
32+
self,
33+
) -> some::long::path::to::GenericType<long::path::to::GenericType<some::Type>, some::Type_____>
34+
{
35+
todo!()
36+
}
37+
38+
// Incorrectly places the return type on a single line
39+
fn into_msg_return_type_single_line_101(
40+
self,
41+
) -> some::long::path::to::GenericType<long::path::to::GenericType<some::Type>, some::Type______>
42+
{
43+
todo!()
44+
}
45+
}
46+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// rustfmt-style_edition: 2027
2+
// rustfmt-max_width: 100
3+
4+
impl<T: CapnpWrite>
5+
IntoMessage<
6+
capnp::message::TypedReader<
7+
capnp::message::Builder<capnp::message::HeapAllocator>,
8+
T::Capnp,
9+
>,
10+
> for T
11+
{
12+
fn into_msg(
13+
self,
14+
) -> capnp::message::TypedReader<
15+
capnp::message::Builder<capnp::message::HeapAllocator>, T::Capnp
16+
>
17+
{
18+
todo!()
19+
}
20+
}
21+
22+
impl<T> Foo for T {
23+
fn into_msg_return_type_single_line_99(
24+
self,
25+
) -> some::long::path::to::GenericType<long::path::to::GenericType<some::Type>, some::Type____>
26+
{
27+
todo!()
28+
}
29+
30+
fn into_msg_return_type_single_line_100(
31+
self,
32+
) -> some::long::path::to::GenericType<long::path::to::GenericType<some::Type>, some::Type_____>
33+
{
34+
todo!()
35+
}
36+
37+
38+
fn into_msg_return_type_single_line_101(
39+
self,
40+
) -> some::long::path::to::GenericType<long::path::to::GenericType<some::Type>, some::Type______>
41+
{
42+
todo!()
43+
}
44+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// rustfmt-style_edition: 2021
2+
// rustfmt-max_width: 100
3+
4+
impl<T: CapnpWrite>
5+
IntoMessage<
6+
capnp::message::TypedReader<
7+
capnp::message::Builder<capnp::message::HeapAllocator>,
8+
T::Capnp,
9+
>,
10+
> for T
11+
{
12+
// Incorrectly places the return type on a single line
13+
fn into_msg(
14+
self,
15+
) -> capnp::message::TypedReader<capnp::message::Builder<capnp::message::HeapAllocator>, T::Capnp>
16+
{
17+
todo!()
18+
}
19+
}
20+
21+
impl<T> Foo for T {
22+
fn into_msg_return_type_single_line_99(
23+
self,
24+
) -> some::long::path::to::GenericType<long::path::to::GenericType<some::Type>, some::Type____>
25+
{
26+
todo!()
27+
}
28+
29+
fn into_msg_return_type_single_line_100(
30+
self,
31+
) -> some::long::path::to::GenericType<long::path::to::GenericType<some::Type>, some::Type_____>
32+
{
33+
todo!()
34+
}
35+
36+
// Incorrectly places the return type on a single line
37+
fn into_msg_return_type_single_line_101(
38+
self,
39+
) -> some::long::path::to::GenericType<long::path::to::GenericType<some::Type>, some::Type______>
40+
{
41+
todo!()
42+
}
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// rustfmt-style_edition: 2024
2+
// rustfmt-max_width: 100
3+
4+
impl<T: CapnpWrite>
5+
IntoMessage<
6+
capnp::message::TypedReader<
7+
capnp::message::Builder<capnp::message::HeapAllocator>,
8+
T::Capnp,
9+
>,
10+
> for T
11+
{
12+
// Incorrectly places the return type on a single line
13+
fn into_msg(
14+
self,
15+
) -> capnp::message::TypedReader<capnp::message::Builder<capnp::message::HeapAllocator>, T::Capnp>
16+
{
17+
todo!()
18+
}
19+
}
20+
21+
impl<T> Foo for T {
22+
fn into_msg_return_type_single_line_99(
23+
self,
24+
) -> some::long::path::to::GenericType<long::path::to::GenericType<some::Type>, some::Type____>
25+
{
26+
todo!()
27+
}
28+
29+
fn into_msg_return_type_single_line_100(
30+
self,
31+
) -> some::long::path::to::GenericType<long::path::to::GenericType<some::Type>, some::Type_____>
32+
{
33+
todo!()
34+
}
35+
36+
// Incorrectly places the return type on a single line
37+
fn into_msg_return_type_single_line_101(
38+
self,
39+
) -> some::long::path::to::GenericType<long::path::to::GenericType<some::Type>, some::Type______>
40+
{
41+
todo!()
42+
}
43+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// rustfmt-style_edition: 2027
2+
// rustfmt-max_width: 100
3+
4+
impl<T: CapnpWrite>
5+
IntoMessage<
6+
capnp::message::TypedReader<
7+
capnp::message::Builder<capnp::message::HeapAllocator>,
8+
T::Capnp,
9+
>,
10+
> for T
11+
{
12+
fn into_msg(
13+
self,
14+
) -> capnp::message::TypedReader<
15+
capnp::message::Builder<capnp::message::HeapAllocator>,
16+
T::Capnp,
17+
> {
18+
todo!()
19+
}
20+
}
21+
22+
impl<T> Foo for T {
23+
fn into_msg_return_type_single_line_99(
24+
self,
25+
) -> some::long::path::to::GenericType<long::path::to::GenericType<some::Type>, some::Type____>
26+
{
27+
todo!()
28+
}
29+
30+
fn into_msg_return_type_single_line_100(
31+
self,
32+
) -> some::long::path::to::GenericType<long::path::to::GenericType<some::Type>, some::Type_____>
33+
{
34+
todo!()
35+
}
36+
37+
fn into_msg_return_type_single_line_101(
38+
self,
39+
) -> some::long::path::to::GenericType<
40+
long::path::to::GenericType<some::Type>,
41+
some::Type______,
42+
> {
43+
todo!()
44+
}
45+
}

0 commit comments

Comments
 (0)