Skip to content

Commit 6181154

Browse files
committed
Add some more inline_call tests
1 parent d308f17 commit 6181154

File tree

2 files changed

+86
-18
lines changed

2 files changed

+86
-18
lines changed

crates/ide_assists/src/handlers/inline_call.rs

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,16 @@ use crate::{
1717
// Inlines a function or method body.
1818
//
1919
// ```
20-
// fn add(a: u32, b: u32) -> u32 { a + b }
20+
// fn align(a: u32, b: u32) -> u32 { (a + b - 1) & !(b - 1) }
2121
// fn main() {
22-
// let x = add$0(1, 2);
22+
// let x = align$0(1, 2);
2323
// }
2424
// ```
2525
// ->
2626
// ```
27-
// fn add(a: u32, b: u32) -> u32 { a + b }
27+
// fn align(a: u32, b: u32) -> u32 { (a + b - 1) & !(b - 1) }
2828
// fn main() {
29-
// let x = {
30-
// let a = 1;
31-
// let b = 2;
32-
// a + b
33-
// };
29+
// let x = { let b = 2; (1 + b - 1) & !(b - 1) };
3430
// }
3531
// ```
3632
pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
@@ -169,9 +165,8 @@ pub(crate) fn inline_(
169165
if expr.path().and_then(|path| path.as_single_name_ref()).is_some()
170166
) =>
171167
{
172-
let expr = expr.syntax().clone_for_update();
173168
usages.into_iter().for_each(|usage| {
174-
ted::replace(usage, &expr);
169+
ted::replace(usage, &expr.syntax().clone_for_update());
175170
});
176171
}
177172
// cant inline, emit a let statement
@@ -415,6 +410,83 @@ fn main() {
415410
this.0 = 0;
416411
};
417412
}
413+
"#,
414+
);
415+
}
416+
417+
#[test]
418+
fn function_single_use_expr_in_param() {
419+
check_assist(
420+
inline_call,
421+
r#"
422+
fn double(x: u32) -> u32 {
423+
2 * x
424+
}
425+
fn main() {
426+
let x = 51;
427+
let x = double$0(10 + x);
428+
}
429+
"#,
430+
r#"
431+
fn double(x: u32) -> u32 {
432+
2 * x
433+
}
434+
fn main() {
435+
let x = 51;
436+
let x = 2 * 10 + x;
437+
}
438+
"#,
439+
);
440+
}
441+
442+
#[test]
443+
fn function_multi_use_expr_in_param() {
444+
check_assist(
445+
inline_call,
446+
r#"
447+
fn square(x: u32) -> u32 {
448+
x * x
449+
}
450+
fn main() {
451+
let x = 51;
452+
let y = square$0(10 + x);
453+
}
454+
"#,
455+
r#"
456+
fn square(x: u32) -> u32 {
457+
x * x
458+
}
459+
fn main() {
460+
let x = 51;
461+
let y = { let x = 10 + x;
462+
x * x
463+
};
464+
}
465+
"#,
466+
);
467+
}
468+
469+
#[test]
470+
fn function_multi_use_local_in_param() {
471+
check_assist(
472+
inline_call,
473+
r#"
474+
fn square(x: u32) -> u32 {
475+
x * x
476+
}
477+
fn main() {
478+
let local = 51;
479+
let y = square$0(local);
480+
}
481+
"#,
482+
r#"
483+
fn square(x: u32) -> u32 {
484+
x * x
485+
}
486+
fn main() {
487+
let local = 51;
488+
let y = local * local;
489+
}
418490
"#,
419491
);
420492
}

crates/ide_assists/src/tests/generated.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -923,19 +923,15 @@ fn doctest_inline_call() {
923923
check_doc_test(
924924
"inline_call",
925925
r#####"
926-
fn add(a: u32, b: u32) -> u32 { a + b }
926+
fn align(a: u32, b: u32) -> u32 { (a + b - 1) & !(b - 1) }
927927
fn main() {
928-
let x = add$0(1, 2);
928+
let x = align$0(1, 2);
929929
}
930930
"#####,
931931
r#####"
932-
fn add(a: u32, b: u32) -> u32 { a + b }
932+
fn align(a: u32, b: u32) -> u32 { (a + b - 1) & !(b - 1) }
933933
fn main() {
934-
let x = {
935-
let a = 1;
936-
let b = 2;
937-
a + b
938-
};
934+
let x = { let b = 2; (1 + b - 1) & !(b - 1) };
939935
}
940936
"#####,
941937
)

0 commit comments

Comments
 (0)