diff options
| author | A4-Tacks <wdsjxhno1001@163.com> | 2025-09-24 14:36:09 +0800 |
|---|---|---|
| committer | A4-Tacks <wdsjxhno1001@163.com> | 2025-09-24 14:36:09 +0800 |
| commit | 6e897949a5a1fb3bb70c5166cced9b24af879882 (patch) | |
| tree | 5c69288a1978bf3ab4f659b4a831747cf1d69421 /src | |
| parent | 1393e638bd29f24203c5f153a863a5c18464e304 (diff) | |
| download | rust-6e897949a5a1fb3bb70c5166cced9b24af879882.tar.gz rust-6e897949a5a1fb3bb70c5166cced9b24af879882.zip | |
Fix applicable on if-let-chain for invert_if
Example
---
```rust
fn f() { i$0f x && let Some(_) = Some(1) { 1 } else { 0 } }
```
**Before this PR**:
```rust
fn f() { if !(x && let Some(_) = Some(1)) { 0 } else { 1 } }
```
**After this PR**:
Assist not applicable
Diffstat (limited to 'src')
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-assists/src/handlers/invert_if.rs | 12 | ||||
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-db/src/syntax_helpers/node_ext.rs | 5 |
2 files changed, 13 insertions, 4 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/invert_if.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/invert_if.rs index d198870b023..7576d2fab97 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/invert_if.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/invert_if.rs @@ -125,6 +125,18 @@ mod tests { } #[test] + fn invert_if_doesnt_apply_with_if_let_chain() { + check_assist_not_applicable( + invert_if, + "fn f() { i$0f x && let Some(_) = Some(1) { 1 } else { 0 } }", + ); + check_assist_not_applicable( + invert_if, + "fn f() { i$0f let Some(_) = Some(1) && x { 1 } else { 0 } }", + ); + } + + #[test] fn invert_if_option_case() { check_assist( invert_if, diff --git a/src/tools/rust-analyzer/crates/ide-db/src/syntax_helpers/node_ext.rs b/src/tools/rust-analyzer/crates/ide-db/src/syntax_helpers/node_ext.rs index cefd8fd4967..e1d140730ed 100644 --- a/src/tools/rust-analyzer/crates/ide-db/src/syntax_helpers/node_ext.rs +++ b/src/tools/rust-analyzer/crates/ide-db/src/syntax_helpers/node_ext.rs @@ -265,10 +265,7 @@ pub fn is_pattern_cond(expr: ast::Expr) -> bool { ast::Expr::BinExpr(expr) if expr.op_kind() == Some(ast::BinaryOp::LogicOp(ast::LogicOp::And)) => { - expr.lhs() - .map(is_pattern_cond) - .or_else(|| expr.rhs().map(is_pattern_cond)) - .unwrap_or(false) + expr.lhs().map_or(false, is_pattern_cond) || expr.rhs().map_or(false, is_pattern_cond) } ast::Expr::ParenExpr(expr) => expr.expr().is_some_and(is_pattern_cond), ast::Expr::LetExpr(_) => true, |
