Skip to content

Commit 3a2a137

Browse files
committed
Review 1
1 parent 888157b commit 3a2a137

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

crates/ra_assists/src/fill_match_arms.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt::Write;
2+
use itertools::Itertools;
23

34
use hir::{
45
AdtDef, FieldSource, HasSource,
@@ -9,19 +10,14 @@ use ra_syntax::ast::{self, AstNode};
910
use crate::{AssistCtx, Assist, AssistId};
1011

1112
fn is_trivial_arm(arm: &ast::MatchArm) -> bool {
12-
for (i, p) in arm.pats().enumerate() {
13-
if i > 0 {
14-
return false;
15-
}
16-
17-
match p.kind() {
18-
ast::PatKind::PlaceholderPat(_) => {}
19-
_ => {
20-
return false;
21-
}
22-
};
13+
fn single_pattern(arm: &ast::MatchArm) -> Option<ast::PatKind> {
14+
let (pat,) = arm.pats().collect_tuple()?;
15+
Some(pat.kind())
16+
}
17+
match single_pattern(arm) {
18+
Some(ast::PatKind::PlaceholderPat(..)) => true,
19+
_ => false,
2320
}
24-
return true;
2521
}
2622

2723
pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
@@ -32,12 +28,19 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
3228
// by match postfix complete. Trivial match arm is the catch all arm.
3329
match match_expr.match_arm_list() {
3430
Some(arm_list) => {
35-
for (i, a) in arm_list.arms().enumerate() {
36-
if i > 0 {
37-
return None;
31+
let mut arm_iter = arm_list.arms();
32+
let first = arm_iter.next();
33+
34+
match first {
35+
// If there arm list is empty or there is only one trivial arm, then proceed.
36+
Some(arm) if is_trivial_arm(arm) => {
37+
if arm_iter.next() != None {
38+
return None;
39+
}
3840
}
41+
None => {}
3942

40-
if !is_trivial_arm(a) {
43+
_ => {
4144
return None;
4245
}
4346
}

0 commit comments

Comments
 (0)