diff options
Diffstat (limited to 'docs/user/generated_assists.adoc')
-rw-r--r-- | docs/user/generated_assists.adoc | 973 |
1 files changed, 973 insertions, 0 deletions
diff --git a/docs/user/generated_assists.adoc b/docs/user/generated_assists.adoc new file mode 100644 index 000000000..b8cdfb1cd --- /dev/null +++ b/docs/user/generated_assists.adoc | |||
@@ -0,0 +1,973 @@ | |||
1 | [discrete] | ||
2 | === `add_custom_impl` | ||
3 | |||
4 | Adds impl block for derived trait. | ||
5 | |||
6 | .Before | ||
7 | ```rust | ||
8 | #[derive(Deb┃ug, Display)] | ||
9 | struct S; | ||
10 | ``` | ||
11 | |||
12 | .After | ||
13 | ```rust | ||
14 | #[derive(Display)] | ||
15 | struct S; | ||
16 | |||
17 | impl Debug for S { | ||
18 | $0 | ||
19 | } | ||
20 | ``` | ||
21 | |||
22 | |||
23 | [discrete] | ||
24 | === `add_derive` | ||
25 | |||
26 | Adds a new `#[derive()]` clause to a struct or enum. | ||
27 | |||
28 | .Before | ||
29 | ```rust | ||
30 | struct Point { | ||
31 | x: u32, | ||
32 | y: u32,┃ | ||
33 | } | ||
34 | ``` | ||
35 | |||
36 | .After | ||
37 | ```rust | ||
38 | #[derive($0)] | ||
39 | struct Point { | ||
40 | x: u32, | ||
41 | y: u32, | ||
42 | } | ||
43 | ``` | ||
44 | |||
45 | |||
46 | [discrete] | ||
47 | === `add_explicit_type` | ||
48 | |||
49 | Specify type for a let binding. | ||
50 | |||
51 | .Before | ||
52 | ```rust | ||
53 | fn main() { | ||
54 | let x┃ = 92; | ||
55 | } | ||
56 | ``` | ||
57 | |||
58 | .After | ||
59 | ```rust | ||
60 | fn main() { | ||
61 | let x: i32 = 92; | ||
62 | } | ||
63 | ``` | ||
64 | |||
65 | |||
66 | [discrete] | ||
67 | === `add_from_impl_for_enum` | ||
68 | |||
69 | Adds a From impl for an enum variant with one tuple field. | ||
70 | |||
71 | .Before | ||
72 | ```rust | ||
73 | enum A { ┃One(u32) } | ||
74 | ``` | ||
75 | |||
76 | .After | ||
77 | ```rust | ||
78 | enum A { One(u32) } | ||
79 | |||
80 | impl From<u32> for A { | ||
81 | fn from(v: u32) -> Self { | ||
82 | A::One(v) | ||
83 | } | ||
84 | } | ||
85 | ``` | ||
86 | |||
87 | |||
88 | [discrete] | ||
89 | === `add_function` | ||
90 | |||
91 | Adds a stub function with a signature matching the function under the cursor. | ||
92 | |||
93 | .Before | ||
94 | ```rust | ||
95 | struct Baz; | ||
96 | fn baz() -> Baz { Baz } | ||
97 | fn foo() { | ||
98 | bar┃("", baz()); | ||
99 | } | ||
100 | |||
101 | ``` | ||
102 | |||
103 | .After | ||
104 | ```rust | ||
105 | struct Baz; | ||
106 | fn baz() -> Baz { Baz } | ||
107 | fn foo() { | ||
108 | bar("", baz()); | ||
109 | } | ||
110 | |||
111 | fn bar(arg: &str, baz: Baz) { | ||
112 | ${0:todo!()} | ||
113 | } | ||
114 | |||
115 | ``` | ||
116 | |||
117 | |||
118 | [discrete] | ||
119 | === `add_hash` | ||
120 | |||
121 | Adds a hash to a raw string literal. | ||
122 | |||
123 | .Before | ||
124 | ```rust | ||
125 | fn main() { | ||
126 | r#"Hello,┃ World!"#; | ||
127 | } | ||
128 | ``` | ||
129 | |||
130 | .After | ||
131 | ```rust | ||
132 | fn main() { | ||
133 | r##"Hello, World!"##; | ||
134 | } | ||
135 | ``` | ||
136 | |||
137 | |||
138 | [discrete] | ||
139 | === `add_impl` | ||
140 | |||
141 | Adds a new inherent impl for a type. | ||
142 | |||
143 | .Before | ||
144 | ```rust | ||
145 | struct Ctx<T: Clone> { | ||
146 | data: T,┃ | ||
147 | } | ||
148 | ``` | ||
149 | |||
150 | .After | ||
151 | ```rust | ||
152 | struct Ctx<T: Clone> { | ||
153 | data: T, | ||
154 | } | ||
155 | |||
156 | impl<T: Clone> Ctx<T> { | ||
157 | $0 | ||
158 | } | ||
159 | ``` | ||
160 | |||
161 | |||
162 | [discrete] | ||
163 | === `add_impl_default_members` | ||
164 | |||
165 | Adds scaffold for overriding default impl members. | ||
166 | |||
167 | .Before | ||
168 | ```rust | ||
169 | trait Trait { | ||
170 | Type X; | ||
171 | fn foo(&self); | ||
172 | fn bar(&self) {} | ||
173 | } | ||
174 | |||
175 | impl Trait for () { | ||
176 | Type X = (); | ||
177 | fn foo(&self) {}┃ | ||
178 | |||
179 | } | ||
180 | ``` | ||
181 | |||
182 | .After | ||
183 | ```rust | ||
184 | trait Trait { | ||
185 | Type X; | ||
186 | fn foo(&self); | ||
187 | fn bar(&self) {} | ||
188 | } | ||
189 | |||
190 | impl Trait for () { | ||
191 | Type X = (); | ||
192 | fn foo(&self) {} | ||
193 | $0fn bar(&self) {} | ||
194 | |||
195 | } | ||
196 | ``` | ||
197 | |||
198 | |||
199 | [discrete] | ||
200 | === `add_impl_missing_members` | ||
201 | |||
202 | Adds scaffold for required impl members. | ||
203 | |||
204 | .Before | ||
205 | ```rust | ||
206 | trait Trait<T> { | ||
207 | Type X; | ||
208 | fn foo(&self) -> T; | ||
209 | fn bar(&self) {} | ||
210 | } | ||
211 | |||
212 | impl Trait<u32> for () {┃ | ||
213 | |||
214 | } | ||
215 | ``` | ||
216 | |||
217 | .After | ||
218 | ```rust | ||
219 | trait Trait<T> { | ||
220 | Type X; | ||
221 | fn foo(&self) -> T; | ||
222 | fn bar(&self) {} | ||
223 | } | ||
224 | |||
225 | impl Trait<u32> for () { | ||
226 | fn foo(&self) -> u32 { | ||
227 | ${0:todo!()} | ||
228 | } | ||
229 | |||
230 | } | ||
231 | ``` | ||
232 | |||
233 | |||
234 | [discrete] | ||
235 | === `add_new` | ||
236 | |||
237 | Adds a new inherent impl for a type. | ||
238 | |||
239 | .Before | ||
240 | ```rust | ||
241 | struct Ctx<T: Clone> { | ||
242 | data: T,┃ | ||
243 | } | ||
244 | ``` | ||
245 | |||
246 | .After | ||
247 | ```rust | ||
248 | struct Ctx<T: Clone> { | ||
249 | data: T, | ||
250 | } | ||
251 | |||
252 | impl<T: Clone> Ctx<T> { | ||
253 | fn $0new(data: T) -> Self { Self { data } } | ||
254 | } | ||
255 | |||
256 | ``` | ||
257 | |||
258 | |||
259 | [discrete] | ||
260 | === `add_turbo_fish` | ||
261 | |||
262 | Adds `::<_>` to a call of a generic method or function. | ||
263 | |||
264 | .Before | ||
265 | ```rust | ||
266 | fn make<T>() -> T { todo!() } | ||
267 | fn main() { | ||
268 | let x = make┃(); | ||
269 | } | ||
270 | ``` | ||
271 | |||
272 | .After | ||
273 | ```rust | ||
274 | fn make<T>() -> T { todo!() } | ||
275 | fn main() { | ||
276 | let x = make::<${0:_}>(); | ||
277 | } | ||
278 | ``` | ||
279 | |||
280 | |||
281 | [discrete] | ||
282 | === `apply_demorgan` | ||
283 | |||
284 | Apply [De Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws). | ||
285 | This transforms expressions of the form `!l || !r` into `!(l && r)`. | ||
286 | This also works with `&&`. This assist can only be applied with the cursor | ||
287 | on either `||` or `&&`, with both operands being a negation of some kind. | ||
288 | This means something of the form `!x` or `x != y`. | ||
289 | |||
290 | .Before | ||
291 | ```rust | ||
292 | fn main() { | ||
293 | if x != 4 ||┃ !y {} | ||
294 | } | ||
295 | ``` | ||
296 | |||
297 | .After | ||
298 | ```rust | ||
299 | fn main() { | ||
300 | if !(x == 4 && y) {} | ||
301 | } | ||
302 | ``` | ||
303 | |||
304 | |||
305 | [discrete] | ||
306 | === `auto_import` | ||
307 | |||
308 | If the name is unresolved, provides all possible imports for it. | ||
309 | |||
310 | .Before | ||
311 | ```rust | ||
312 | fn main() { | ||
313 | let map = HashMap┃::new(); | ||
314 | } | ||
315 | ``` | ||
316 | |||
317 | .After | ||
318 | ```rust | ||
319 | use std::collections::HashMap; | ||
320 | |||
321 | fn main() { | ||
322 | let map = HashMap::new(); | ||
323 | } | ||
324 | ``` | ||
325 | |||
326 | |||
327 | [discrete] | ||
328 | === `change_lifetime_anon_to_named` | ||
329 | |||
330 | Change an anonymous lifetime to a named lifetime. | ||
331 | |||
332 | .Before | ||
333 | ```rust | ||
334 | impl Cursor<'_┃> { | ||
335 | fn node(self) -> &SyntaxNode { | ||
336 | match self { | ||
337 | Cursor::Replace(node) | Cursor::Before(node) => node, | ||
338 | } | ||
339 | } | ||
340 | } | ||
341 | ``` | ||
342 | |||
343 | .After | ||
344 | ```rust | ||
345 | impl<'a> Cursor<'a> { | ||
346 | fn node(self) -> &SyntaxNode { | ||
347 | match self { | ||
348 | Cursor::Replace(node) | Cursor::Before(node) => node, | ||
349 | } | ||
350 | } | ||
351 | } | ||
352 | ``` | ||
353 | |||
354 | |||
355 | [discrete] | ||
356 | === `change_return_type_to_result` | ||
357 | |||
358 | Change the function's return type to Result. | ||
359 | |||
360 | .Before | ||
361 | ```rust | ||
362 | fn foo() -> i32┃ { 42i32 } | ||
363 | ``` | ||
364 | |||
365 | .After | ||
366 | ```rust | ||
367 | fn foo() -> Result<i32, ${0:_}> { Ok(42i32) } | ||
368 | ``` | ||
369 | |||
370 | |||
371 | [discrete] | ||
372 | === `change_visibility` | ||
373 | |||
374 | Adds or changes existing visibility specifier. | ||
375 | |||
376 | .Before | ||
377 | ```rust | ||
378 | ┃fn frobnicate() {} | ||
379 | ``` | ||
380 | |||
381 | .After | ||
382 | ```rust | ||
383 | pub(crate) fn frobnicate() {} | ||
384 | ``` | ||
385 | |||
386 | |||
387 | [discrete] | ||
388 | === `convert_to_guarded_return` | ||
389 | |||
390 | Replace a large conditional with a guarded return. | ||
391 | |||
392 | .Before | ||
393 | ```rust | ||
394 | fn main() { | ||
395 | ┃if cond { | ||
396 | foo(); | ||
397 | bar(); | ||
398 | } | ||
399 | } | ||
400 | ``` | ||
401 | |||
402 | .After | ||
403 | ```rust | ||
404 | fn main() { | ||
405 | if !cond { | ||
406 | return; | ||
407 | } | ||
408 | foo(); | ||
409 | bar(); | ||
410 | } | ||
411 | ``` | ||
412 | |||
413 | |||
414 | [discrete] | ||
415 | === `fill_match_arms` | ||
416 | |||
417 | Adds missing clauses to a `match` expression. | ||
418 | |||
419 | .Before | ||
420 | ```rust | ||
421 | enum Action { Move { distance: u32 }, Stop } | ||
422 | |||
423 | fn handle(action: Action) { | ||
424 | match action { | ||
425 | ┃ | ||
426 | } | ||
427 | } | ||
428 | ``` | ||
429 | |||
430 | .After | ||
431 | ```rust | ||
432 | enum Action { Move { distance: u32 }, Stop } | ||
433 | |||
434 | fn handle(action: Action) { | ||
435 | match action { | ||
436 | $0Action::Move { distance } => {} | ||
437 | Action::Stop => {} | ||
438 | } | ||
439 | } | ||
440 | ``` | ||
441 | |||
442 | |||
443 | [discrete] | ||
444 | === `fix_visibility` | ||
445 | |||
446 | Makes inaccessible item public. | ||
447 | |||
448 | .Before | ||
449 | ```rust | ||
450 | mod m { | ||
451 | fn frobnicate() {} | ||
452 | } | ||
453 | fn main() { | ||
454 | m::frobnicate┃() {} | ||
455 | } | ||
456 | ``` | ||
457 | |||
458 | .After | ||
459 | ```rust | ||
460 | mod m { | ||
461 | $0pub(crate) fn frobnicate() {} | ||
462 | } | ||
463 | fn main() { | ||
464 | m::frobnicate() {} | ||
465 | } | ||
466 | ``` | ||
467 | |||
468 | |||
469 | [discrete] | ||
470 | === `flip_binexpr` | ||
471 | |||
472 | Flips operands of a binary expression. | ||
473 | |||
474 | .Before | ||
475 | ```rust | ||
476 | fn main() { | ||
477 | let _ = 90 +┃ 2; | ||
478 | } | ||
479 | ``` | ||
480 | |||
481 | .After | ||
482 | ```rust | ||
483 | fn main() { | ||
484 | let _ = 2 + 90; | ||
485 | } | ||
486 | ``` | ||
487 | |||
488 | |||
489 | [discrete] | ||
490 | === `flip_comma` | ||
491 | |||
492 | Flips two comma-separated items. | ||
493 | |||
494 | .Before | ||
495 | ```rust | ||
496 | fn main() { | ||
497 | ((1, 2),┃ (3, 4)); | ||
498 | } | ||
499 | ``` | ||
500 | |||
501 | .After | ||
502 | ```rust | ||
503 | fn main() { | ||
504 | ((3, 4), (1, 2)); | ||
505 | } | ||
506 | ``` | ||
507 | |||
508 | |||
509 | [discrete] | ||
510 | === `flip_trait_bound` | ||
511 | |||
512 | Flips two trait bounds. | ||
513 | |||
514 | .Before | ||
515 | ```rust | ||
516 | fn foo<T: Clone +┃ Copy>() { } | ||
517 | ``` | ||
518 | |||
519 | .After | ||
520 | ```rust | ||
521 | fn foo<T: Copy + Clone>() { } | ||
522 | ``` | ||
523 | |||
524 | |||
525 | [discrete] | ||
526 | === `inline_local_variable` | ||
527 | |||
528 | Inlines local variable. | ||
529 | |||
530 | .Before | ||
531 | ```rust | ||
532 | fn main() { | ||
533 | let x┃ = 1 + 2; | ||
534 | x * 4; | ||
535 | } | ||
536 | ``` | ||
537 | |||
538 | .After | ||
539 | ```rust | ||
540 | fn main() { | ||
541 | (1 + 2) * 4; | ||
542 | } | ||
543 | ``` | ||
544 | |||
545 | |||
546 | [discrete] | ||
547 | === `introduce_variable` | ||
548 | |||
549 | Extracts subexpression into a variable. | ||
550 | |||
551 | .Before | ||
552 | ```rust | ||
553 | fn main() { | ||
554 | ┃(1 + 2)┃ * 4; | ||
555 | } | ||
556 | ``` | ||
557 | |||
558 | .After | ||
559 | ```rust | ||
560 | fn main() { | ||
561 | let $0var_name = (1 + 2); | ||
562 | var_name * 4; | ||
563 | } | ||
564 | ``` | ||
565 | |||
566 | |||
567 | [discrete] | ||
568 | === `invert_if` | ||
569 | |||
570 | Apply invert_if | ||
571 | This transforms if expressions of the form `if !x {A} else {B}` into `if x {B} else {A}` | ||
572 | This also works with `!=`. This assist can only be applied with the cursor | ||
573 | on `if`. | ||
574 | |||
575 | .Before | ||
576 | ```rust | ||
577 | fn main() { | ||
578 | if┃ !y { A } else { B } | ||
579 | } | ||
580 | ``` | ||
581 | |||
582 | .After | ||
583 | ```rust | ||
584 | fn main() { | ||
585 | if y { B } else { A } | ||
586 | } | ||
587 | ``` | ||
588 | |||
589 | |||
590 | [discrete] | ||
591 | === `make_raw_string` | ||
592 | |||
593 | Adds `r#` to a plain string literal. | ||
594 | |||
595 | .Before | ||
596 | ```rust | ||
597 | fn main() { | ||
598 | "Hello,┃ World!"; | ||
599 | } | ||
600 | ``` | ||
601 | |||
602 | .After | ||
603 | ```rust | ||
604 | fn main() { | ||
605 | r#"Hello, World!"#; | ||
606 | } | ||
607 | ``` | ||
608 | |||
609 | |||
610 | [discrete] | ||
611 | === `make_usual_string` | ||
612 | |||
613 | Turns a raw string into a plain string. | ||
614 | |||
615 | .Before | ||
616 | ```rust | ||
617 | fn main() { | ||
618 | r#"Hello,┃ "World!""#; | ||
619 | } | ||
620 | ``` | ||
621 | |||
622 | .After | ||
623 | ```rust | ||
624 | fn main() { | ||
625 | "Hello, \"World!\""; | ||
626 | } | ||
627 | ``` | ||
628 | |||
629 | |||
630 | [discrete] | ||
631 | === `merge_imports` | ||
632 | |||
633 | Merges two imports with a common prefix. | ||
634 | |||
635 | .Before | ||
636 | ```rust | ||
637 | use std::┃fmt::Formatter; | ||
638 | use std::io; | ||
639 | ``` | ||
640 | |||
641 | .After | ||
642 | ```rust | ||
643 | use std::{fmt::Formatter, io}; | ||
644 | ``` | ||
645 | |||
646 | |||
647 | [discrete] | ||
648 | === `merge_match_arms` | ||
649 | |||
650 | Merges identical match arms. | ||
651 | |||
652 | .Before | ||
653 | ```rust | ||
654 | enum Action { Move { distance: u32 }, Stop } | ||
655 | |||
656 | fn handle(action: Action) { | ||
657 | match action { | ||
658 | ┃Action::Move(..) => foo(), | ||
659 | Action::Stop => foo(), | ||
660 | } | ||
661 | } | ||
662 | ``` | ||
663 | |||
664 | .After | ||
665 | ```rust | ||
666 | enum Action { Move { distance: u32 }, Stop } | ||
667 | |||
668 | fn handle(action: Action) { | ||
669 | match action { | ||
670 | Action::Move(..) | Action::Stop => foo(), | ||
671 | } | ||
672 | } | ||
673 | ``` | ||
674 | |||
675 | |||
676 | [discrete] | ||
677 | === `move_arm_cond_to_match_guard` | ||
678 | |||
679 | Moves if expression from match arm body into a guard. | ||
680 | |||
681 | .Before | ||
682 | ```rust | ||
683 | enum Action { Move { distance: u32 }, Stop } | ||
684 | |||
685 | fn handle(action: Action) { | ||
686 | match action { | ||
687 | Action::Move { distance } => ┃if distance > 10 { foo() }, | ||
688 | _ => (), | ||
689 | } | ||
690 | } | ||
691 | ``` | ||
692 | |||
693 | .After | ||
694 | ```rust | ||
695 | enum Action { Move { distance: u32 }, Stop } | ||
696 | |||
697 | fn handle(action: Action) { | ||
698 | match action { | ||
699 | Action::Move { distance } if distance > 10 => foo(), | ||
700 | _ => (), | ||
701 | } | ||
702 | } | ||
703 | ``` | ||
704 | |||
705 | |||
706 | [discrete] | ||
707 | === `move_bounds_to_where_clause` | ||
708 | |||
709 | Moves inline type bounds to a where clause. | ||
710 | |||
711 | .Before | ||
712 | ```rust | ||
713 | fn apply<T, U, ┃F: FnOnce(T) -> U>(f: F, x: T) -> U { | ||
714 | f(x) | ||
715 | } | ||
716 | ``` | ||
717 | |||
718 | .After | ||
719 | ```rust | ||
720 | fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U { | ||
721 | f(x) | ||
722 | } | ||
723 | ``` | ||
724 | |||
725 | |||
726 | [discrete] | ||
727 | === `move_guard_to_arm_body` | ||
728 | |||
729 | Moves match guard into match arm body. | ||
730 | |||
731 | .Before | ||
732 | ```rust | ||
733 | enum Action { Move { distance: u32 }, Stop } | ||
734 | |||
735 | fn handle(action: Action) { | ||
736 | match action { | ||
737 | Action::Move { distance } ┃if distance > 10 => foo(), | ||
738 | _ => (), | ||
739 | } | ||
740 | } | ||
741 | ``` | ||
742 | |||
743 | .After | ||
744 | ```rust | ||
745 | enum Action { Move { distance: u32 }, Stop } | ||
746 | |||
747 | fn handle(action: Action) { | ||
748 | match action { | ||
749 | Action::Move { distance } => if distance > 10 { foo() }, | ||
750 | _ => (), | ||
751 | } | ||
752 | } | ||
753 | ``` | ||
754 | |||
755 | |||
756 | [discrete] | ||
757 | === `remove_dbg` | ||
758 | |||
759 | Removes `dbg!()` macro call. | ||
760 | |||
761 | .Before | ||
762 | ```rust | ||
763 | fn main() { | ||
764 | ┃dbg!(92); | ||
765 | } | ||
766 | ``` | ||
767 | |||
768 | .After | ||
769 | ```rust | ||
770 | fn main() { | ||
771 | 92; | ||
772 | } | ||
773 | ``` | ||
774 | |||
775 | |||
776 | [discrete] | ||
777 | === `remove_hash` | ||
778 | |||
779 | Removes a hash from a raw string literal. | ||
780 | |||
781 | .Before | ||
782 | ```rust | ||
783 | fn main() { | ||
784 | r#"Hello,┃ World!"#; | ||
785 | } | ||
786 | ``` | ||
787 | |||
788 | .After | ||
789 | ```rust | ||
790 | fn main() { | ||
791 | r"Hello, World!"; | ||
792 | } | ||
793 | ``` | ||
794 | |||
795 | |||
796 | [discrete] | ||
797 | === `remove_mut` | ||
798 | |||
799 | Removes the `mut` keyword. | ||
800 | |||
801 | .Before | ||
802 | ```rust | ||
803 | impl Walrus { | ||
804 | fn feed(&mut┃ self, amount: u32) {} | ||
805 | } | ||
806 | ``` | ||
807 | |||
808 | .After | ||
809 | ```rust | ||
810 | impl Walrus { | ||
811 | fn feed(&self, amount: u32) {} | ||
812 | } | ||
813 | ``` | ||
814 | |||
815 | |||
816 | [discrete] | ||
817 | === `reorder_fields` | ||
818 | |||
819 | Reorder the fields of record literals and record patterns in the same order as in | ||
820 | the definition. | ||
821 | |||
822 | .Before | ||
823 | ```rust | ||
824 | struct Foo {foo: i32, bar: i32}; | ||
825 | const test: Foo = ┃Foo {bar: 0, foo: 1} | ||
826 | ``` | ||
827 | |||
828 | .After | ||
829 | ```rust | ||
830 | struct Foo {foo: i32, bar: i32}; | ||
831 | const test: Foo = Foo {foo: 1, bar: 0} | ||
832 | ``` | ||
833 | |||
834 | |||
835 | [discrete] | ||
836 | === `replace_if_let_with_match` | ||
837 | |||
838 | Replaces `if let` with an else branch with a `match` expression. | ||
839 | |||
840 | .Before | ||
841 | ```rust | ||
842 | enum Action { Move { distance: u32 }, Stop } | ||
843 | |||
844 | fn handle(action: Action) { | ||
845 | ┃if let Action::Move { distance } = action { | ||
846 | foo(distance) | ||
847 | } else { | ||
848 | bar() | ||
849 | } | ||
850 | } | ||
851 | ``` | ||
852 | |||
853 | .After | ||
854 | ```rust | ||
855 | enum Action { Move { distance: u32 }, Stop } | ||
856 | |||
857 | fn handle(action: Action) { | ||
858 | match action { | ||
859 | Action::Move { distance } => foo(distance), | ||
860 | _ => bar(), | ||
861 | } | ||
862 | } | ||
863 | ``` | ||
864 | |||
865 | |||
866 | [discrete] | ||
867 | === `replace_let_with_if_let` | ||
868 | |||
869 | Replaces `let` with an `if-let`. | ||
870 | |||
871 | .Before | ||
872 | ```rust | ||
873 | |||
874 | fn main(action: Action) { | ||
875 | ┃let x = compute(); | ||
876 | } | ||
877 | |||
878 | fn compute() -> Option<i32> { None } | ||
879 | ``` | ||
880 | |||
881 | .After | ||
882 | ```rust | ||
883 | |||
884 | fn main(action: Action) { | ||
885 | if let Some(x) = compute() { | ||
886 | } | ||
887 | } | ||
888 | |||
889 | fn compute() -> Option<i32> { None } | ||
890 | ``` | ||
891 | |||
892 | |||
893 | [discrete] | ||
894 | === `replace_qualified_name_with_use` | ||
895 | |||
896 | Adds a use statement for a given fully-qualified name. | ||
897 | |||
898 | .Before | ||
899 | ```rust | ||
900 | fn process(map: std::collections::┃HashMap<String, String>) {} | ||
901 | ``` | ||
902 | |||
903 | .After | ||
904 | ```rust | ||
905 | use std::collections::HashMap; | ||
906 | |||
907 | fn process(map: HashMap<String, String>) {} | ||
908 | ``` | ||
909 | |||
910 | |||
911 | [discrete] | ||
912 | === `replace_unwrap_with_match` | ||
913 | |||
914 | Replaces `unwrap` a `match` expression. Works for Result and Option. | ||
915 | |||
916 | .Before | ||
917 | ```rust | ||
918 | enum Result<T, E> { Ok(T), Err(E) } | ||
919 | fn main() { | ||
920 | let x: Result<i32, i32> = Result::Ok(92); | ||
921 | let y = x.┃unwrap(); | ||
922 | } | ||
923 | ``` | ||
924 | |||
925 | .After | ||
926 | ```rust | ||
927 | enum Result<T, E> { Ok(T), Err(E) } | ||
928 | fn main() { | ||
929 | let x: Result<i32, i32> = Result::Ok(92); | ||
930 | let y = match x { | ||
931 | Ok(a) => a, | ||
932 | $0_ => unreachable!(), | ||
933 | }; | ||
934 | } | ||
935 | ``` | ||
936 | |||
937 | |||
938 | [discrete] | ||
939 | === `split_import` | ||
940 | |||
941 | Wraps the tail of import into braces. | ||
942 | |||
943 | .Before | ||
944 | ```rust | ||
945 | use std::┃collections::HashMap; | ||
946 | ``` | ||
947 | |||
948 | .After | ||
949 | ```rust | ||
950 | use std::{collections::HashMap}; | ||
951 | ``` | ||
952 | |||
953 | |||
954 | [discrete] | ||
955 | === `unwrap_block` | ||
956 | |||
957 | This assist removes if...else, for, while and loop control statements to just keep the body. | ||
958 | |||
959 | .Before | ||
960 | ```rust | ||
961 | fn foo() { | ||
962 | if true {┃ | ||
963 | println!("foo"); | ||
964 | } | ||
965 | } | ||
966 | ``` | ||
967 | |||
968 | .After | ||
969 | ```rust | ||
970 | fn foo() { | ||
971 | println!("foo"); | ||
972 | } | ||
973 | ``` | ||