Fix IfExpr then branch suggest

- And add logic operation suggest

Example
---
In the old implementation, it always suggested conditions,
this is a lot of noise, e.g `contract_checks()~(use std::intrinsics::contract_checks) const fn() -> bool`

```rust
fn foo() {
    if true {
        c$0
    }
}
```
This commit is contained in:
A4-Tacks 2025-09-14 00:42:11 +08:00
parent 259a01d73d
commit 07f33e2b81
No known key found for this signature in database
GPG key ID: DBD861323040663B
3 changed files with 82 additions and 8 deletions

View file

@ -637,6 +637,9 @@ fn expected_type_and_name<'db>(
.or_else(|| it.rhs().and_then(|rhs| sema.type_of_expr(&rhs)))
.map(TypeInfo::original);
(ty, None)
} else if let Some(ast::BinaryOp::LogicOp(_)) = it.op_kind() {
let ty = sema.type_of_expr(&it.clone().into()).map(TypeInfo::original);
(ty, None)
} else {
(None, None)
}
@ -707,9 +710,13 @@ fn expected_type_and_name<'db>(
(ty, None)
},
ast::IfExpr(it) => {
let ty = it.condition()
.and_then(|e| sema.type_of_expr(&e))
.map(TypeInfo::original);
let ty = if let Some(body) = it.then_branch()
&& token.text_range().end() > body.syntax().text_range().start()
{
sema.type_of_expr(&body.into())
} else {
it.condition().and_then(|e| sema.type_of_expr(&e))
}.map(TypeInfo::original);
(ty, None)
},
ast::IdentPat(it) => {