]> git.lizzy.rs Git - rust.git/blob - crates/ide_assists/src/handlers/inline_call.rs
Respect coercions in `inline_call`
[rust.git] / crates / ide_assists / src / handlers / inline_call.rs
1 use ast::make;
2 use hir::{HasSource, PathResolution};
3 use ide_db::{defs::Definition, search::FileReference};
4 use itertools::izip;
5 use syntax::{
6     ast::{self, edit::AstNodeEdit, ArgListOwner},
7     ted, AstNode,
8 };
9
10 use crate::{
11     assist_context::{AssistContext, Assists},
12     AssistId, AssistKind,
13 };
14
15 // Assist: inline_call
16 //
17 // Inlines a function or method body creating a `let` statement per parameter unless the parameter
18 // can be inlined. The parameter will be inlined either if it the supplied argument is a simple local
19 // or if the parameter is only accessed inside the function body once.
20 //
21 // ```
22 // # //- minicore: option
23 // fn foo(name: Option<&str>) {
24 //     let name = name.unwrap$0();
25 // }
26 // ```
27 // ->
28 // ```
29 // fn foo(name: Option<&str>) {
30 //     let name = match name {
31 //             Some(val) => val,
32 //             None => panic!("called `Option::unwrap()` on a `None` value"),
33 //         };
34 // }
35 // ```
36 pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
37     let (label, function, arguments, expr) =
38         if let Some(path_expr) = ctx.find_node_at_offset::<ast::PathExpr>() {
39             let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;
40             let path = path_expr.path()?;
41
42             let function = match ctx.sema.resolve_path(&path)? {
43                 PathResolution::Def(hir::ModuleDef::Function(f))
44                 | PathResolution::AssocItem(hir::AssocItem::Function(f)) => f,
45                 _ => return None,
46             };
47             (
48                 format!("Inline `{}`", path),
49                 function,
50                 call.arg_list()?.args().collect(),
51                 ast::Expr::CallExpr(call),
52             )
53         } else {
54             let name_ref: ast::NameRef = ctx.find_node_at_offset()?;
55             let call = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
56             let receiver = call.receiver()?;
57             let function = ctx.sema.resolve_method_call(&call)?;
58             let mut arguments = vec![receiver];
59             arguments.extend(call.arg_list()?.args());
60             (format!("Inline `{}`", name_ref), function, arguments, ast::Expr::MethodCallExpr(call))
61         };
62
63     inline_(acc, ctx, label, function, arguments, expr)
64 }
65
66 pub(crate) fn inline_(
67     acc: &mut Assists,
68     ctx: &AssistContext,
69     label: String,
70     function: hir::Function,
71     arg_list: Vec<ast::Expr>,
72     expr: ast::Expr,
73 ) -> Option<()> {
74     let hir::InFile { value: function_source, file_id } = function.source(ctx.db())?;
75     let param_list = function_source.param_list()?;
76     let mut assoc_fn_params = function.assoc_fn_params(ctx.sema.db).into_iter();
77
78     let mut params = Vec::new();
79     if let Some(self_param) = param_list.self_param() {
80         // FIXME this should depend on the receiver as well as the self_param
81         params.push((
82             make::ident_pat(
83                 self_param.amp_token().is_some(),
84                 self_param.mut_token().is_some(),
85                 make::name("this"),
86             )
87             .into(),
88             None,
89             assoc_fn_params.next()?,
90         ));
91     }
92     for param in param_list.params() {
93         params.push((param.pat()?, param.ty(), assoc_fn_params.next()?));
94     }
95
96     if arg_list.len() != params.len() {
97         // Can't inline the function because they've passed the wrong number of
98         // arguments to this function
99         cov_mark::hit!(inline_call_incorrect_number_of_arguments);
100         return None;
101     }
102
103     let body = function_source.body()?;
104
105     acc.add(
106         AssistId("inline_call", AssistKind::RefactorInline),
107         label,
108         expr.syntax().text_range(),
109         |builder| {
110             let body = body.clone_for_update();
111
112             let file_id = file_id.original_file(ctx.sema.db);
113             let usages_for_locals = |local| {
114                 Definition::Local(local)
115                     .usages(&ctx.sema)
116                     .all()
117                     .references
118                     .remove(&file_id)
119                     .unwrap_or_default()
120                     .into_iter()
121             };
122             // Contains the nodes of usages of parameters.
123             // If the inner Vec for a parameter is empty it either means there are no usages or that the parameter
124             // has a pattern that does not allow inlining
125             let param_use_nodes: Vec<Vec<_>> = params
126                 .iter()
127                 .map(|(pat, _, param)| {
128                     if !matches!(pat, ast::Pat::IdentPat(pat) if pat.is_simple_ident()) {
129                         return Vec::new();
130                     }
131                     usages_for_locals(param.as_local(ctx.sema.db))
132                         .map(|FileReference { name, range, .. }| match name {
133                             ast::NameLike::NameRef(_) => body
134                                 .syntax()
135                                 .covering_element(range)
136                                 .ancestors()
137                                 .nth(3)
138                                 .and_then(ast::PathExpr::cast),
139                             _ => None,
140                         })
141                         .collect::<Option<Vec<_>>>()
142                         .unwrap_or_default()
143                 })
144                 .collect();
145
146             // Rewrite `self` to `this`
147             if param_list.self_param().is_some() {
148                 let this = || make::name_ref("this").syntax().clone_for_update();
149                 usages_for_locals(params[0].2.as_local(ctx.sema.db))
150                     .flat_map(|FileReference { name, range, .. }| match name {
151                         ast::NameLike::NameRef(_) => Some(body.syntax().covering_element(range)),
152                         _ => None,
153                     })
154                     .for_each(|it| {
155                         ted::replace(it, &this());
156                     })
157             }
158
159             // Inline parameter expressions or generate `let` statements depending on whether inlining works or not.
160             for ((pat, param_ty, _), usages, expr) in izip!(params, param_use_nodes, arg_list).rev()
161             {
162                 let expr_is_name_ref = matches!(&expr,
163                     ast::Expr::PathExpr(expr)
164                         if expr.path().and_then(|path| path.as_single_name_ref()).is_some()
165                 );
166                 match &*usages {
167                     // inline single use closure arguments
168                     [usage]
169                         if matches!(expr, ast::Expr::ClosureExpr(_))
170                             && usage.syntax().parent().and_then(ast::Expr::cast).is_some() =>
171                     {
172                         cov_mark::hit!(inline_call_inline_closure);
173                         let expr = make::expr_paren(expr);
174                         ted::replace(usage.syntax(), expr.syntax().clone_for_update());
175                     }
176                     // inline single use literals
177                     [usage] if matches!(expr, ast::Expr::Literal(_)) => {
178                         cov_mark::hit!(inline_call_inline_literal);
179                         ted::replace(usage.syntax(), expr.syntax().clone_for_update());
180                     }
181                     // inline direct local arguments
182                     [_, ..] if expr_is_name_ref => {
183                         cov_mark::hit!(inline_call_inline_locals);
184                         usages.into_iter().for_each(|usage| {
185                             ted::replace(usage.syntax(), &expr.syntax().clone_for_update());
186                         });
187                     }
188                     // cant inline, emit a let statement
189                     _ => {
190                         let ty = ctx
191                             .sema
192                             .type_of_expr_with_coercion(&expr)
193                             .map_or(false, |(_, coerced)| coerced.is_some())
194                             .then(|| param_ty)
195                             .flatten();
196                         body.push_front(
197                             make::let_stmt(pat, ty, Some(expr)).clone_for_update().into(),
198                         )
199                     }
200                 }
201             }
202
203             let original_indentation = expr.indent_level();
204             let replacement = body.reset_indent().indent(original_indentation);
205
206             let replacement = match replacement.tail_expr() {
207                 Some(expr) if replacement.statements().next().is_none() => expr,
208                 _ => ast::Expr::BlockExpr(replacement),
209             };
210             builder.replace_ast(expr, replacement);
211         },
212     )
213 }
214
215 #[cfg(test)]
216 mod tests {
217     use crate::tests::{check_assist, check_assist_not_applicable};
218
219     use super::*;
220
221     #[test]
222     fn no_args_or_return_value_gets_inlined_without_block() {
223         check_assist(
224             inline_call,
225             r#"
226 fn foo() { println!("Hello, World!"); }
227 fn main() {
228     fo$0o();
229 }
230 "#,
231             r#"
232 fn foo() { println!("Hello, World!"); }
233 fn main() {
234     { println!("Hello, World!"); };
235 }
236 "#,
237         );
238     }
239
240     #[test]
241     fn not_applicable_when_incorrect_number_of_parameters_are_provided() {
242         cov_mark::check!(inline_call_incorrect_number_of_arguments);
243         check_assist_not_applicable(
244             inline_call,
245             r#"
246 fn add(a: u32, b: u32) -> u32 { a + b }
247 fn main() { let x = add$0(42); }
248 "#,
249         );
250     }
251
252     #[test]
253     fn args_with_side_effects() {
254         check_assist(
255             inline_call,
256             r#"
257 fn foo(name: String) {
258     println!("Hello, {}!", name);
259 }
260 fn main() {
261     foo$0(String::from("Michael"));
262 }
263 "#,
264             r#"
265 fn foo(name: String) {
266     println!("Hello, {}!", name);
267 }
268 fn main() {
269     {
270         let name = String::from("Michael");
271         println!("Hello, {}!", name);
272     };
273 }
274 "#,
275         );
276     }
277
278     #[test]
279     fn function_with_multiple_statements() {
280         check_assist(
281             inline_call,
282             r#"
283 fn foo(a: u32, b: u32) -> u32 {
284     let x = a + b;
285     let y = x - b;
286     x * y
287 }
288
289 fn main() {
290     let x = foo$0(1, 2);
291 }
292 "#,
293             r#"
294 fn foo(a: u32, b: u32) -> u32 {
295     let x = a + b;
296     let y = x - b;
297     x * y
298 }
299
300 fn main() {
301     let x = {
302         let b = 2;
303         let x = 1 + b;
304         let y = x - b;
305         x * y
306     };
307 }
308 "#,
309         );
310     }
311
312     #[test]
313     fn function_with_self_param() {
314         check_assist(
315             inline_call,
316             r#"
317 struct Foo(u32);
318
319 impl Foo {
320     fn add(self, a: u32) -> Self {
321         Foo(self.0 + a)
322     }
323 }
324
325 fn main() {
326     let x = Foo::add$0(Foo(3), 2);
327 }
328 "#,
329             r#"
330 struct Foo(u32);
331
332 impl Foo {
333     fn add(self, a: u32) -> Self {
334         Foo(self.0 + a)
335     }
336 }
337
338 fn main() {
339     let x = {
340         let this = Foo(3);
341         Foo(this.0 + 2)
342     };
343 }
344 "#,
345         );
346     }
347
348     #[test]
349     fn method_by_val() {
350         check_assist(
351             inline_call,
352             r#"
353 struct Foo(u32);
354
355 impl Foo {
356     fn add(self, a: u32) -> Self {
357         Foo(self.0 + a)
358     }
359 }
360
361 fn main() {
362     let x = Foo(3).add$0(2);
363 }
364 "#,
365             r#"
366 struct Foo(u32);
367
368 impl Foo {
369     fn add(self, a: u32) -> Self {
370         Foo(self.0 + a)
371     }
372 }
373
374 fn main() {
375     let x = {
376         let this = Foo(3);
377         Foo(this.0 + 2)
378     };
379 }
380 "#,
381         );
382     }
383
384     #[test]
385     fn method_by_ref() {
386         check_assist(
387             inline_call,
388             r#"
389 struct Foo(u32);
390
391 impl Foo {
392     fn add(&self, a: u32) -> Self {
393         Foo(self.0 + a)
394     }
395 }
396
397 fn main() {
398     let x = Foo(3).add$0(2);
399 }
400 "#,
401             r#"
402 struct Foo(u32);
403
404 impl Foo {
405     fn add(&self, a: u32) -> Self {
406         Foo(self.0 + a)
407     }
408 }
409
410 fn main() {
411     let x = {
412         let ref this = Foo(3);
413         Foo(this.0 + 2)
414     };
415 }
416 "#,
417         );
418     }
419
420     #[test]
421     fn method_by_ref_mut() {
422         check_assist(
423             inline_call,
424             r#"
425 struct Foo(u32);
426
427 impl Foo {
428     fn clear(&mut self) {
429         self.0 = 0;
430     }
431 }
432
433 fn main() {
434     let mut foo = Foo(3);
435     foo.clear$0();
436 }
437 "#,
438             r#"
439 struct Foo(u32);
440
441 impl Foo {
442     fn clear(&mut self) {
443         self.0 = 0;
444     }
445 }
446
447 fn main() {
448     let mut foo = Foo(3);
449     {
450         let ref mut this = foo;
451         this.0 = 0;
452     };
453 }
454 "#,
455         );
456     }
457
458     #[test]
459     fn function_multi_use_expr_in_param() {
460         check_assist(
461             inline_call,
462             r#"
463 fn square(x: u32) -> u32 {
464     x * x
465 }
466 fn main() {
467     let x = 51;
468     let y = square$0(10 + x);
469 }
470 "#,
471             r#"
472 fn square(x: u32) -> u32 {
473     x * x
474 }
475 fn main() {
476     let x = 51;
477     let y = {
478         let x = 10 + x;
479         x * x
480     };
481 }
482 "#,
483         );
484     }
485
486     #[test]
487     fn function_use_local_in_param() {
488         cov_mark::check!(inline_call_inline_locals);
489         check_assist(
490             inline_call,
491             r#"
492 fn square(x: u32) -> u32 {
493     x * x
494 }
495 fn main() {
496     let local = 51;
497     let y = square$0(local);
498 }
499 "#,
500             r#"
501 fn square(x: u32) -> u32 {
502     x * x
503 }
504 fn main() {
505     let local = 51;
506     let y = local * local;
507 }
508 "#,
509         );
510     }
511
512     #[test]
513     fn method_in_impl() {
514         check_assist(
515             inline_call,
516             r#"
517 struct Foo;
518 impl Foo {
519     fn foo(&self) {
520         self;
521         self;
522     }
523     fn bar(&self) {
524         self.foo$0();
525     }
526 }
527 "#,
528             r#"
529 struct Foo;
530 impl Foo {
531     fn foo(&self) {
532         self;
533         self;
534     }
535     fn bar(&self) {
536         {
537             let ref this = self;
538             this;
539             this;
540         };
541     }
542 }
543 "#,
544         );
545     }
546
547     #[test]
548     fn wraps_closure_in_paren() {
549         cov_mark::check!(inline_call_inline_closure);
550         check_assist(
551             inline_call,
552             r#"
553 fn foo(x: fn()) {
554     x();
555 }
556
557 fn main() {
558     foo$0(|| {})
559 }
560 "#,
561             r#"
562 fn foo(x: fn()) {
563     x();
564 }
565
566 fn main() {
567     {
568         (|| {})();
569     }
570 }
571 "#,
572         );
573         check_assist(
574             inline_call,
575             r#"
576 fn foo(x: fn()) {
577     x();
578 }
579
580 fn main() {
581     foo$0(main)
582 }
583 "#,
584             r#"
585 fn foo(x: fn()) {
586     x();
587 }
588
589 fn main() {
590     {
591         main();
592     }
593 }
594 "#,
595         );
596     }
597
598     #[test]
599     fn inline_single_literal_expr() {
600         cov_mark::check!(inline_call_inline_literal);
601         check_assist(
602             inline_call,
603             r#"
604 fn foo(x: u32) -> u32{
605     x
606 }
607
608 fn main() {
609     foo$0(222);
610 }
611 "#,
612             r#"
613 fn foo(x: u32) -> u32{
614     x
615 }
616
617 fn main() {
618     222;
619 }
620 "#,
621         );
622     }
623
624     #[test]
625     fn inline_emits_type_for_coercion() {
626         check_assist(
627             inline_call,
628             r#"
629 fn foo(x: *const u32) -> u32 {
630     x as u32
631 }
632
633 fn main() {
634     foo$0(&222);
635 }
636 "#,
637             r#"
638 fn foo(x: *const u32) -> u32 {
639     x as u32
640 }
641
642 fn main() {
643     {
644         let x: *const u32 = &222;
645         x as u32
646     };
647 }
648 "#,
649         );
650     }
651 }