Skip to content

Commit 22b15a9

Browse files
authored
Merge pull request #20817 from A4-Tacks/explicit-ty-param-in-let
Fix not applicable on param in let-stmt for add_explicit_type
2 parents bc569d0 + ea9ba34 commit 22b15a9

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use either::Either;
12
use hir::HirDisplay;
23
use ide_db::syntax_helpers::node_ext::walk_ty;
34
use syntax::ast::{self, AstNode, LetStmt, Param};
@@ -20,7 +21,8 @@ use crate::{AssistContext, AssistId, Assists};
2021
// }
2122
// ```
2223
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 {
2426
let cursor_in_range = {
2527
let eq_range = let_stmt.eq_token()?.text_range();
2628
ctx.offset() < eq_range.start()
@@ -31,7 +33,7 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
3133
}
3234

3335
(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 {
3537
if param.syntax().ancestors().nth(2).and_then(ast::ClosureExpr::cast).is_none() {
3638
cov_mark::hit!(add_explicit_type_not_applicable_in_fn_param);
3739
return None;
@@ -298,6 +300,20 @@ fn f() {
298300
let x: i32 = y;
299301
};
300302
}
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+
}
301317
"#,
302318
);
303319
}

0 commit comments

Comments
 (0)