Skip to content

Commit 20aec94

Browse files
committed
fix test
1 parent d08ddbe commit 20aec94

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

src/expr.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -710,11 +710,12 @@ impl Rewrite for ast::Stmt {
710710
};
711711

712712
let shape = shape.sub_width(suffix.len())?;
713-
let expr_type = if stmt_is_expr(&self) {
714-
ExprType::SubExpression
715-
} else {
716-
ExprType::Statement
717-
};
713+
let expr_type =
714+
if stmt_is_expr(&self) && context.snippet(self.span).starts_with("if") {
715+
ExprType::SubExpression
716+
} else {
717+
ExprType::Statement
718+
};
718719
format_expr(ex, expr_type, context, shape).map(|s| s + suffix)
719720
}
720721
ast::StmtKind::Mac(..) | ast::StmtKind::Item(..) => None,

tests/source/one_line_if.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
fn plain_if(x: bool) -> u8 {
2+
if x {
3+
0
4+
} else {
5+
1
6+
}
7+
}
8+
9+
fn paren_if(x: bool) -> u8 {
10+
(if x { 0 } else { 1 })
11+
}
12+
13+
fn let_if(x: bool) -> u8 {
14+
let x = if x { 0 } else { 1 };
15+
x
16+
}
17+
18+
fn return_if(x: bool) -> u8 {
19+
return if x {
20+
0
21+
} else {
22+
1
23+
};
24+
}

tests/target/one_line_if.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
fn plain_if(x: bool) -> u8 {
2+
if x { 0 } else { 1 }
3+
}
4+
5+
fn paren_if(x: bool) -> u8 {
6+
(if x { 0 } else { 1 })
7+
}
8+
9+
fn let_if(x: bool) -> u8 {
10+
let x = if x { 0 } else { 1 };
11+
x
12+
}
13+
14+
fn return_if(x: bool) -> u8 {
15+
return if x { 0 } else { 1 };
16+
}

0 commit comments

Comments
 (0)