2 assists::{AssistId, AssistKind},
6 syntax_helpers::node_ext::full_path_of_name_ref,
9 ast::{self, NameLike, NameRef},
10 AstNode, SyntaxKind, TextRange,
13 use crate::{AssistContext, Assists};
15 // Assist: unnecessary_async
17 // Removes the `async` mark from functions which have no `.await` in their body.
18 // Looks for calls to the functions and removes the `.await` on the call site.
21 // pub async f$0n foo() {}
22 // pub async fn bar() { foo().await }
27 // pub async fn bar() { foo() }
29 pub(crate) fn unnecessary_async(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
30 let function: ast::Fn = ctx.find_node_at_offset()?;
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() {
39 // Do nothing if the function isn't async.
40 if let None = function.async_token() {
43 // Do nothing if the function has an `await` expression in its body.
44 if function.body()?.syntax().descendants().find_map(ast::AwaitExpr::cast).is_some() {
48 // Remove the `async` keyword plus whitespace after it, if any.
50 let async_token = function.async_token()?;
51 let next_token = async_token.next_token()?;
52 if matches!(next_token.kind(), SyntaxKind::WHITESPACE) {
53 TextRange::new(async_token.text_range().start(), next_token.text_range().end())
55 async_token.text_range()
59 // Otherwise, we may remove the `async` keyword.
61 AssistId("unnecessary_async", AssistKind::QuickFix),
62 "Remove unnecessary async",
65 // Remove async on the function definition.
66 edit.replace(async_range, "");
68 // Remove all `.await`s from calls to the function we remove `async` from.
69 if let Some(fn_def) = ctx.sema.to_def(&function) {
70 for await_expr in find_all_references(ctx, &Definition::Function(fn_def))
71 // Keep only references that correspond NameRefs.
72 .filter_map(|(_, reference)| match reference.name {
73 NameLike::NameRef(nameref) => Some(nameref),
76 // Keep only references that correspond to await expressions
77 .filter_map(|nameref| find_await_expression(ctx, &nameref))
79 if let Some(await_token) = &await_expr.await_token() {
80 edit.replace(await_token.text_range(), "");
82 if let Some(dot_token) = &await_expr.dot_token() {
83 edit.replace(dot_token.text_range(), "");
91 fn find_all_references(
92 ctx: &AssistContext<'_>,
94 ) -> impl Iterator<Item = (FileId, FileReference)> {
95 def.usages(&ctx.sema).all().into_iter().flat_map(|(file_id, references)| {
96 references.into_iter().map(move |reference| (file_id, reference))
100 /// Finds the await expression for the given `NameRef`.
101 /// If no await expression is found, returns None.
102 fn find_await_expression(ctx: &AssistContext<'_>, nameref: &NameRef) -> Option<ast::AwaitExpr> {
103 // From the nameref, walk up the tree to the await expression.
104 let await_expr = if let Some(path) = full_path_of_name_ref(&nameref) {
108 .and_then(ast::PathExpr::cast)?
111 .and_then(ast::CallExpr::cast)?
114 .and_then(ast::AwaitExpr::cast)
120 .and_then(ast::MethodCallExpr::cast)?
123 .and_then(ast::AwaitExpr::cast)
126 ctx.sema.original_ast_node(await_expr?)
133 use crate::tests::{check_assist, check_assist_not_applicable};
136 fn applies_on_empty_function() {
137 check_assist(unnecessary_async, "pub async f$0n f() {}", "pub fn f() {}")
141 fn applies_and_removes_whitespace() {
142 check_assist(unnecessary_async, "pub async f$0n f() {}", "pub fn f() {}")
146 fn does_not_apply_on_non_async_function() {
147 check_assist_not_applicable(unnecessary_async, "pub f$0n f() {}")
151 fn applies_on_function_with_a_non_await_expr() {
152 check_assist(unnecessary_async, "pub async f$0n f() { f2() }", "pub fn f() { f2() }")
156 fn does_not_apply_on_function_with_an_await_expr() {
157 check_assist_not_applicable(unnecessary_async, "pub async f$0n f() { f2().await }")
161 fn applies_and_removes_await_on_reference() {
165 pub async fn f4() { }
166 pub async f$0n f2() { }
167 pub async fn f() { f2().await }
168 pub async fn f3() { f2().await }"#,
170 pub async fn f4() { }
172 pub async fn f() { f2() }
173 pub async fn f3() { f2() }"#,
178 fn applies_and_removes_await_from_within_module() {
182 pub async fn f4() { }
183 mod a { pub async f$0n f2() { } }
184 pub async fn f() { a::f2().await }
185 pub async fn f3() { a::f2().await }"#,
187 pub async fn f4() { }
188 mod a { pub fn f2() { } }
189 pub async fn f() { a::f2() }
190 pub async fn f3() { a::f2() }"#,
195 fn applies_and_removes_await_on_inner_await() {
198 // Ensure that it is the first await on the 3rd line that is removed
200 pub async fn f() { f2().await }
201 pub async f$0n f2() -> i32 { 1 }
202 pub async fn f3() { f4(f2().await).await }
203 pub async fn f4(i: i32) { }"#,
205 pub async fn f() { f2() }
206 pub fn f2() -> i32 { 1 }
207 pub async fn f3() { f4(f2()).await }
208 pub async fn f4(i: i32) { }"#,
213 fn applies_and_removes_await_on_outer_await() {
216 // Ensure that it is the second await on the 3rd line that is removed
218 pub async fn f() { f2().await }
219 pub async f$0n f2(i: i32) { }
220 pub async fn f3() { f2(f4().await).await }
221 pub async fn f4() -> i32 { 1 }"#,
223 pub async fn f() { f2() }
224 pub fn f2(i: i32) { }
225 pub async fn f3() { f2(f4().await) }
226 pub async fn f4() -> i32 { 1 }"#,
231 fn applies_on_method_call() {
236 impl S { pub async f$0n f2(&self) { } }
237 pub async fn f(s: &S) { s.f2().await }"#,
240 impl S { pub fn f2(&self) { } }
241 pub async fn f(s: &S) { s.f2() }"#,
246 fn does_not_apply_on_function_with_a_nested_await_expr() {
247 check_assist_not_applicable(
249 "async f$0n f() { if true { loop { f2().await } } }",
254 fn does_not_apply_when_not_on_prototype() {
255 check_assist_not_applicable(unnecessary_async, "pub async fn f() { $0f2() }")