Skip to content

Commit b393059

Browse files
committed
calc depth
1 parent ef9c4b6 commit b393059

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

crates/ide_assists/src/handlers/replace_if_let_with_match.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use syntax::{
1212
};
1313

1414
use crate::{
15-
utils::{does_pat_match_variant, unwrap_trivial_block},
15+
utils::{does_nested_pattern, does_pat_match_variant, unwrap_trivial_block},
1616
AssistContext, AssistId, AssistKind, Assists,
1717
};
1818

@@ -143,6 +143,8 @@ fn make_else_arm(
143143
Some((it, pat)) => {
144144
if does_pat_match_variant(pat, &it.sad_pattern()) {
145145
it.happy_pattern_wildcard()
146+
} else if does_nested_pattern(pat) {
147+
make::wildcard_pat().into()
146148
} else {
147149
it.sad_pattern()
148150
}
@@ -596,6 +598,7 @@ fn foo(x: Result<i32, ()>) {
596598
Ok(Some(_)) => (),
597599
_ => (),
598600
}
601+
}
599602
"#,
600603
);
601604
}

crates/ide_assists/src/utils.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,43 @@ pub(crate) fn does_pat_match_variant(pat: &ast::Pat, var: &ast::Pat) -> bool {
285285
pat_head == var_head
286286
}
287287

288+
pub(crate) fn does_nested_pattern(pat: &ast::Pat) -> bool {
289+
let depth = calc_depth(pat, 0);
290+
291+
if 1 < depth {
292+
return true;
293+
}
294+
false
295+
}
296+
297+
fn calc_depth(pat: &ast::Pat, mut depth: usize) -> usize {
298+
match pat {
299+
ast::Pat::IdentPat(_)
300+
| ast::Pat::BoxPat(_)
301+
| ast::Pat::RestPat(_)
302+
| ast::Pat::LiteralPat(_)
303+
| ast::Pat::MacroPat(_)
304+
| ast::Pat::OrPat(_)
305+
| ast::Pat::ParenPat(_)
306+
| ast::Pat::PathPat(_)
307+
| ast::Pat::WildcardPat(_)
308+
| ast::Pat::RangePat(_)
309+
| ast::Pat::RecordPat(_)
310+
| ast::Pat::RefPat(_)
311+
| ast::Pat::SlicePat(_)
312+
| ast::Pat::TuplePat(_)
313+
| ast::Pat::ConstBlockPat(_) => 1,
314+
315+
// TODO implement
316+
ast::Pat::TupleStructPat(pat) => {
317+
for p in pat.fields() {
318+
depth += calc_depth(&p, depth + 1);
319+
}
320+
depth
321+
}
322+
}
323+
}
324+
288325
// Uses a syntax-driven approach to find any impl blocks for the struct that
289326
// exist within the module/file
290327
//

0 commit comments

Comments
 (0)