Skip to content

Commit 9889678

Browse files
committed
Replace Option<Vec<&'a ast::pat>> with Vec<&'a ast::Pat>
1 parent 3f0b630 commit 9889678

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

src/expr.rs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ struct ControlFlow<'a> {
730730
block: &'a ast::Block,
731731
else_block: Option<&'a ast::Expr>,
732732
label: Option<ast::Label>,
733-
pats: Option<Vec<&'a ast::Pat>>,
733+
pats: Vec<&'a ast::Pat>,
734734
keyword: &'a str,
735735
matcher: &'a str,
736736
connector: &'a str,
@@ -744,7 +744,7 @@ fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option<ControlFlow>
744744
match expr.node {
745745
ast::ExprKind::If(ref cond, ref if_block, ref else_block) => Some(ControlFlow::new_if(
746746
cond,
747-
None,
747+
vec![],
748748
if_block,
749749
else_block.as_ref().map(|e| &**e),
750750
expr_type == ExprType::SubExpression,
@@ -754,7 +754,7 @@ fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option<ControlFlow>
754754
ast::ExprKind::IfLet(ref pat, ref cond, ref if_block, ref else_block) => {
755755
Some(ControlFlow::new_if(
756756
cond,
757-
Some(ptr_vec_to_ref_vec(pat)),
757+
ptr_vec_to_ref_vec(pat),
758758
if_block,
759759
else_block.as_ref().map(|e| &**e),
760760
expr_type == ExprType::SubExpression,
@@ -768,30 +768,39 @@ fn to_control_flow(expr: &ast::Expr, expr_type: ExprType) -> Option<ControlFlow>
768768
ast::ExprKind::Loop(ref block, label) => {
769769
Some(ControlFlow::new_loop(block, label, expr.span))
770770
}
771-
ast::ExprKind::While(ref cond, ref block, label) => {
772-
Some(ControlFlow::new_while(None, cond, block, label, expr.span))
773-
}
771+
ast::ExprKind::While(ref cond, ref block, label) => Some(ControlFlow::new_while(
772+
vec![],
773+
cond,
774+
block,
775+
label,
776+
expr.span,
777+
)),
774778
ast::ExprKind::WhileLet(ref pat, ref cond, ref block, label) => Some(
775-
ControlFlow::new_while(Some(ptr_vec_to_ref_vec(pat)), cond, block, label, expr.span),
779+
ControlFlow::new_while(ptr_vec_to_ref_vec(pat), cond, block, label, expr.span),
776780
),
777781
_ => None,
778782
}
779783
}
780784

785+
fn choose_matcher(pats: &[&ast::Pat]) -> &'static str {
786+
if pats.is_empty() {
787+
""
788+
} else {
789+
"let"
790+
}
791+
}
792+
781793
impl<'a> ControlFlow<'a> {
782794
fn new_if(
783795
cond: &'a ast::Expr,
784-
pats: Option<Vec<&'a ast::Pat>>,
796+
pats: Vec<&'a ast::Pat>,
785797
block: &'a ast::Block,
786798
else_block: Option<&'a ast::Expr>,
787799
allow_single_line: bool,
788800
nested_if: bool,
789801
span: Span,
790802
) -> ControlFlow<'a> {
791-
let matcher = match pats {
792-
Some(..) => "let",
793-
None => "",
794-
};
803+
let matcher = choose_matcher(&pats);
795804
ControlFlow {
796805
cond: Some(cond),
797806
block,
@@ -813,7 +822,7 @@ impl<'a> ControlFlow<'a> {
813822
block,
814823
else_block: None,
815824
label,
816-
pats: None,
825+
pats: vec![],
817826
keyword: "loop",
818827
matcher: "",
819828
connector: "",
@@ -824,16 +833,13 @@ impl<'a> ControlFlow<'a> {
824833
}
825834

826835
fn new_while(
827-
pats: Option<Vec<&'a ast::Pat>>,
836+
pats: Vec<&'a ast::Pat>,
828837
cond: &'a ast::Expr,
829838
block: &'a ast::Block,
830839
label: Option<ast::Label>,
831840
span: Span,
832841
) -> ControlFlow<'a> {
833-
let matcher = match pats {
834-
Some(..) => "let",
835-
None => "",
836-
};
842+
let matcher = choose_matcher(&pats);
837843
ControlFlow {
838844
cond: Some(cond),
839845
block,
@@ -861,7 +867,7 @@ impl<'a> ControlFlow<'a> {
861867
block,
862868
else_block: None,
863869
label,
864-
pats: Some(vec![pat]),
870+
pats: vec![pat],
865871
keyword: "for",
866872
matcher: "",
867873
connector: " in",
@@ -926,7 +932,7 @@ impl<'a> ControlFlow<'a> {
926932
debug!("rewrite_pat_expr {:?} {:?} {:?}", shape, self.pats, expr);
927933

928934
let cond_shape = shape.offset_left(offset)?;
929-
if let Some(ref pat) = self.pats {
935+
if !self.pats.is_empty() {
930936
let matcher = if self.matcher.is_empty() {
931937
self.matcher.to_owned()
932938
} else {
@@ -935,7 +941,7 @@ impl<'a> ControlFlow<'a> {
935941
let pat_shape = cond_shape
936942
.offset_left(matcher.len())?
937943
.sub_width(self.connector.len())?;
938-
let pat_string = rewrite_multiple_patterns(context, pat, pat_shape)?;
944+
let pat_string = rewrite_multiple_patterns(context, &self.pats, pat_shape)?;
939945
let result = format!("{}{}{}", matcher, pat_string, self.connector);
940946
return rewrite_assign_rhs(context, result, expr, cond_shape);
941947
}
@@ -1036,15 +1042,17 @@ impl<'a> ControlFlow<'a> {
10361042
context
10371043
.snippet_provider
10381044
.span_after(mk_sp(lo, self.span.hi()), self.keyword.trim()),
1039-
self.pats.as_ref().map_or(cond_span.lo(), |p| {
1045+
if self.pats.is_empty() {
1046+
cond_span.lo()
1047+
} else {
10401048
if self.matcher.is_empty() {
1041-
p[0].span.lo()
1049+
self.pats[0].span.lo()
10421050
} else {
10431051
context
10441052
.snippet_provider
10451053
.span_before(self.span, self.matcher.trim())
10461054
}
1047-
}),
1055+
},
10481056
);
10491057

10501058
let between_kwd_cond_comment = extract_comment(between_kwd_cond, context, shape);
@@ -1131,7 +1139,7 @@ impl<'a> Rewrite for ControlFlow<'a> {
11311139
ast::ExprKind::IfLet(ref pat, ref cond, ref if_block, ref next_else_block) => {
11321140
ControlFlow::new_if(
11331141
cond,
1134-
Some(ptr_vec_to_ref_vec(pat)),
1142+
ptr_vec_to_ref_vec(pat),
11351143
if_block,
11361144
next_else_block.as_ref().map(|e| &**e),
11371145
false,
@@ -1142,7 +1150,7 @@ impl<'a> Rewrite for ControlFlow<'a> {
11421150
ast::ExprKind::If(ref cond, ref if_block, ref next_else_block) => {
11431151
ControlFlow::new_if(
11441152
cond,
1145-
None,
1153+
vec![],
11461154
if_block,
11471155
next_else_block.as_ref().map(|e| &**e),
11481156
false,

0 commit comments

Comments
 (0)