Skip to content

Commit 3c90ed6

Browse files
authored
Merge pull request #21850 from A4-Tacks/add-braces-bin-expr
feat: offer 'add_braces' on bin-expr assignment
2 parents 8c522b6 + c2bb947 commit 3c90ed6

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

crates/ide-assists/src/handlers/add_braces.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ fn get_replacement_node(ctx: &AssistContext<'_>) -> Option<(ParentType, ast::Exp
8484
match parent {
8585
ast::LetStmt(it) => it.initializer()?,
8686
ast::LetExpr(it) => it.expr()?,
87+
ast::BinExpr(it) => it.rhs()?,
8788
ast::Static(it) => it.body()?,
8889
ast::Const(it) => it.body()?,
8990
_ => return None,
@@ -174,6 +175,70 @@ fn foo() {
174175
n + 100
175176
};
176177
}
178+
"#,
179+
);
180+
181+
check_assist(
182+
add_braces,
183+
r#"
184+
fn foo() {
185+
let x;
186+
x =$0 n + 100;
187+
}
188+
"#,
189+
r#"
190+
fn foo() {
191+
let x;
192+
x = {
193+
n + 100
194+
};
195+
}
196+
"#,
197+
);
198+
199+
check_assist(
200+
add_braces,
201+
r#"
202+
fn foo() {
203+
if let x =$0 n + 100 {}
204+
}
205+
"#,
206+
r#"
207+
fn foo() {
208+
if let x = {
209+
n + 100
210+
} {}
211+
}
212+
"#,
213+
);
214+
}
215+
216+
#[test]
217+
fn suggest_add_braces_for_const_initializer() {
218+
check_assist(
219+
add_braces,
220+
r#"
221+
const X: i32 =$0 1 + 2;
222+
"#,
223+
r#"
224+
const X: i32 = {
225+
1 + 2
226+
};
227+
"#,
228+
);
229+
}
230+
231+
#[test]
232+
fn suggest_add_braces_for_static_initializer() {
233+
check_assist(
234+
add_braces,
235+
r#"
236+
static X: i32 $0= 1 + 2;
237+
"#,
238+
r#"
239+
static X: i32 = {
240+
1 + 2
241+
};
177242
"#,
178243
);
179244
}

0 commit comments

Comments
 (0)