1
+ use either:: Either ;
1
2
use hir:: HirDisplay ;
2
3
use ide_db:: syntax_helpers:: node_ext:: walk_ty;
3
4
use syntax:: ast:: { self , AstNode , LetStmt , Param } ;
@@ -20,7 +21,8 @@ use crate::{AssistContext, AssistId, Assists};
20
21
// }
21
22
// ```
22
23
pub ( crate ) fn add_explicit_type ( acc : & mut Assists , ctx : & AssistContext < ' _ > ) -> Option < ( ) > {
23
- let ( ascribed_ty, expr, pat) = if let Some ( let_stmt) = ctx. find_node_at_offset :: < LetStmt > ( ) {
24
+ let syntax_node = ctx. find_node_at_offset :: < Either < LetStmt , Param > > ( ) ?;
25
+ let ( ascribed_ty, expr, pat) = if let Either :: Left ( let_stmt) = syntax_node {
24
26
let cursor_in_range = {
25
27
let eq_range = let_stmt. eq_token ( ) ?. text_range ( ) ;
26
28
ctx. offset ( ) < eq_range. start ( )
@@ -31,7 +33,7 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
31
33
}
32
34
33
35
( let_stmt. ty ( ) , let_stmt. initializer ( ) , let_stmt. pat ( ) ?)
34
- } else if let Some ( param) = ctx . find_node_at_offset :: < Param > ( ) {
36
+ } else if let Either :: Right ( param) = syntax_node {
35
37
if param. syntax ( ) . ancestors ( ) . nth ( 2 ) . and_then ( ast:: ClosureExpr :: cast) . is_none ( ) {
36
38
cov_mark:: hit!( add_explicit_type_not_applicable_in_fn_param) ;
37
39
return None ;
@@ -298,6 +300,20 @@ fn f() {
298
300
let x: i32 = y;
299
301
};
300
302
}
303
+ "# ,
304
+ ) ;
305
+
306
+ check_assist (
307
+ add_explicit_type,
308
+ r#"
309
+ fn f() {
310
+ let f: fn(i32) = |y$0| {};
311
+ }
312
+ "# ,
313
+ r#"
314
+ fn f() {
315
+ let f: fn(i32) = |y: i32| {};
316
+ }
301
317
"# ,
302
318
) ;
303
319
}
0 commit comments