Skip to content

Commit 1624b00

Browse files
committed
Fix suggestion to add unneeded space in manual_async
1 parent b20d4c1 commit 1624b00

File tree

4 files changed

+115
-10
lines changed

4 files changed

+115
-10
lines changed

clippy_lints/src/manual_async_fn.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,20 @@ impl<'tcx> LateLintPass<'tcx> for ManualAsyncFn {
6969
|diag| {
7070
if_chain! {
7171
if let Some(header_snip) = snippet_opt(cx, header_span);
72-
if let Some(ret_pos) = header_snip.rfind("->");
72+
if let Some(ret_pos) = header_snip.rfind("->").map(|rpos| {
73+
let mut rpos = rpos;
74+
let chars: Vec<char> = header_snip.chars().collect();
75+
while rpos > 1 {
76+
if let Some(c) = chars.get(rpos - 1) {
77+
if c.is_whitespace() {
78+
rpos -= 1;
79+
continue;
80+
}
81+
}
82+
break;
83+
}
84+
rpos
85+
});
7386
if let Some((ret_sugg, ret_snip)) = suggested_ret(cx, output);
7487
then {
7588
let help = format!("make the function `async` and {}", ret_sugg);
@@ -194,7 +207,7 @@ fn suggested_ret(cx: &LateContext<'_>, output: &Ty<'_>) -> Option<(&'static str,
194207
},
195208
_ => {
196209
let sugg = "return the output of the future directly";
197-
snippet_opt(cx, output.span).map(|snip| (sugg, format!("-> {}", snip)))
210+
snippet_opt(cx, output.span).map(|snip| (sugg, format!(" -> {}", snip)))
198211
},
199212
}
200213
}

tests/ui/manual_async_fn.fixed

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,19 @@ use std::future::Future;
77

88
async fn fut() -> i32 { 42 }
99

10-
async fn empty_fut() {}
10+
#[rustfmt::skip]
11+
async fn fut2() -> i32 { 42 }
12+
13+
#[rustfmt::skip]
14+
async fn fut3() -> i32 { 42 }
15+
16+
async fn empty_fut() {}
17+
18+
#[rustfmt::skip]
19+
async fn empty_fut2() {}
20+
21+
#[rustfmt::skip]
22+
async fn empty_fut3() {}
1123

1224
async fn core_fut() -> i32 { 42 }
1325

tests/ui/manual_async_fn.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,30 @@ fn fut() -> impl Future<Output = i32> {
99
async { 42 }
1010
}
1111

12+
#[rustfmt::skip]
13+
fn fut2() ->impl Future<Output = i32> {
14+
async { 42 }
15+
}
16+
17+
#[rustfmt::skip]
18+
fn fut3()-> impl Future<Output = i32> {
19+
async { 42 }
20+
}
21+
1222
fn empty_fut() -> impl Future<Output = ()> {
1323
async {}
1424
}
1525

26+
#[rustfmt::skip]
27+
fn empty_fut2() ->impl Future<Output = ()> {
28+
async {}
29+
}
30+
31+
#[rustfmt::skip]
32+
fn empty_fut3()-> impl Future<Output = ()> {
33+
async {}
34+
}
35+
1636
fn core_fut() -> impl core::future::Future<Output = i32> {
1737
async move { 42 }
1838
}

tests/ui/manual_async_fn.stderr

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,82 @@ LL | fn fut() -> impl Future<Output = i32> { 42 }
1515
| ^^^^^^
1616

1717
error: this function can be simplified using the `async fn` syntax
18-
--> $DIR/manual_async_fn.rs:12:1
18+
--> $DIR/manual_async_fn.rs:13:1
19+
|
20+
LL | fn fut2() ->impl Future<Output = i32> {
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
|
23+
help: make the function `async` and return the output of the future directly
24+
|
25+
LL | async fn fut2() -> i32 {
26+
| ^^^^^^^^^^^^^^^^^^^^^^
27+
help: move the body of the async block to the enclosing function
28+
|
29+
LL | fn fut2() ->impl Future<Output = i32> { 42 }
30+
| ^^^^^^
31+
32+
error: this function can be simplified using the `async fn` syntax
33+
--> $DIR/manual_async_fn.rs:18:1
34+
|
35+
LL | fn fut3()-> impl Future<Output = i32> {
36+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
37+
|
38+
help: make the function `async` and return the output of the future directly
39+
|
40+
LL | async fn fut3() -> i32 {
41+
| ^^^^^^^^^^^^^^^^^^^^^^
42+
help: move the body of the async block to the enclosing function
43+
|
44+
LL | fn fut3()-> impl Future<Output = i32> { 42 }
45+
| ^^^^^^
46+
47+
error: this function can be simplified using the `async fn` syntax
48+
--> $DIR/manual_async_fn.rs:22:1
1949
|
2050
LL | fn empty_fut() -> impl Future<Output = ()> {
2151
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2252
|
2353
help: make the function `async` and remove the return type
2454
|
25-
LL | async fn empty_fut() {
55+
LL | async fn empty_fut() {
2656
| ^^^^^^^^^^^^^^^^^^^^
2757
help: move the body of the async block to the enclosing function
2858
|
2959
LL | fn empty_fut() -> impl Future<Output = ()> {}
3060
| ^^
3161

3262
error: this function can be simplified using the `async fn` syntax
33-
--> $DIR/manual_async_fn.rs:16:1
63+
--> $DIR/manual_async_fn.rs:27:1
64+
|
65+
LL | fn empty_fut2() ->impl Future<Output = ()> {
66+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
67+
|
68+
help: make the function `async` and remove the return type
69+
|
70+
LL | async fn empty_fut2() {
71+
| ^^^^^^^^^^^^^^^^^^^^^
72+
help: move the body of the async block to the enclosing function
73+
|
74+
LL | fn empty_fut2() ->impl Future<Output = ()> {}
75+
| ^^
76+
77+
error: this function can be simplified using the `async fn` syntax
78+
--> $DIR/manual_async_fn.rs:32:1
79+
|
80+
LL | fn empty_fut3()-> impl Future<Output = ()> {
81+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
82+
|
83+
help: make the function `async` and remove the return type
84+
|
85+
LL | async fn empty_fut3() {
86+
| ^^^^^^^^^^^^^^^^^^^^^
87+
help: move the body of the async block to the enclosing function
88+
|
89+
LL | fn empty_fut3()-> impl Future<Output = ()> {}
90+
| ^^
91+
92+
error: this function can be simplified using the `async fn` syntax
93+
--> $DIR/manual_async_fn.rs:36:1
3494
|
3595
LL | fn core_fut() -> impl core::future::Future<Output = i32> {
3696
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -45,7 +105,7 @@ LL | fn core_fut() -> impl core::future::Future<Output = i32> { 42 }
45105
| ^^^^^^
46106

47107
error: this function can be simplified using the `async fn` syntax
48-
--> $DIR/manual_async_fn.rs:38:5
108+
--> $DIR/manual_async_fn.rs:58:5
49109
|
50110
LL | fn inh_fut() -> impl Future<Output = i32> {
51111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -65,7 +125,7 @@ LL | let c = 21;
65125
...
66126

67127
error: this function can be simplified using the `async fn` syntax
68-
--> $DIR/manual_async_fn.rs:73:1
128+
--> $DIR/manual_async_fn.rs:93:1
69129
|
70130
LL | fn elided(_: &i32) -> impl Future<Output = i32> + '_ {
71131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -80,7 +140,7 @@ LL | fn elided(_: &i32) -> impl Future<Output = i32> + '_ { 42 }
80140
| ^^^^^^
81141

82142
error: this function can be simplified using the `async fn` syntax
83-
--> $DIR/manual_async_fn.rs:82:1
143+
--> $DIR/manual_async_fn.rs:102:1
84144
|
85145
LL | fn explicit<'a, 'b>(_: &'a i32, _: &'b i32) -> impl Future<Output = i32> + 'a + 'b {
86146
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -94,5 +154,5 @@ help: move the body of the async block to the enclosing function
94154
LL | fn explicit<'a, 'b>(_: &'a i32, _: &'b i32) -> impl Future<Output = i32> + 'a + 'b { 42 }
95155
| ^^^^^^
96156

97-
error: aborting due to 6 previous errors
157+
error: aborting due to 10 previous errors
98158

0 commit comments

Comments
 (0)