mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-27 18:26:19 +00:00
Fix not applicable for if-expr in let-stmt
Example
---
```rust
fn main() {
let _x = loop {
if$0 let Ok(x) = Err(92) {
foo(x);
}
};
}
```
**Before**:
Assist not applicable
**After**:
```rust
fn main() {
let _x = loop {
let Ok(x) = Err(92) else { continue };
foo(x);
};
}
```
This commit is contained in:
parent
a98da9f795
commit
4e3b6b38dd
1 changed files with 28 additions and 6 deletions
|
|
@ -1,6 +1,7 @@
|
|||
use std::iter::once;
|
||||
|
||||
use hir::Semantics;
|
||||
use either::Either;
|
||||
use ide_db::{RootDatabase, ty_filter::TryEnum};
|
||||
use syntax::{
|
||||
AstNode,
|
||||
|
|
@ -42,12 +43,9 @@ use crate::{
|
|||
// }
|
||||
// ```
|
||||
pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
||||
if let Some(let_stmt) = ctx.find_node_at_offset() {
|
||||
let_stmt_to_guarded_return(let_stmt, acc, ctx)
|
||||
} else if let Some(if_expr) = ctx.find_node_at_offset() {
|
||||
if_expr_to_guarded_return(if_expr, acc, ctx)
|
||||
} else {
|
||||
None
|
||||
match ctx.find_node_at_offset::<Either<ast::LetStmt, ast::IfExpr>>()? {
|
||||
Either::Left(let_stmt) => let_stmt_to_guarded_return(let_stmt, acc, ctx),
|
||||
Either::Right(if_expr) => if_expr_to_guarded_return(if_expr, acc, ctx),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -379,6 +377,30 @@ fn main() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn convert_if_let_result_inside_let() {
|
||||
check_assist(
|
||||
convert_to_guarded_return,
|
||||
r#"
|
||||
fn main() {
|
||||
let _x = loop {
|
||||
if$0 let Ok(x) = Err(92) {
|
||||
foo(x);
|
||||
}
|
||||
};
|
||||
}
|
||||
"#,
|
||||
r#"
|
||||
fn main() {
|
||||
let _x = loop {
|
||||
let Ok(x) = Err(92) else { continue };
|
||||
foo(x);
|
||||
};
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn convert_if_let_chain_result() {
|
||||
check_assist(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue