@@ -12,13 +12,15 @@ use syntax::{
12
12
13
13
use crate :: { AssistContext , Assists } ;
14
14
15
+ // FIXME: This ought to be a diagnostic lint.
16
+
15
17
// Assist: unnecessary_async
16
18
//
17
19
// Removes the `async` mark from functions which have no `.await` in their body.
18
20
// Looks for calls to the functions and removes the `.await` on the call site.
19
21
//
20
22
// ```
21
- // pub async f$0n foo() {}
23
+ // pub asy$0nc fn foo() {}
22
24
// pub async fn bar() { foo().await }
23
25
// ```
24
26
// ->
@@ -29,15 +31,11 @@ use crate::{AssistContext, Assists};
29
31
pub ( crate ) fn unnecessary_async ( acc : & mut Assists , ctx : & AssistContext < ' _ > ) -> Option < ( ) > {
30
32
let function: ast:: Fn = ctx. find_node_at_offset ( ) ?;
31
33
32
- // Do nothing if the cursor is not on the prototype. This is so that the check does not pollute
33
- // when the user asks us for assists when in the middle of the function body.
34
- // We consider the prototype to be anything that is before the body of the function.
35
- let cursor_position = ctx. offset ( ) ;
36
- if cursor_position >= function. body ( ) ?. syntax ( ) . text_range ( ) . start ( ) {
34
+ // Do nothing if the cursor isn't on the async token.
35
+ let async_token = function. async_token ( ) ?;
36
+ if !async_token. text_range ( ) . contains_inclusive ( ctx. offset ( ) ) {
37
37
return None ;
38
38
}
39
- // Do nothing if the function isn't async.
40
- function. async_token ( ) ?;
41
39
// Do nothing if the function has an `await` expression in its body.
42
40
if function. body ( ) ?. syntax ( ) . descendants ( ) . find_map ( ast:: AwaitExpr :: cast) . is_some ( ) {
43
41
return None ;
@@ -138,27 +136,22 @@ mod tests {
138
136
139
137
#[ test]
140
138
fn applies_on_empty_function ( ) {
141
- check_assist ( unnecessary_async, "pub async f$0n f() {}" , "pub fn f() {}" )
139
+ check_assist ( unnecessary_async, "pub asy$0nc fn f() {}" , "pub fn f() {}" )
142
140
}
143
141
144
142
#[ test]
145
143
fn applies_and_removes_whitespace ( ) {
146
- check_assist ( unnecessary_async, "pub async f$0n f() {}" , "pub fn f() {}" )
147
- }
148
-
149
- #[ test]
150
- fn does_not_apply_on_non_async_function ( ) {
151
- check_assist_not_applicable ( unnecessary_async, "pub f$0n f() {}" )
144
+ check_assist ( unnecessary_async, "pub async$0 fn f() {}" , "pub fn f() {}" )
152
145
}
153
146
154
147
#[ test]
155
148
fn applies_on_function_with_a_non_await_expr ( ) {
156
- check_assist ( unnecessary_async, "pub async f$0n f() { f2() }" , "pub fn f() { f2() }" )
149
+ check_assist ( unnecessary_async, "pub asy$0nc fn f() { f2() }" , "pub fn f() { f2() }" )
157
150
}
158
151
159
152
#[ test]
160
153
fn does_not_apply_on_function_with_an_await_expr ( ) {
161
- check_assist_not_applicable ( unnecessary_async, "pub async f$0n f() { f2().await }" )
154
+ check_assist_not_applicable ( unnecessary_async, "pub asy$0nc fn f() { f2().await }" )
162
155
}
163
156
164
157
#[ test]
@@ -167,7 +160,7 @@ mod tests {
167
160
unnecessary_async,
168
161
r#"
169
162
pub async fn f4() { }
170
- pub async f$0n f2() { }
163
+ pub asy$0nc fn f2() { }
171
164
pub async fn f() { f2().await }
172
165
pub async fn f3() { f2().await }"# ,
173
166
r#"
@@ -184,7 +177,7 @@ pub async fn f3() { f2() }"#,
184
177
unnecessary_async,
185
178
r#"
186
179
pub async fn f4() { }
187
- mod a { pub async f$0n f2() { } }
180
+ mod a { pub asy$0nc fn f2() { } }
188
181
pub async fn f() { a::f2().await }
189
182
pub async fn f3() { a::f2().await }"# ,
190
183
r#"
@@ -202,7 +195,7 @@ pub async fn f3() { a::f2() }"#,
202
195
// Ensure that it is the first await on the 3rd line that is removed
203
196
r#"
204
197
pub async fn f() { f2().await }
205
- pub async f$0n f2() -> i32 { 1 }
198
+ pub asy$0nc fn f2() -> i32 { 1 }
206
199
pub async fn f3() { f4(f2().await).await }
207
200
pub async fn f4(i: i32) { }"# ,
208
201
r#"
@@ -220,7 +213,7 @@ pub async fn f4(i: i32) { }"#,
220
213
// Ensure that it is the second await on the 3rd line that is removed
221
214
r#"
222
215
pub async fn f() { f2().await }
223
- pub async f$0n f2(i: i32) { }
216
+ pub async$0 fn f2(i: i32) { }
224
217
pub async fn f3() { f2(f4().await).await }
225
218
pub async fn f4() -> i32 { 1 }"# ,
226
219
r#"
@@ -237,7 +230,7 @@ pub async fn f4() -> i32 { 1 }"#,
237
230
unnecessary_async,
238
231
r#"
239
232
pub struct S { }
240
- impl S { pub async f$0n f2(&self) { } }
233
+ impl S { pub async$0 fn f2(&self) { } }
241
234
pub async fn f(s: &S) { s.f2().await }"# ,
242
235
r#"
243
236
pub struct S { }
@@ -250,13 +243,13 @@ pub async fn f(s: &S) { s.f2() }"#,
250
243
fn does_not_apply_on_function_with_a_nested_await_expr ( ) {
251
244
check_assist_not_applicable (
252
245
unnecessary_async,
253
- "async f$0n f() { if true { loop { f2().await } } }" ,
246
+ "async$0 fn f() { if true { loop { f2().await } } }" ,
254
247
)
255
248
}
256
249
257
250
#[ test]
258
- fn does_not_apply_when_not_on_prototype ( ) {
259
- check_assist_not_applicable ( unnecessary_async, "pub async fn f() { $0f2 () }" )
251
+ fn does_not_apply_when_not_on_async_token ( ) {
252
+ check_assist_not_applicable ( unnecessary_async, "pub async fn$0 f() { f2 () }" )
260
253
}
261
254
262
255
#[ test]
0 commit comments