|
1 | 1 | use hir::HirDisplay;
|
2 | 2 | use syntax::{
|
3 |
| - ast::{Expr, GenericArg}, |
| 3 | + ast::{Expr, GenericArg, GenericArgList}, |
4 | 4 | ast::{LetStmt, Type::InferType},
|
5 | 5 | AstNode, TextRange,
|
6 | 6 | };
|
@@ -35,21 +35,7 @@ pub(crate) fn replace_turbofish_with_explicit_type(
|
35 | 35 |
|
36 | 36 | let initializer = let_stmt.initializer()?;
|
37 | 37 |
|
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)?; |
53 | 39 |
|
54 | 40 | // Find range of ::<_>
|
55 | 41 | let colon2 = generic_args.coloncolon_token()?;
|
@@ -117,6 +103,26 @@ pub(crate) fn replace_turbofish_with_explicit_type(
|
117 | 103 | None
|
118 | 104 | }
|
119 | 105 |
|
| 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 | + |
120 | 126 | #[cfg(test)]
|
121 | 127 | mod tests {
|
122 | 128 | use super::*;
|
@@ -325,6 +331,34 @@ fn make<T>() -> HasDefault<T> {}
|
325 | 331 | fn main() {
|
326 | 332 | let a: HasDefault<bool> = make();
|
327 | 333 | }
|
| 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 | +} |
328 | 362 | "#,
|
329 | 363 | );
|
330 | 364 | }
|
|
0 commit comments