]> git.lizzy.rs Git - rust.git/blob - docs/user/assists.md
8da7578e2f2b9712a1f6e5b683d48900f7469750
[rust.git] / docs / user / assists.md
1 # Assists
2
3 Cursor position or selection is signified by `┃` character.
4
5
6 ## `add_derive`
7
8 Adds a new `#[derive()]` clause to a struct or enum.
9
10 ```rust
11 // BEFORE
12 struct Point {
13     x: u32,
14     y: u32,┃
15 }
16
17 // AFTER
18 #[derive()]
19 struct Point {
20     x: u32,
21     y: u32,
22 }
23 ```
24
25 ## `add_explicit_type`
26
27 Specify type for a let binding.
28
29 ```rust
30 // BEFORE
31 fn main() {
32     let x┃ = 92;
33 }
34
35 // AFTER
36 fn main() {
37     let x: i32 = 92;
38 }
39 ```
40
41 ## `add_hash`
42
43 Adds a hash to a raw string literal.
44
45 ```rust
46 // BEFORE
47 fn main() {
48     r#"Hello,┃ World!"#;
49 }
50
51 // AFTER
52 fn main() {
53     r##"Hello, World!"##;
54 }
55 ```
56
57 ## `add_impl`
58
59 Adds a new inherent impl for a type.
60
61 ```rust
62 // BEFORE
63 struct Ctx<T: Clone> {
64      data: T,┃
65 }
66
67 // AFTER
68 struct Ctx<T: Clone> {
69      data: T,
70 }
71
72 impl<T: Clone> Ctx<T> {
73
74 }
75 ```
76
77 ## `add_impl_default_members`
78
79 Adds scaffold for overriding default impl members.
80
81 ```rust
82 // BEFORE
83 trait T {
84     Type X;
85     fn foo(&self);
86     fn bar(&self) {}
87 }
88
89 impl T for () {
90     Type X = ();
91     fn foo(&self) {}┃
92
93 }
94
95 // AFTER
96 trait T {
97     Type X;
98     fn foo(&self);
99     fn bar(&self) {}
100 }
101
102 impl T for () {
103     Type X = ();
104     fn foo(&self) {}
105     fn bar(&self) {}
106
107 }
108 ```
109
110 ## `add_impl_missing_members`
111
112 Adds scaffold for required impl members.
113
114 ```rust
115 // BEFORE
116 trait T {
117     Type X;
118     fn foo(&self);
119     fn bar(&self) {}
120 }
121
122 impl T for () {┃
123
124 }
125
126 // AFTER
127 trait T {
128     Type X;
129     fn foo(&self);
130     fn bar(&self) {}
131 }
132
133 impl T for () {
134     fn foo(&self) { unimplemented!() }
135
136 }
137 ```
138
139 ## `add_import`
140
141 Adds a use statement for a given fully-qualified path.
142
143 ```rust
144 // BEFORE
145 fn process(map: std::collections::┃HashMap<String, String>) {}
146
147 // AFTER
148 use std::collections::HashMap;
149
150 fn process(map: HashMap<String, String>) {}
151 ```
152
153 ## `add_new`
154
155 Adds a new inherent impl for a type.
156
157 ```rust
158 // BEFORE
159 struct Ctx<T: Clone> {
160      data: T,┃
161 }
162
163 // AFTER
164 struct Ctx<T: Clone> {
165      data: T,
166 }
167
168 impl<T: Clone> Ctx<T> {
169     fn new(data: T) -> Self { Self { data } }
170 }
171
172 ```
173
174 ## `apply_demorgan`
175
176 Apply [De Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws).
177 This transforms expressions of the form `!l || !r` into `!(l && r)`.
178 This also works with `&&`. This assist can only be applied with the cursor
179 on either `||` or `&&`, with both operands being a negation of some kind.
180 This means something of the form `!x` or `x != y`.
181
182 ```rust
183 // BEFORE
184 fn main() {
185     if x != 4 ||┃ !y {}
186 }
187
188 // AFTER
189 fn main() {
190     if !(x == 4 && y) {}
191 }
192 ```
193
194 ## `change_visibility`
195
196 Adds or changes existing visibility specifier.
197
198 ```rust
199 // BEFORE
200 ┃fn frobnicate() {}
201
202 // AFTER
203 pub(crate) fn frobnicate() {}
204 ```
205
206 ## `convert_to_guarded_return`
207
208 Replace a large conditional with a guarded return.
209
210 ```rust
211 // BEFORE
212 fn main() {
213     ┃if cond {
214         foo();
215         bar();
216     }
217 }
218
219 // AFTER
220 fn main() {
221     if !cond {
222         return;
223     }
224     foo();
225     bar();
226 }
227 ```
228
229 ## `fill_match_arms`
230
231 Adds missing clauses to a `match` expression.
232
233 ```rust
234 // BEFORE
235 enum Action { Move { distance: u32 }, Stop }
236
237 fn handle(action: Action) {
238     match action {
239         ┃
240     }
241 }
242
243 // AFTER
244 enum Action { Move { distance: u32 }, Stop }
245
246 fn handle(action: Action) {
247     match action {
248         Action::Move { distance } => (),
249         Action::Stop => (),
250     }
251 }
252 ```
253
254 ## `flip_binexpr`
255
256 Flips operands of a binary expression.
257
258 ```rust
259 // BEFORE
260 fn main() {
261     let _ = 90 +┃ 2;
262 }
263
264 // AFTER
265 fn main() {
266     let _ = 2 + 90;
267 }
268 ```
269
270 ## `flip_comma`
271
272 Flips two comma-separated items.
273
274 ```rust
275 // BEFORE
276 fn main() {
277     ((1, 2),┃ (3, 4));
278 }
279
280 // AFTER
281 fn main() {
282     ((3, 4), (1, 2));
283 }
284 ```
285
286 ## `flip_trait_bound`
287
288 Flips two trait bounds.
289
290 ```rust
291 // BEFORE
292 fn foo<T: Clone +┃ Copy>() { }
293
294 // AFTER
295 fn foo<T: Copy + Clone>() { }
296 ```
297
298 ## `inline_local_variable`
299
300 Inlines local variable.
301
302 ```rust
303 // BEFORE
304 fn main() {
305     let x┃ = 1 + 2;
306     x * 4;
307 }
308
309 // AFTER
310 fn main() {
311     (1 + 2) * 4;
312 }
313 ```
314
315 ## `introduce_variable`
316
317 Extracts subexpression into a variable.
318
319 ```rust
320 // BEFORE
321 fn main() {
322     ┃(1 + 2)┃ * 4;
323 }
324
325 // AFTER
326 fn main() {
327     let var_name = (1 + 2);
328     var_name * 4;
329 }
330 ```
331
332 ## `make_raw_string`
333
334 Adds `r#` to a plain string literal.
335
336 ```rust
337 // BEFORE
338 fn main() {
339     "Hello,┃ World!";
340 }
341
342 // AFTER
343 fn main() {
344     r#"Hello, World!"#;
345 }
346 ```
347
348 ## `make_usual_string`
349
350 Turns a raw string into a plain string.
351
352 ```rust
353 // BEFORE
354 fn main() {
355     r#"Hello,┃ "World!""#;
356 }
357
358 // AFTER
359 fn main() {
360     "Hello, \"World!\"";
361 }
362 ```
363
364 ## `merge_match_arms`
365
366 Merges identical match arms.
367
368 ```rust
369 // BEFORE
370 enum Action { Move { distance: u32 }, Stop }
371
372 fn handle(action: Action) {
373     match action {
374         ┃Action::Move(..) => foo(),
375         Action::Stop => foo(),
376     }
377 }
378
379 // AFTER
380 enum Action { Move { distance: u32 }, Stop }
381
382 fn handle(action: Action) {
383     match action {
384         Action::Move(..) | Action::Stop => foo(),
385     }
386 }
387 ```
388
389 ## `move_arm_cond_to_match_guard`
390
391 Moves if expression from match arm body into a guard.
392
393 ```rust
394 // BEFORE
395 enum Action { Move { distance: u32 }, Stop }
396
397 fn handle(action: Action) {
398     match action {
399         Action::Move { distance } => ┃if distance > 10 { foo() },
400         _ => (),
401     }
402 }
403
404 // AFTER
405 enum Action { Move { distance: u32 }, Stop }
406
407 fn handle(action: Action) {
408     match action {
409         Action::Move { distance } if distance > 10 => foo(),
410         _ => (),
411     }
412 }
413 ```
414
415 ## `move_bounds_to_where_clause`
416
417 Moves inline type bounds to a where clause.
418
419 ```rust
420 // BEFORE
421 fn apply<T, U, ┃F: FnOnce(T) -> U>(f: F, x: T) -> U {
422     f(x)
423 }
424
425 // AFTER
426 fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
427     f(x)
428 }
429 ```
430
431 ## `move_guard_to_arm_body`
432
433 Moves match guard into match arm body.
434
435 ```rust
436 // BEFORE
437 enum Action { Move { distance: u32 }, Stop }
438
439 fn handle(action: Action) {
440     match action {
441         Action::Move { distance } ┃if distance > 10 => foo(),
442         _ => (),
443     }
444 }
445
446 // AFTER
447 enum Action { Move { distance: u32 }, Stop }
448
449 fn handle(action: Action) {
450     match action {
451         Action::Move { distance } => if distance > 10 { foo() },
452         _ => (),
453     }
454 }
455 ```
456
457 ## `remove_dbg`
458
459 Removes `dbg!()` macro call.
460
461 ```rust
462 // BEFORE
463 fn main() {
464     ┃dbg!(92);
465 }
466
467 // AFTER
468 fn main() {
469     92;
470 }
471 ```
472
473 ## `remove_hash`
474
475 Removes a hash from a raw string literal.
476
477 ```rust
478 // BEFORE
479 fn main() {
480     r#"Hello,┃ World!"#;
481 }
482
483 // AFTER
484 fn main() {
485     r"Hello, World!";
486 }
487 ```
488
489 ## `replace_if_let_with_match`
490
491 Replaces `if let` with an else branch with a `match` expression.
492
493 ```rust
494 // BEFORE
495 enum Action { Move { distance: u32 }, Stop }
496
497 fn handle(action: Action) {
498     ┃if let Action::Move { distance } = action {
499         foo(distance)
500     } else {
501         bar()
502     }
503 }
504
505 // AFTER
506 enum Action { Move { distance: u32 }, Stop }
507
508 fn handle(action: Action) {
509     match action {
510         Action::Move { distance } => foo(distance),
511         _ => bar(),
512     }
513 }
514 ```
515
516 ## `split_import`
517
518 Wraps the tail of import into braces.
519
520 ```rust
521 // BEFORE
522 use std::┃collections::HashMap;
523
524 // AFTER
525 use std::{collections::HashMap};
526 ```