rust/docs/user/assists.md

770 lines
10 KiB
Markdown
Raw Normal View History

2019-10-25 06:16:46 -05:00
# Assists
2019-10-26 13:17:39 -05:00
Cursor position or selection is signified by `┃` character.
## `add_custom_impl`
Adds impl block for derived trait.
```rust
// BEFORE
#[derive(Deb┃ug, Display)]
struct S;
// AFTER
#[derive(Display)]
struct S;
impl Debug for S {
2020-05-17 07:21:24 -05:00
$0
}
```
2019-10-25 15:38:15 -05:00
## `add_derive`
Adds a new `#[derive()]` clause to a struct or enum.
```rust
// BEFORE
struct Point {
x: u32,
2019-10-26 13:17:39 -05:00
y: u32,┃
2019-10-25 15:38:15 -05:00
}
// AFTER
2020-05-17 07:21:24 -05:00
#[derive($0)]
2019-10-25 15:38:15 -05:00
struct Point {
x: u32,
y: u32,
}
```
## `add_explicit_type`
2019-10-26 11:08:13 -05:00
Specify type for a let binding.
2019-10-25 15:38:15 -05:00
```rust
// BEFORE
fn main() {
2019-10-26 13:17:39 -05:00
let x┃ = 92;
2019-10-25 15:38:15 -05:00
}
// AFTER
fn main() {
let x: i32 = 92;
}
```
2020-03-26 14:59:35 -05:00
## `add_function`
Adds a stub function with a signature matching the function under the cursor.
```rust
// BEFORE
struct Baz;
fn baz() -> Baz { Baz }
fn foo() {
bar┃("", baz());
2020-03-26 14:59:35 -05:00
}
// AFTER
struct Baz;
fn baz() -> Baz { Baz }
fn foo() {
bar("", baz());
2020-03-26 14:59:35 -05:00
}
fn bar(arg: &str, baz: Baz) {
2020-05-19 15:25:07 -05:00
${0:todo!()}
2020-03-26 14:59:35 -05:00
}
```
2019-10-27 04:22:53 -05:00
## `add_hash`
Adds a hash to a raw string literal.
```rust
// BEFORE
fn main() {
r#"Hello,┃ World!"#;
}
// AFTER
fn main() {
r##"Hello, World!"##;
}
```
2019-10-25 15:38:15 -05:00
## `add_impl`
2019-10-26 11:08:13 -05:00
Adds a new inherent impl for a type.
2019-10-25 15:38:15 -05:00
```rust
// BEFORE
struct Ctx<T: Clone> {
2020-05-17 07:21:24 -05:00
data: T,┃
2019-10-25 15:38:15 -05:00
}
// AFTER
struct Ctx<T: Clone> {
2020-05-17 07:21:24 -05:00
data: T,
2019-10-25 15:38:15 -05:00
}
impl<T: Clone> Ctx<T> {
2020-05-17 07:21:24 -05:00
$0
2019-10-25 15:38:15 -05:00
}
```
## `add_impl_default_members`
2019-10-26 11:08:13 -05:00
Adds scaffold for overriding default impl members.
2019-10-25 15:38:15 -05:00
```rust
// BEFORE
trait Trait {
2019-10-25 15:38:15 -05:00
Type X;
fn foo(&self);
fn bar(&self) {}
}
impl Trait for () {
2019-10-25 15:38:15 -05:00
Type X = ();
2019-10-26 13:17:39 -05:00
fn foo(&self) {}┃
2019-10-25 15:38:15 -05:00
}
// AFTER
trait Trait {
2019-10-25 15:38:15 -05:00
Type X;
fn foo(&self);
fn bar(&self) {}
}
impl Trait for () {
2019-10-25 15:38:15 -05:00
Type X = ();
fn foo(&self) {}
2020-05-19 18:53:21 -05:00
$0fn bar(&self) {}
2019-10-25 15:38:15 -05:00
}
```
## `add_impl_missing_members`
2019-10-26 11:08:13 -05:00
Adds scaffold for required impl members.
2019-10-25 15:38:15 -05:00
```rust
// BEFORE
trait Trait<T> {
2019-10-25 15:38:15 -05:00
Type X;
fn foo(&self) -> T;
2019-10-25 15:38:15 -05:00
fn bar(&self) {}
}
impl Trait<u32> for () {┃
2019-10-25 15:38:15 -05:00
}
// AFTER
trait Trait<T> {
2019-10-25 15:38:15 -05:00
Type X;
fn foo(&self) -> T;
2019-10-25 15:38:15 -05:00
fn bar(&self) {}
}
impl Trait<u32> for () {
fn foo(&self) -> u32 {
${0:todo!()}
}
2019-10-25 15:38:15 -05:00
}
```
## `add_new`
Adds a new inherent impl for a type.
```rust
// BEFORE
struct Ctx<T: Clone> {
data: T,┃
}
// AFTER
struct Ctx<T: Clone> {
data: T,
}
impl<T: Clone> Ctx<T> {
2020-05-20 03:17:46 -05:00
fn $0new(data: T) -> Self { Self { data } }
}
```
2020-05-19 17:07:00 -05:00
## `add_turbo_fish`
Adds `::<_>` to a call of a generic method or function.
```rust
// BEFORE
fn make<T>() -> T { todo!() }
fn main() {
let x = make┃();
}
// AFTER
fn make<T>() -> T { todo!() }
fn main() {
let x = make::<${0:_}>();
}
```
2019-10-25 15:38:15 -05:00
## `apply_demorgan`
Apply [De Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws).
This transforms expressions of the form `!l || !r` into `!(l && r)`.
This also works with `&&`. This assist can only be applied with the cursor
on either `||` or `&&`, with both operands being a negation of some kind.
This means something of the form `!x` or `x != y`.
```rust
// BEFORE
fn main() {
2019-10-26 13:17:39 -05:00
if x != 4 ||┃ !y {}
2019-10-25 15:38:15 -05:00
}
// AFTER
fn main() {
if !(x == 4 && y) {}
}
```
## `auto_import`
If the name is unresolved, provides all possible imports for it.
```rust
// BEFORE
fn main() {
let map = HashMap┃::new();
}
// AFTER
use std::collections::HashMap;
fn main() {
2020-02-06 11:10:25 -06:00
let map = HashMap::new();
}
```
## `change_return_type_to_result`
Change the function's return type to Result.
```rust
// BEFORE
fn foo() -> i32┃ { 42i32 }
// AFTER
fn foo() -> Result<i32, ${0:_}> { Ok(42i32) }
```
2019-10-26 09:37:04 -05:00
## `change_visibility`
Adds or changes existing visibility specifier.
```rust
// BEFORE
2019-10-26 13:17:39 -05:00
┃fn frobnicate() {}
2019-10-26 09:37:04 -05:00
// AFTER
pub(crate) fn frobnicate() {}
```
2019-10-25 06:16:46 -05:00
## `convert_to_guarded_return`
Replace a large conditional with a guarded return.
```rust
// BEFORE
fn main() {
2019-10-26 13:17:39 -05:00
┃if cond {
2019-10-25 06:16:46 -05:00
foo();
bar();
}
}
// AFTER
fn main() {
if !cond {
return;
}
foo();
bar();
}
```
2019-10-26 09:37:04 -05:00
## `fill_match_arms`
Adds missing clauses to a `match` expression.
```rust
// BEFORE
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
2019-10-26 13:17:39 -05:00
2019-10-26 09:37:04 -05:00
}
}
// AFTER
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
2020-05-20 07:00:37 -05:00
$0Action::Move { distance } => {}
2020-03-24 06:45:07 -05:00
Action::Stop => {}
2019-10-26 09:37:04 -05:00
}
}
```
2019-10-26 11:08:13 -05:00
## `fix_visibility`
Makes inaccessible item public.
```rust
// BEFORE
mod m {
fn frobnicate() {}
}
fn main() {
m::frobnicate┃() {}
}
// AFTER
mod m {
2020-05-20 07:13:17 -05:00
$0pub(crate) fn frobnicate() {}
}
fn main() {
m::frobnicate() {}
}
```
2019-10-26 11:08:13 -05:00
## `flip_binexpr`
Flips operands of a binary expression.
```rust
// BEFORE
fn main() {
2019-10-26 13:17:39 -05:00
let _ = 90 +┃ 2;
2019-10-26 11:08:13 -05:00
}
// AFTER
fn main() {
let _ = 2 + 90;
}
```
## `flip_comma`
Flips two comma-separated items.
```rust
// BEFORE
fn main() {
2019-10-26 13:17:39 -05:00
((1, 2),┃ (3, 4));
2019-10-26 11:08:13 -05:00
}
// AFTER
fn main() {
((3, 4), (1, 2));
}
```
## `flip_trait_bound`
Flips two trait bounds.
```rust
// BEFORE
fn foo<T: Clone + Copy>() { }
// AFTER
fn foo<T: Copy + Clone>() { }
```
2019-10-26 11:08:13 -05:00
## `inline_local_variable`
Inlines local variable.
```rust
// BEFORE
fn main() {
2019-10-26 13:17:39 -05:00
let x┃ = 1 + 2;
2019-10-26 11:08:13 -05:00
x * 4;
}
// AFTER
fn main() {
(1 + 2) * 4;
}
```
2019-10-26 11:58:18 -05:00
## `introduce_variable`
Extracts subexpression into a variable.
```rust
// BEFORE
fn main() {
2019-10-26 13:17:39 -05:00
┃(1 + 2)┃ * 4;
2019-10-26 11:58:18 -05:00
}
// AFTER
fn main() {
2020-05-20 16:07:17 -05:00
let $0var_name = (1 + 2);
2019-10-26 11:58:18 -05:00
var_name * 4;
}
```
2019-10-27 03:26:46 -05:00
2019-11-21 12:51:40 -06:00
## `invert_if`
Apply invert_if
This transforms if expressions of the form `if !x {A} else {B}` into `if x {B} else {A}`
This also works with `!=`. This assist can only be applied with the cursor
on `if`.
```rust
// BEFORE
fn main() {
2019-11-23 23:14:57 -06:00
if┃ !y { A } else { B }
2019-11-21 12:51:40 -06:00
}
// AFTER
fn main() {
2019-11-23 23:14:57 -06:00
if y { B } else { A }
2019-11-21 12:51:40 -06:00
}
```
2019-10-27 04:22:53 -05:00
## `make_raw_string`
Adds `r#` to a plain string literal.
```rust
// BEFORE
fn main() {
"Hello,┃ World!";
}
// AFTER
fn main() {
r#"Hello, World!"#;
}
```
## `make_usual_string`
Turns a raw string into a plain string.
```rust
// BEFORE
fn main() {
r#"Hello,┃ "World!""#;
}
// AFTER
fn main() {
"Hello, \"World!\"";
}
```
## `merge_imports`
Merges two imports with a common prefix.
```rust
// BEFORE
use std::┃fmt::Formatter;
use std::io;
// AFTER
use std::{fmt::Formatter, io};
```
2019-10-27 03:26:46 -05:00
## `merge_match_arms`
Merges identical match arms.
```rust
// BEFORE
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
┃Action::Move(..) => foo(),
Action::Stop => foo(),
}
}
// AFTER
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
Action::Move(..) | Action::Stop => foo(),
}
}
```
## `move_arm_cond_to_match_guard`
Moves if expression from match arm body into a guard.
```rust
// BEFORE
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
Action::Move { distance } => ┃if distance > 10 { foo() },
_ => (),
}
}
// AFTER
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
Action::Move { distance } if distance > 10 => foo(),
_ => (),
}
}
```
## `move_bounds_to_where_clause`
Moves inline type bounds to a where clause.
```rust
// BEFORE
fn apply<T, U, F: FnOnce(T) -> U>(f: F, x: T) -> U {
f(x)
}
// AFTER
fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
f(x)
}
```
## `move_guard_to_arm_body`
Moves match guard into match arm body.
```rust
// BEFORE
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
Action::Move { distance } ┃if distance > 10 => foo(),
_ => (),
}
}
// AFTER
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
Action::Move { distance } => if distance > 10 { foo() },
_ => (),
}
}
```
2019-10-27 04:22:53 -05:00
## `remove_dbg`
Removes `dbg!()` macro call.
```rust
// BEFORE
fn main() {
┃dbg!(92);
}
// AFTER
fn main() {
92;
}
```
## `remove_hash`
Removes a hash from a raw string literal.
```rust
// BEFORE
fn main() {
r#"Hello,┃ World!"#;
}
// AFTER
fn main() {
r"Hello, World!";
}
```
2020-02-19 05:44:20 -06:00
## `remove_mut`
Removes the `mut` keyword.
```rust
// BEFORE
impl Walrus {
fn feed(&mut┃ self, amount: u32) {}
}
// AFTER
impl Walrus {
fn feed(&self, amount: u32) {}
}
```
2020-04-11 13:32:58 -05:00
## `reorder_fields`
Reorder the fields of record literals and record patterns in the same order as in
the definition.
```rust
// BEFORE
struct Foo {foo: i32, bar: i32};
const test: Foo = ┃Foo {bar: 0, foo: 1}
// AFTER
struct Foo {foo: i32, bar: i32};
const test: Foo = Foo {foo: 1, bar: 0}
```
2019-10-27 04:22:53 -05:00
## `replace_if_let_with_match`
Replaces `if let` with an else branch with a `match` expression.
```rust
// BEFORE
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
┃if let Action::Move { distance } = action {
foo(distance)
} else {
bar()
}
}
// AFTER
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
Action::Move { distance } => foo(distance),
_ => bar(),
}
}
```
2020-03-27 06:12:17 -05:00
## `replace_let_with_if_let`
2020-03-27 07:10:40 -05:00
Replaces `let` with an `if-let`.
2020-03-27 06:12:17 -05:00
```rust
// BEFORE
fn main(action: Action) {
┃let x = compute();
}
fn compute() -> Option<i32> { None }
// AFTER
fn main(action: Action) {
if let Some(x) = compute() {
}
}
fn compute() -> Option<i32> { None }
```
2020-02-07 15:35:34 -06:00
## `replace_qualified_name_with_use`
Adds a use statement for a given fully-qualified name.
```rust
// BEFORE
fn process(map: std::collections::┃HashMap<String, String>) {}
// AFTER
use std::collections::HashMap;
fn process(map: HashMap<String, String>) {}
```
2020-03-26 04:16:10 -05:00
## `replace_unwrap_with_match`
Replaces `unwrap` a `match` expression. Works for Result and Option.
```rust
// BEFORE
enum Result<T, E> { Ok(T), Err(E) }
fn main() {
let x: Result<i32, i32> = Result::Ok(92);
let y = x.┃unwrap();
}
// AFTER
enum Result<T, E> { Ok(T), Err(E) }
fn main() {
let x: Result<i32, i32> = Result::Ok(92);
let y = match x {
Ok(a) => a,
2020-05-20 17:01:08 -05:00
$0_ => unreachable!(),
2020-03-26 04:16:10 -05:00
};
}
```
2019-10-27 04:22:53 -05:00
## `split_import`
Wraps the tail of import into braces.
```rust
// BEFORE
use std::┃collections::HashMap;
// AFTER
use std::{collections::HashMap};
```
## `unwrap_block`
This assist removes if...else, for, while and loop control statements to just keep the body.
```rust
// BEFORE
fn foo() {
if true {┃
println!("foo");
}
}
// AFTER
fn foo() {
println!("foo");
}
```