Skip to content

Commit a003d4d

Browse files
authored
Merge pull request #20661 from A4-Tacks/suggest-if-body
Fix IfExpr branches suggests
2 parents 3705f71 + 07f33e2 commit a003d4d

File tree

3 files changed

+82
-8
lines changed

3 files changed

+82
-8
lines changed

crates/ide-completion/src/context/analysis.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,9 @@ fn expected_type_and_name<'db>(
637637
.or_else(|| it.rhs().and_then(|rhs| sema.type_of_expr(&rhs)))
638638
.map(TypeInfo::original);
639639
(ty, None)
640+
} else if let Some(ast::BinaryOp::LogicOp(_)) = it.op_kind() {
641+
let ty = sema.type_of_expr(&it.clone().into()).map(TypeInfo::original);
642+
(ty, None)
640643
} else {
641644
(None, None)
642645
}
@@ -707,9 +710,13 @@ fn expected_type_and_name<'db>(
707710
(ty, None)
708711
},
709712
ast::IfExpr(it) => {
710-
let ty = it.condition()
711-
.and_then(|e| sema.type_of_expr(&e))
712-
.map(TypeInfo::original);
713+
let ty = if let Some(body) = it.then_branch()
714+
&& token.text_range().end() > body.syntax().text_range().start()
715+
{
716+
sema.type_of_expr(&body.into())
717+
} else {
718+
it.condition().and_then(|e| sema.type_of_expr(&e))
719+
}.map(TypeInfo::original);
713720
(ty, None)
714721
},
715722
ast::IdentPat(it) => {

crates/ide-completion/src/context/tests.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,62 @@ fn foo() {
278278
)
279279
}
280280

281+
#[test]
282+
fn expected_type_if_let_chain_bool() {
283+
check_expected_type_and_name(
284+
r#"
285+
fn foo() {
286+
let f = Foo::Quux;
287+
if let c = f && $0 { }
288+
}
289+
"#,
290+
expect![[r#"ty: bool, name: ?"#]],
291+
);
292+
}
293+
294+
#[test]
295+
fn expected_type_if_condition() {
296+
check_expected_type_and_name(
297+
r#"
298+
fn foo() {
299+
if a$0 { }
300+
}
301+
"#,
302+
expect![[r#"ty: bool, name: ?"#]],
303+
);
304+
}
305+
306+
#[test]
307+
fn expected_type_if_body() {
308+
check_expected_type_and_name(
309+
r#"
310+
enum Foo { Bar, Baz, Quux }
311+
312+
fn foo() {
313+
let _: Foo = if true {
314+
$0
315+
};
316+
}
317+
"#,
318+
expect![[r#"ty: Foo, name: ?"#]],
319+
);
320+
321+
check_expected_type_and_name(
322+
r#"
323+
enum Foo { Bar, Baz, Quux }
324+
325+
fn foo() {
326+
let _: Foo = if true {
327+
Foo::Bar
328+
} else {
329+
$0
330+
};
331+
}
332+
"#,
333+
expect![[r#"ty: Foo, name: ?"#]],
334+
);
335+
}
336+
281337
#[test]
282338
fn expected_type_fn_ret_without_leading_char() {
283339
cov_mark::check!(expected_type_fn_ret_without_leading_char);
@@ -526,3 +582,16 @@ fn foo() {
526582
expect![[r#"ty: State, name: ?"#]],
527583
);
528584
}
585+
586+
#[test]
587+
fn expected_type_logic_op() {
588+
check_expected_type_and_name(
589+
r#"
590+
enum State { Stop }
591+
fn foo() {
592+
true && $0;
593+
}
594+
"#,
595+
expect![[r#"ty: bool, name: ?"#]],
596+
);
597+
}

crates/ide-completion/src/tests/expression.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,6 @@ fn complete_in_block() {
271271
sn macro_rules
272272
sn pd
273273
sn ppd
274-
ex false
275-
ex true
276274
"#]],
277275
)
278276
}
@@ -1668,7 +1666,7 @@ fn foo() { let x = if foo {} $0; let y = 92; }
16681666
fn foo() { let x = if foo {} $0 else {}; }
16691667
"#,
16701668
expect![[r#"
1671-
fn foo fn()
1669+
fn foo() fn()
16721670
bt u32 u32
16731671
kw async
16741672
kw const
@@ -1710,7 +1708,7 @@ fn foo() { let x = if foo {} $0 else {}; }
17101708
fn foo() { let x = if foo {} $0 else if true {}; }
17111709
"#,
17121710
expect![[r#"
1713-
fn foo fn()
1711+
fn foo() fn()
17141712
bt u32 u32
17151713
kw async
17161714
kw const
@@ -1795,7 +1793,7 @@ fn foo() { let x = if foo {} el$0 else if true {} else {}; }
17951793
fn foo() { let x = if foo {} $0 else if true {} else {}; }
17961794
"#,
17971795
expect![[r#"
1798-
fn foo fn()
1796+
fn foo() fn()
17991797
bt u32 u32
18001798
kw async
18011799
kw const

0 commit comments

Comments
 (0)