Skip to content

Commit c2eebd7

Browse files
committed
Auto merge of rust-lang#12877 - zachs18:inline-def-in-macro, r=zachs18
fix: Insert spaces when inlining a function defined in a macro. (partially) fixes rust-lang#12860. This PR (only) addresses the whitespace issue when inlining functions defined in macros. Additionally, the indentation/spacing is not ideal, but works, e.g. ```rs macro_rules! define_function { () => { fn test_function_macro() { if let Some(3) = 3i32.checked_add(0) { println!("3 + 0 == 3"); } } }; } define_function!(); fn main() { test_function_macro(); } // previously became // ... fn main() { ifletSome(3)=3i32.checked_add(0){println!("3 + 0 == 3");}; } // now becomes // ... fn main() { if let Some(3) = 3i32.checked_add(0){ println!("3 + 0 == 3"); }; } ``` The `self` -> `this` problem[^this] is (probably?) a separate problem that I am also looking into. [^this]: As mentioned in [my comment on the above issue](rust-lang/rust-analyzer#12860 (comment)), inlining a method defined in a macro does not properly replace `self` with the new local `this`.
2 parents 7a30f62 + add33b6 commit c2eebd7

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

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

+46-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ide_db::{
77
imports::insert_use::remove_path_if_in_use_stmt,
88
path_transform::PathTransform,
99
search::{FileReference, SearchScope},
10-
syntax_helpers::node_ext::expr_as_name_ref,
10+
syntax_helpers::{insert_whitespace_into_node::insert_ws_into, node_ext::expr_as_name_ref},
1111
RootDatabase,
1212
};
1313
use itertools::{izip, Itertools};
@@ -301,7 +301,16 @@ fn inline(
301301
params: &[(ast::Pat, Option<ast::Type>, hir::Param)],
302302
CallInfo { node, arguments, generic_arg_list }: &CallInfo,
303303
) -> ast::Expr {
304-
let body = fn_body.clone_for_update();
304+
let body = if sema.hir_file_for(fn_body.syntax()).is_macro() {
305+
cov_mark::hit!(inline_call_defined_in_macro);
306+
if let Some(body) = ast::BlockExpr::cast(insert_ws_into(fn_body.syntax().clone())) {
307+
body
308+
} else {
309+
fn_body.clone_for_update()
310+
}
311+
} else {
312+
fn_body.clone_for_update()
313+
};
305314
let usages_for_locals = |local| {
306315
Definition::Local(local)
307316
.usages(sema)
@@ -1144,6 +1153,41 @@ fn bar() -> u32 {
11441153
x
11451154
}) + foo()
11461155
}
1156+
"#,
1157+
)
1158+
}
1159+
1160+
#[test]
1161+
fn inline_call_defined_in_macro() {
1162+
cov_mark::check!(inline_call_defined_in_macro);
1163+
check_assist(
1164+
inline_call,
1165+
r#"
1166+
macro_rules! define_foo {
1167+
() => { fn foo() -> u32 {
1168+
let x = 0;
1169+
x
1170+
} };
1171+
}
1172+
define_foo!();
1173+
fn bar() -> u32 {
1174+
foo$0()
1175+
}
1176+
"#,
1177+
r#"
1178+
macro_rules! define_foo {
1179+
() => { fn foo() -> u32 {
1180+
let x = 0;
1181+
x
1182+
} };
1183+
}
1184+
define_foo!();
1185+
fn bar() -> u32 {
1186+
{
1187+
let x = 0;
1188+
x
1189+
}
1190+
}
11471191
"#,
11481192
)
11491193
}

0 commit comments

Comments
 (0)