Skip to content

Commit bcdacfe

Browse files
Support ? and .await in "Replace turbofish with explicit type"
Now that we use type information this is easy.
1 parent e5e9799 commit bcdacfe

File tree

1 file changed

+50
-16
lines changed

1 file changed

+50
-16
lines changed

crates/ide-assists/src/handlers/replace_turbofish_with_explicit_type.rs

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use hir::HirDisplay;
22
use syntax::{
3-
ast::{Expr, GenericArg},
3+
ast::{Expr, GenericArg, GenericArgList},
44
ast::{LetStmt, Type::InferType},
55
AstNode, TextRange,
66
};
@@ -35,21 +35,7 @@ pub(crate) fn replace_turbofish_with_explicit_type(
3535

3636
let initializer = let_stmt.initializer()?;
3737

38-
let generic_args = match &initializer {
39-
Expr::MethodCallExpr(ce) => ce.generic_arg_list()?,
40-
Expr::CallExpr(ce) => {
41-
if let Expr::PathExpr(pe) = ce.expr()? {
42-
pe.path()?.segment()?.generic_arg_list()?
43-
} else {
44-
cov_mark::hit!(not_applicable_if_non_path_function_call);
45-
return None;
46-
}
47-
}
48-
_ => {
49-
cov_mark::hit!(not_applicable_if_non_function_call_initializer);
50-
return None;
51-
}
52-
};
38+
let generic_args = generic_arg_list(&initializer)?;
5339

5440
// Find range of ::<_>
5541
let colon2 = generic_args.coloncolon_token()?;
@@ -117,6 +103,26 @@ pub(crate) fn replace_turbofish_with_explicit_type(
117103
None
118104
}
119105

106+
fn generic_arg_list(expr: &Expr) -> Option<GenericArgList> {
107+
match expr {
108+
Expr::MethodCallExpr(expr) => expr.generic_arg_list(),
109+
Expr::CallExpr(expr) => {
110+
if let Expr::PathExpr(pe) = expr.expr()? {
111+
pe.path()?.segment()?.generic_arg_list()
112+
} else {
113+
cov_mark::hit!(not_applicable_if_non_path_function_call);
114+
return None;
115+
}
116+
}
117+
Expr::AwaitExpr(expr) => generic_arg_list(&expr.expr()?),
118+
Expr::TryExpr(expr) => generic_arg_list(&expr.expr()?),
119+
_ => {
120+
cov_mark::hit!(not_applicable_if_non_function_call_initializer);
121+
None
122+
}
123+
}
124+
}
125+
120126
#[cfg(test)]
121127
mod tests {
122128
use super::*;
@@ -325,6 +331,34 @@ fn make<T>() -> HasDefault<T> {}
325331
fn main() {
326332
let a: HasDefault<bool> = make();
327333
}
334+
"#,
335+
);
336+
}
337+
338+
#[test]
339+
fn replaces_turbofish_try_await() {
340+
check_assist(
341+
replace_turbofish_with_explicit_type,
342+
r#"
343+
//- minicore: option, future
344+
struct Fut<T>(T);
345+
impl<T> core::future::Future for Fut<T> {
346+
type Output = Option<T>;
347+
}
348+
fn make<T>() -> Fut<T> {}
349+
fn main() {
350+
let a = make$0::<bool>().await?;
351+
}
352+
"#,
353+
r#"
354+
struct Fut<T>(T);
355+
impl<T> core::future::Future for Fut<T> {
356+
type Output = Option<T>;
357+
}
358+
fn make<T>() -> Fut<T> {}
359+
fn main() {
360+
let a: bool = make().await?;
361+
}
328362
"#,
329363
);
330364
}

0 commit comments

Comments
 (0)