]> git.lizzy.rs Git - rust.git/blob - docs/user/assists.md
add Ok wrapping
[rust.git] / docs / user / assists.md
1 # Assists
2
3 Cursor position or selection is signified by `┃` character.
4
5
6 ## `add_custom_impl`
7
8 Adds impl block for derived trait.
9
10 ```rust
11 // BEFORE
12 #[derive(Deb┃ug, Display)]
13 struct S;
14
15 // AFTER
16 #[derive(Display)]
17 struct S;
18
19 impl Debug for S {
20
21 }
22 ```
23
24 ## `add_derive`
25
26 Adds a new `#[derive()]` clause to a struct or enum.
27
28 ```rust
29 // BEFORE
30 struct Point {
31     x: u32,
32     y: u32,┃
33 }
34
35 // AFTER
36 #[derive()]
37 struct Point {
38     x: u32,
39     y: u32,
40 }
41 ```
42
43 ## `add_explicit_type`
44
45 Specify type for a let binding.
46
47 ```rust
48 // BEFORE
49 fn main() {
50     let x┃ = 92;
51 }
52
53 // AFTER
54 fn main() {
55     let x: i32 = 92;
56 }
57 ```
58
59 ## `add_function`
60
61 Adds a stub function with a signature matching the function under the cursor.
62
63 ```rust
64 // BEFORE
65 struct Baz;
66 fn baz() -> Baz { Baz }
67 fn foo() {
68     bar┃("", baz());
69 }
70
71
72 // AFTER
73 struct Baz;
74 fn baz() -> Baz { Baz }
75 fn foo() {
76     bar("", baz());
77 }
78
79 fn bar(arg: &str, baz: Baz) {
80     todo!()
81 }
82
83 ```
84
85 ## `add_hash`
86
87 Adds a hash to a raw string literal.
88
89 ```rust
90 // BEFORE
91 fn main() {
92     r#"Hello,┃ World!"#;
93 }
94
95 // AFTER
96 fn main() {
97     r##"Hello, World!"##;
98 }
99 ```
100
101 ## `add_impl`
102
103 Adds a new inherent impl for a type.
104
105 ```rust
106 // BEFORE
107 struct Ctx<T: Clone> {
108      data: T,┃
109 }
110
111 // AFTER
112 struct Ctx<T: Clone> {
113      data: T,
114 }
115
116 impl<T: Clone> Ctx<T> {
117
118 }
119 ```
120
121 ## `add_impl_default_members`
122
123 Adds scaffold for overriding default impl members.
124
125 ```rust
126 // BEFORE
127 trait Trait {
128     Type X;
129     fn foo(&self);
130     fn bar(&self) {}
131 }
132
133 impl Trait for () {
134     Type X = ();
135     fn foo(&self) {}┃
136
137 }
138
139 // AFTER
140 trait Trait {
141     Type X;
142     fn foo(&self);
143     fn bar(&self) {}
144 }
145
146 impl Trait for () {
147     Type X = ();
148     fn foo(&self) {}
149     fn bar(&self) {}
150
151 }
152 ```
153
154 ## `add_impl_missing_members`
155
156 Adds scaffold for required impl members.
157
158 ```rust
159 // BEFORE
160 trait Trait<T> {
161     Type X;
162     fn foo(&self) -> T;
163     fn bar(&self) {}
164 }
165
166 impl Trait<u32> for () {┃
167
168 }
169
170 // AFTER
171 trait Trait<T> {
172     Type X;
173     fn foo(&self) -> T;
174     fn bar(&self) {}
175 }
176
177 impl Trait<u32> for () {
178     fn foo(&self) -> u32 {
179         todo!()
180     }
181
182 }
183 ```
184
185 ## `add_new`
186
187 Adds a new inherent impl for a type.
188
189 ```rust
190 // BEFORE
191 struct Ctx<T: Clone> {
192      data: T,┃
193 }
194
195 // AFTER
196 struct Ctx<T: Clone> {
197      data: T,
198 }
199
200 impl<T: Clone> Ctx<T> {
201     fn new(data: T) -> Self { Self { data } }
202 }
203
204 ```
205
206 ## `apply_demorgan`
207
208 Apply [De Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws).
209 This transforms expressions of the form `!l || !r` into `!(l && r)`.
210 This also works with `&&`. This assist can only be applied with the cursor
211 on either `||` or `&&`, with both operands being a negation of some kind.
212 This means something of the form `!x` or `x != y`.
213
214 ```rust
215 // BEFORE
216 fn main() {
217     if x != 4 ||┃ !y {}
218 }
219
220 // AFTER
221 fn main() {
222     if !(x == 4 && y) {}
223 }
224 ```
225
226 ## `auto_import`
227
228 If the name is unresolved, provides all possible imports for it.
229
230 ```rust
231 // BEFORE
232 fn main() {
233     let map = HashMap┃::new();
234 }
235
236 // AFTER
237 use std::collections::HashMap;
238
239 fn main() {
240     let map = HashMap::new();
241 }
242 ```
243
244 ## `change_return_type_to_result`
245
246 Change the function's return type to Result.
247
248 ```rust
249 // BEFORE
250 fn foo() -> i32┃ { 42i32 }
251
252 // AFTER
253 fn foo() -> Result<i32, > { Ok(42i32) }
254 ```
255
256 ## `change_visibility`
257
258 Adds or changes existing visibility specifier.
259
260 ```rust
261 // BEFORE
262 ┃fn frobnicate() {}
263
264 // AFTER
265 pub(crate) fn frobnicate() {}
266 ```
267
268 ## `convert_to_guarded_return`
269
270 Replace a large conditional with a guarded return.
271
272 ```rust
273 // BEFORE
274 fn main() {
275     ┃if cond {
276         foo();
277         bar();
278     }
279 }
280
281 // AFTER
282 fn main() {
283     if !cond {
284         return;
285     }
286     foo();
287     bar();
288 }
289 ```
290
291 ## `fill_match_arms`
292
293 Adds missing clauses to a `match` expression.
294
295 ```rust
296 // BEFORE
297 enum Action { Move { distance: u32 }, Stop }
298
299 fn handle(action: Action) {
300     match action {
301         ┃
302     }
303 }
304
305 // AFTER
306 enum Action { Move { distance: u32 }, Stop }
307
308 fn handle(action: Action) {
309     match action {
310         Action::Move { distance } => {}
311         Action::Stop => {}
312     }
313 }
314 ```
315
316 ## `flip_binexpr`
317
318 Flips operands of a binary expression.
319
320 ```rust
321 // BEFORE
322 fn main() {
323     let _ = 90 +┃ 2;
324 }
325
326 // AFTER
327 fn main() {
328     let _ = 2 + 90;
329 }
330 ```
331
332 ## `flip_comma`
333
334 Flips two comma-separated items.
335
336 ```rust
337 // BEFORE
338 fn main() {
339     ((1, 2),┃ (3, 4));
340 }
341
342 // AFTER
343 fn main() {
344     ((3, 4), (1, 2));
345 }
346 ```
347
348 ## `flip_trait_bound`
349
350 Flips two trait bounds.
351
352 ```rust
353 // BEFORE
354 fn foo<T: Clone +┃ Copy>() { }
355
356 // AFTER
357 fn foo<T: Copy + Clone>() { }
358 ```
359
360 ## `inline_local_variable`
361
362 Inlines local variable.
363
364 ```rust
365 // BEFORE
366 fn main() {
367     let x┃ = 1 + 2;
368     x * 4;
369 }
370
371 // AFTER
372 fn main() {
373     (1 + 2) * 4;
374 }
375 ```
376
377 ## `introduce_variable`
378
379 Extracts subexpression into a variable.
380
381 ```rust
382 // BEFORE
383 fn main() {
384     ┃(1 + 2)┃ * 4;
385 }
386
387 // AFTER
388 fn main() {
389     let var_name = (1 + 2);
390     var_name * 4;
391 }
392 ```
393
394 ## `invert_if`
395
396 Apply invert_if
397 This transforms if expressions of the form `if !x {A} else {B}` into `if x {B} else {A}`
398 This also works with `!=`. This assist can only be applied with the cursor
399 on `if`.
400
401 ```rust
402 // BEFORE
403 fn main() {
404     if┃ !y { A } else { B }
405 }
406
407 // AFTER
408 fn main() {
409     if y { B } else { A }
410 }
411 ```
412
413 ## `make_raw_string`
414
415 Adds `r#` to a plain string literal.
416
417 ```rust
418 // BEFORE
419 fn main() {
420     "Hello,┃ World!";
421 }
422
423 // AFTER
424 fn main() {
425     r#"Hello, World!"#;
426 }
427 ```
428
429 ## `make_usual_string`
430
431 Turns a raw string into a plain string.
432
433 ```rust
434 // BEFORE
435 fn main() {
436     r#"Hello,┃ "World!""#;
437 }
438
439 // AFTER
440 fn main() {
441     "Hello, \"World!\"";
442 }
443 ```
444
445 ## `merge_imports`
446
447 Merges two imports with a common prefix.
448
449 ```rust
450 // BEFORE
451 use std::┃fmt::Formatter;
452 use std::io;
453
454 // AFTER
455 use std::{fmt::Formatter, io};
456 ```
457
458 ## `merge_match_arms`
459
460 Merges identical match arms.
461
462 ```rust
463 // BEFORE
464 enum Action { Move { distance: u32 }, Stop }
465
466 fn handle(action: Action) {
467     match action {
468         ┃Action::Move(..) => foo(),
469         Action::Stop => foo(),
470     }
471 }
472
473 // AFTER
474 enum Action { Move { distance: u32 }, Stop }
475
476 fn handle(action: Action) {
477     match action {
478         Action::Move(..) | Action::Stop => foo(),
479     }
480 }
481 ```
482
483 ## `move_arm_cond_to_match_guard`
484
485 Moves if expression from match arm body into a guard.
486
487 ```rust
488 // BEFORE
489 enum Action { Move { distance: u32 }, Stop }
490
491 fn handle(action: Action) {
492     match action {
493         Action::Move { distance } => ┃if distance > 10 { foo() },
494         _ => (),
495     }
496 }
497
498 // AFTER
499 enum Action { Move { distance: u32 }, Stop }
500
501 fn handle(action: Action) {
502     match action {
503         Action::Move { distance } if distance > 10 => foo(),
504         _ => (),
505     }
506 }
507 ```
508
509 ## `move_bounds_to_where_clause`
510
511 Moves inline type bounds to a where clause.
512
513 ```rust
514 // BEFORE
515 fn apply<T, U, ┃F: FnOnce(T) -> U>(f: F, x: T) -> U {
516     f(x)
517 }
518
519 // AFTER
520 fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
521     f(x)
522 }
523 ```
524
525 ## `move_guard_to_arm_body`
526
527 Moves match guard into match arm body.
528
529 ```rust
530 // BEFORE
531 enum Action { Move { distance: u32 }, Stop }
532
533 fn handle(action: Action) {
534     match action {
535         Action::Move { distance } ┃if distance > 10 => foo(),
536         _ => (),
537     }
538 }
539
540 // AFTER
541 enum Action { Move { distance: u32 }, Stop }
542
543 fn handle(action: Action) {
544     match action {
545         Action::Move { distance } => if distance > 10 { foo() },
546         _ => (),
547     }
548 }
549 ```
550
551 ## `remove_dbg`
552
553 Removes `dbg!()` macro call.
554
555 ```rust
556 // BEFORE
557 fn main() {
558     ┃dbg!(92);
559 }
560
561 // AFTER
562 fn main() {
563     92;
564 }
565 ```
566
567 ## `remove_hash`
568
569 Removes a hash from a raw string literal.
570
571 ```rust
572 // BEFORE
573 fn main() {
574     r#"Hello,┃ World!"#;
575 }
576
577 // AFTER
578 fn main() {
579     r"Hello, World!";
580 }
581 ```
582
583 ## `remove_mut`
584
585 Removes the `mut` keyword.
586
587 ```rust
588 // BEFORE
589 impl Walrus {
590     fn feed(&mut┃ self, amount: u32) {}
591 }
592
593 // AFTER
594 impl Walrus {
595     fn feed(&self, amount: u32) {}
596 }
597 ```
598
599 ## `reorder_fields`
600
601 Reorder the fields of record literals and record patterns in the same order as in
602 the definition.
603
604 ```rust
605 // BEFORE
606 struct Foo {foo: i32, bar: i32};
607 const test: Foo = ┃Foo {bar: 0, foo: 1}
608
609 // AFTER
610 struct Foo {foo: i32, bar: i32};
611 const test: Foo = Foo {foo: 1, bar: 0}
612 ```
613
614 ## `replace_if_let_with_match`
615
616 Replaces `if let` with an else branch with a `match` expression.
617
618 ```rust
619 // BEFORE
620 enum Action { Move { distance: u32 }, Stop }
621
622 fn handle(action: Action) {
623     ┃if let Action::Move { distance } = action {
624         foo(distance)
625     } else {
626         bar()
627     }
628 }
629
630 // AFTER
631 enum Action { Move { distance: u32 }, Stop }
632
633 fn handle(action: Action) {
634     match action {
635         Action::Move { distance } => foo(distance),
636         _ => bar(),
637     }
638 }
639 ```
640
641 ## `replace_let_with_if_let`
642
643 Replaces `let` with an `if-let`.
644
645 ```rust
646 // BEFORE
647
648 fn main(action: Action) {
649     ┃let x = compute();
650 }
651
652 fn compute() -> Option<i32> { None }
653
654 // AFTER
655
656 fn main(action: Action) {
657     if let Some(x) = compute() {
658     }
659 }
660
661 fn compute() -> Option<i32> { None }
662 ```
663
664 ## `replace_qualified_name_with_use`
665
666 Adds a use statement for a given fully-qualified name.
667
668 ```rust
669 // BEFORE
670 fn process(map: std::collections::┃HashMap<String, String>) {}
671
672 // AFTER
673 use std::collections::HashMap;
674
675 fn process(map: HashMap<String, String>) {}
676 ```
677
678 ## `replace_unwrap_with_match`
679
680 Replaces `unwrap` a `match` expression. Works for Result and Option.
681
682 ```rust
683 // BEFORE
684 enum Result<T, E> { Ok(T), Err(E) }
685 fn main() {
686     let x: Result<i32, i32> = Result::Ok(92);
687     let y = x.┃unwrap();
688 }
689
690 // AFTER
691 enum Result<T, E> { Ok(T), Err(E) }
692 fn main() {
693     let x: Result<i32, i32> = Result::Ok(92);
694     let y = match x {
695         Ok(a) => a,
696         _ => unreachable!(),
697     };
698 }
699 ```
700
701 ## `split_import`
702
703 Wraps the tail of import into braces.
704
705 ```rust
706 // BEFORE
707 use std::┃collections::HashMap;
708
709 // AFTER
710 use std::{collections::HashMap};
711 ```
712
713 ## `unwrap_block`
714
715 This assist removes if...else, for, while and loop control statements to just keep the body.
716
717 ```rust
718 // BEFORE
719 fn foo() {
720     if true {┃
721         println!("foo");
722     }
723 }
724
725 // AFTER
726 fn foo() {
727     println!("foo");
728 }
729 ```