rust/docs/user/generated_assists.adoc

1016 lines
18 KiB
Plaintext
Raw Normal View History

[discrete]
=== `add_custom_impl`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/add_custom_impl.rs#L14[add_custom_impl.rs]
Adds impl block for derived trait.
.Before
```rust
#[derive(Deb┃ug, Display)]
struct S;
```
.After
```rust
#[derive(Display)]
struct S;
impl Debug for S {
2020-05-17 12:21:24 +00:00
$0
}
```
[discrete]
=== `add_derive`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/add_derive.rs#L9[add_derive.rs]
2019-10-25 20:38:15 +00:00
Adds a new `#[derive()]` clause to a struct or enum.
.Before
2019-10-25 20:38:15 +00:00
```rust
struct Point {
x: u32,
2019-10-26 18:17:39 +00:00
y: u32,┃
2019-10-25 20:38:15 +00:00
}
```
2019-10-25 20:38:15 +00:00
.After
```rust
2020-05-17 12:21:24 +00:00
#[derive($0)]
2019-10-25 20:38:15 +00:00
struct Point {
x: u32,
y: u32,
}
```
[discrete]
=== `add_explicit_type`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/add_explicit_type.rs#L9[add_explicit_type.rs]
2019-10-25 20:38:15 +00:00
2019-10-26 16:08:13 +00:00
Specify type for a let binding.
2019-10-25 20:38:15 +00:00
.Before
2019-10-25 20:38:15 +00:00
```rust
fn main() {
2019-10-26 18:17:39 +00:00
let x┃ = 92;
2019-10-25 20:38:15 +00:00
}
```
2019-10-25 20:38:15 +00:00
.After
```rust
2019-10-25 20:38:15 +00:00
fn main() {
let x: i32 = 92;
}
```
[discrete]
=== `add_from_impl_for_enum`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs#L7[add_from_impl_for_enum.rs]
Adds a From impl for an enum variant with one tuple field.
.Before
```rust
enum A { ┃One(u32) }
```
.After
```rust
enum A { One(u32) }
impl From<u32> for A {
fn from(v: u32) -> Self {
A::One(v)
}
}
```
[discrete]
=== `add_function`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/add_function.rs#L19[add_function.rs]
2020-03-26 19:59:35 +00:00
Adds a stub function with a signature matching the function under the cursor.
.Before
2020-03-26 19:59:35 +00:00
```rust
struct Baz;
fn baz() -> Baz { Baz }
fn foo() {
bar┃("", baz());
2020-03-26 19:59:35 +00:00
}
```
2020-03-26 19:59:35 +00:00
.After
```rust
2020-03-26 19:59:35 +00:00
struct Baz;
fn baz() -> Baz { Baz }
fn foo() {
bar("", baz());
2020-03-26 19:59:35 +00:00
}
fn bar(arg: &str, baz: Baz) {
2020-05-19 20:25:07 +00:00
${0:todo!()}
2020-03-26 19:59:35 +00:00
}
```
[discrete]
=== `add_hash`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/raw_string.rs#L65[raw_string.rs]
2019-10-27 09:22:53 +00:00
Adds a hash to a raw string literal.
.Before
2019-10-27 09:22:53 +00:00
```rust
fn main() {
r#"Hello,┃ World!"#;
}
```
2019-10-27 09:22:53 +00:00
.After
```rust
2019-10-27 09:22:53 +00:00
fn main() {
r##"Hello, World!"##;
}
```
[discrete]
=== `add_impl`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/add_impl.rs#L6[add_impl.rs]
2019-10-25 20:38:15 +00:00
2019-10-26 16:08:13 +00:00
Adds a new inherent impl for a type.
2019-10-25 20:38:15 +00:00
.Before
2019-10-25 20:38:15 +00:00
```rust
struct Ctx<T: Clone> {
2020-05-17 12:21:24 +00:00
data: T,┃
2019-10-25 20:38:15 +00:00
}
```
2019-10-25 20:38:15 +00:00
.After
```rust
2019-10-25 20:38:15 +00:00
struct Ctx<T: Clone> {
2020-05-17 12:21:24 +00:00
data: T,
2019-10-25 20:38:15 +00:00
}
impl<T: Clone> Ctx<T> {
2020-05-17 12:21:24 +00:00
$0
2019-10-25 20:38:15 +00:00
}
```
[discrete]
=== `add_impl_default_members`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/add_missing_impl_members.rs#L64[add_missing_impl_members.rs]
2019-10-25 20:38:15 +00:00
2019-10-26 16:08:13 +00:00
Adds scaffold for overriding default impl members.
2019-10-25 20:38:15 +00:00
.Before
2019-10-25 20:38:15 +00:00
```rust
trait Trait {
2019-10-25 20:38:15 +00:00
Type X;
fn foo(&self);
fn bar(&self) {}
}
impl Trait for () {
2019-10-25 20:38:15 +00:00
Type X = ();
2019-10-26 18:17:39 +00:00
fn foo(&self) {}┃
2019-10-25 20:38:15 +00:00
}
```
2019-10-25 20:38:15 +00:00
.After
```rust
trait Trait {
2019-10-25 20:38:15 +00:00
Type X;
fn foo(&self);
fn bar(&self) {}
}
impl Trait for () {
2019-10-25 20:38:15 +00:00
Type X = ();
fn foo(&self) {}
2020-05-19 23:53:21 +00:00
$0fn bar(&self) {}
2019-10-25 20:38:15 +00:00
}
```
[discrete]
=== `add_impl_missing_members`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/add_missing_impl_members.rs#L24[add_missing_impl_members.rs]
2019-10-25 20:38:15 +00:00
2019-10-26 16:08:13 +00:00
Adds scaffold for required impl members.
2019-10-25 20:38:15 +00:00
.Before
2019-10-25 20:38:15 +00:00
```rust
trait Trait<T> {
2019-10-25 20:38:15 +00:00
Type X;
fn foo(&self) -> T;
2019-10-25 20:38:15 +00:00
fn bar(&self) {}
}
impl Trait<u32> for () {┃
2019-10-25 20:38:15 +00:00
}
```
2019-10-25 20:38:15 +00:00
.After
```rust
trait Trait<T> {
2019-10-25 20:38:15 +00:00
Type X;
fn foo(&self) -> T;
2019-10-25 20:38:15 +00:00
fn bar(&self) {}
}
impl Trait<u32> for () {
fn foo(&self) -> u32 {
${0:todo!()}
}
2019-10-25 20:38:15 +00:00
}
```
[discrete]
=== `add_new`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/add_new.rs#L12[add_new.rs]
Adds a new inherent impl for a type.
.Before
```rust
struct Ctx<T: Clone> {
data: T,┃
}
```
.After
```rust
struct Ctx<T: Clone> {
data: T,
}
impl<T: Clone> Ctx<T> {
2020-05-20 08:17:46 +00:00
fn $0new(data: T) -> Self { Self { data } }
}
```
[discrete]
=== `add_turbo_fish`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/add_turbo_fish.rs#L10[add_turbo_fish.rs]
2020-05-19 22:07:00 +00:00
Adds `::<_>` to a call of a generic method or function.
.Before
2020-05-19 22:07:00 +00:00
```rust
fn make<T>() -> T { todo!() }
fn main() {
let x = make┃();
}
```
2020-05-19 22:07:00 +00:00
.After
```rust
2020-05-19 22:07:00 +00:00
fn make<T>() -> T { todo!() }
fn main() {
let x = make::<${0:_}>();
}
```
[discrete]
=== `apply_demorgan`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/apply_demorgan.rs#L5[apply_demorgan.rs]
2019-10-25 20:38:15 +00:00
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`.
.Before
2019-10-25 20:38:15 +00:00
```rust
fn main() {
2019-10-26 18:17:39 +00:00
if x != 4 ||┃ !y {}
2019-10-25 20:38:15 +00:00
}
```
2019-10-25 20:38:15 +00:00
.After
```rust
2019-10-25 20:38:15 +00:00
fn main() {
if !(x == 4 && y) {}
}
```
[discrete]
=== `auto_import`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/auto_import.rs#L18[auto_import.rs]
If the name is unresolved, provides all possible imports for it.
.Before
```rust
fn main() {
let map = HashMap┃::new();
}
```
.After
```rust
use std::collections::HashMap;
fn main() {
2020-02-06 17:10:25 +00:00
let map = HashMap::new();
}
```
[discrete]
=== `change_return_type_to_result`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/change_return_type_to_result.rs#L8[change_return_type_to_result.rs]
Change the function's return type to Result.
.Before
```rust
fn foo() -> i32┃ { 42i32 }
```
.After
```rust
fn foo() -> Result<i32, ${0:_}> { Ok(42i32) }
```
[discrete]
=== `change_visibility`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/change_visibility.rs#L14[change_visibility.rs]
2019-10-26 14:37:04 +00:00
Adds or changes existing visibility specifier.
.Before
2019-10-26 14:37:04 +00:00
```rust
2019-10-26 18:17:39 +00:00
┃fn frobnicate() {}
```
2019-10-26 14:37:04 +00:00
.After
```rust
2019-10-26 14:37:04 +00:00
pub(crate) fn frobnicate() {}
```
[discrete]
=== `convert_to_guarded_return`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/early_return.rs#L21[early_return.rs]
2019-10-25 11:16:46 +00:00
Replace a large conditional with a guarded return.
.Before
2019-10-25 11:16:46 +00:00
```rust
fn main() {
2019-10-26 18:17:39 +00:00
┃if cond {
2019-10-25 11:16:46 +00:00
foo();
bar();
}
}
```
2019-10-25 11:16:46 +00:00
.After
```rust
2019-10-25 11:16:46 +00:00
fn main() {
if !cond {
return;
}
foo();
bar();
}
```
2019-10-26 14:37:04 +00:00
[discrete]
=== `fill_match_arms`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/fill_match_arms.rs#L14[fill_match_arms.rs]
2019-10-26 14:37:04 +00:00
Adds missing clauses to a `match` expression.
.Before
2019-10-26 14:37:04 +00:00
```rust
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
2019-10-26 18:17:39 +00:00
2019-10-26 14:37:04 +00:00
}
}
```
2019-10-26 14:37:04 +00:00
.After
```rust
2019-10-26 14:37:04 +00:00
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
2020-05-20 12:00:37 +00:00
$0Action::Move { distance } => {}
2020-03-24 11:45:07 +00:00
Action::Stop => {}
2019-10-26 14:37:04 +00:00
}
}
```
2019-10-26 16:08:13 +00:00
[discrete]
=== `fix_visibility`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/fix_visibility.rs#L13[fix_visibility.rs]
Makes inaccessible item public.
.Before
```rust
mod m {
fn frobnicate() {}
}
fn main() {
m::frobnicate┃() {}
}
```
.After
```rust
mod m {
2020-05-20 12:13:17 +00:00
$0pub(crate) fn frobnicate() {}
}
fn main() {
m::frobnicate() {}
}
```
[discrete]
=== `flip_binexpr`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/flip_binexpr.rs#L5[flip_binexpr.rs]
2019-10-26 16:08:13 +00:00
Flips operands of a binary expression.
.Before
2019-10-26 16:08:13 +00:00
```rust
fn main() {
2019-10-26 18:17:39 +00:00
let _ = 90 +┃ 2;
2019-10-26 16:08:13 +00:00
}
```
2019-10-26 16:08:13 +00:00
.After
```rust
2019-10-26 16:08:13 +00:00
fn main() {
let _ = 2 + 90;
}
```
[discrete]
=== `flip_comma`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/flip_comma.rs#L5[flip_comma.rs]
2019-10-26 16:08:13 +00:00
Flips two comma-separated items.
.Before
2019-10-26 16:08:13 +00:00
```rust
fn main() {
2019-10-26 18:17:39 +00:00
((1, 2),┃ (3, 4));
2019-10-26 16:08:13 +00:00
}
```
2019-10-26 16:08:13 +00:00
.After
```rust
2019-10-26 16:08:13 +00:00
fn main() {
((3, 4), (1, 2));
}
```
[discrete]
=== `flip_trait_bound`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/flip_trait_bound.rs#L9[flip_trait_bound.rs]
Flips two trait bounds.
.Before
```rust
fn foo<T: Clone +┃ Copy>() { }
```
.After
```rust
fn foo<T: Copy + Clone>() { }
```
[discrete]
=== `inline_local_variable`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/inline_local_variable.rs#L13[inline_local_variable.rs]
2019-10-26 16:08:13 +00:00
Inlines local variable.
.Before
2019-10-26 16:08:13 +00:00
```rust
fn main() {
2019-10-26 18:17:39 +00:00
let x┃ = 1 + 2;
2019-10-26 16:08:13 +00:00
x * 4;
}
```
2019-10-26 16:08:13 +00:00
.After
```rust
2019-10-26 16:08:13 +00:00
fn main() {
(1 + 2) * 4;
}
```
2019-10-26 16:58:18 +00:00
2020-06-01 13:36:51 +00:00
[discrete]
=== `introduce_named_lifetime`
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/introduce_named_lifetime.rs#L12[introduce_named_lifetime.rs]
Change an anonymous lifetime to a named lifetime.
.Before
```rust
impl Cursor<'_┃> {
fn node(self) -> &SyntaxNode {
match self {
Cursor::Replace(node) | Cursor::Before(node) => node,
}
}
}
```
.After
```rust
impl<'a> Cursor<'a> {
fn node(self) -> &SyntaxNode {
match self {
Cursor::Replace(node) | Cursor::Before(node) => node,
}
}
}
```
[discrete]
=== `introduce_variable`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/introduce_variable.rs#L14[introduce_variable.rs]
2019-10-26 16:58:18 +00:00
Extracts subexpression into a variable.
.Before
2019-10-26 16:58:18 +00:00
```rust
fn main() {
2019-10-26 18:17:39 +00:00
┃(1 + 2)┃ * 4;
2019-10-26 16:58:18 +00:00
}
```
2019-10-26 16:58:18 +00:00
.After
```rust
2019-10-26 16:58:18 +00:00
fn main() {
2020-05-20 21:07:17 +00:00
let $0var_name = (1 + 2);
2019-10-26 16:58:18 +00:00
var_name * 4;
}
```
2019-10-27 08:26:46 +00:00
[discrete]
=== `invert_if`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/invert_if.rs#L12[invert_if.rs]
2019-11-21 18:51:40 +00:00
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`.
.Before
2019-11-21 18:51:40 +00:00
```rust
fn main() {
2019-11-24 05:14:57 +00:00
if┃ !y { A } else { B }
2019-11-21 18:51:40 +00:00
}
```
2019-11-21 18:51:40 +00:00
.After
```rust
2019-11-21 18:51:40 +00:00
fn main() {
2019-11-24 05:14:57 +00:00
if y { B } else { A }
2019-11-21 18:51:40 +00:00
}
```
[discrete]
=== `make_raw_string`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/raw_string.rs#L10[raw_string.rs]
2019-10-27 09:22:53 +00:00
Adds `r#` to a plain string literal.
.Before
2019-10-27 09:22:53 +00:00
```rust
fn main() {
"Hello,┃ World!";
}
```
2019-10-27 09:22:53 +00:00
.After
```rust
2019-10-27 09:22:53 +00:00
fn main() {
r#"Hello, World!"#;
}
```
[discrete]
=== `make_usual_string`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/raw_string.rs#L39[raw_string.rs]
2019-10-27 09:22:53 +00:00
Turns a raw string into a plain string.
.Before
2019-10-27 09:22:53 +00:00
```rust
fn main() {
r#"Hello,┃ "World!""#;
}
```
2019-10-27 09:22:53 +00:00
.After
```rust
2019-10-27 09:22:53 +00:00
fn main() {
"Hello, \"World!\"";
}
```
[discrete]
=== `merge_imports`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/merge_imports.rs#L14[merge_imports.rs]
Merges two imports with a common prefix.
.Before
```rust
use std::┃fmt::Formatter;
use std::io;
```
.After
```rust
use std::{fmt::Formatter, io};
```
[discrete]
=== `merge_match_arms`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/merge_match_arms.rs#L11[merge_match_arms.rs]
2019-10-27 08:26:46 +00:00
Merges identical match arms.
.Before
2019-10-27 08:26:46 +00:00
```rust
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
┃Action::Move(..) => foo(),
Action::Stop => foo(),
}
}
```
2019-10-27 08:26:46 +00:00
.After
```rust
2019-10-27 08:26:46 +00:00
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
Action::Move(..) | Action::Stop => foo(),
}
}
```
[discrete]
=== `move_arm_cond_to_match_guard`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/move_guard.rs#L56[move_guard.rs]
2019-10-27 08:26:46 +00:00
Moves if expression from match arm body into a guard.
.Before
2019-10-27 08:26:46 +00:00
```rust
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
Action::Move { distance } => ┃if distance > 10 { foo() },
_ => (),
}
}
```
2019-10-27 08:26:46 +00:00
.After
```rust
2019-10-27 08:26:46 +00:00
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
Action::Move { distance } if distance > 10 => foo(),
_ => (),
}
}
```
[discrete]
=== `move_bounds_to_where_clause`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/move_bounds.rs#L10[move_bounds.rs]
2019-10-27 08:26:46 +00:00
Moves inline type bounds to a where clause.
.Before
2019-10-27 08:26:46 +00:00
```rust
fn apply<T, U, ┃F: FnOnce(T) -> U>(f: F, x: T) -> U {
f(x)
}
```
2019-10-27 08:26:46 +00:00
.After
```rust
2019-10-27 08:26:46 +00:00
fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
f(x)
}
```
[discrete]
=== `move_guard_to_arm_body`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/move_guard.rs#L8[move_guard.rs]
2019-10-27 08:26:46 +00:00
Moves match guard into match arm body.
.Before
2019-10-27 08:26:46 +00:00
```rust
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
Action::Move { distance } ┃if distance > 10 => foo(),
_ => (),
}
}
```
2019-10-27 08:26:46 +00:00
.After
```rust
2019-10-27 08:26:46 +00:00
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
Action::Move { distance } => if distance > 10 { foo() },
_ => (),
}
}
```
2019-10-27 09:22:53 +00:00
[discrete]
=== `remove_dbg`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/remove_dbg.rs#L8[remove_dbg.rs]
2019-10-27 09:22:53 +00:00
Removes `dbg!()` macro call.
.Before
2019-10-27 09:22:53 +00:00
```rust
fn main() {
┃dbg!(92);
}
```
2019-10-27 09:22:53 +00:00
.After
```rust
2019-10-27 09:22:53 +00:00
fn main() {
92;
}
```
[discrete]
=== `remove_hash`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/raw_string.rs#L89[raw_string.rs]
2019-10-27 09:22:53 +00:00
Removes a hash from a raw string literal.
.Before
2019-10-27 09:22:53 +00:00
```rust
fn main() {
r#"Hello,┃ World!"#;
}
```
2019-10-27 09:22:53 +00:00
.After
```rust
2019-10-27 09:22:53 +00:00
fn main() {
r"Hello, World!";
}
```
[discrete]
=== `remove_mut`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/remove_mut.rs#L5[remove_mut.rs]
2020-02-19 11:44:20 +00:00
Removes the `mut` keyword.
.Before
2020-02-19 11:44:20 +00:00
```rust
impl Walrus {
fn feed(&mut┃ self, amount: u32) {}
}
```
2020-02-19 11:44:20 +00:00
.After
```rust
2020-02-19 11:44:20 +00:00
impl Walrus {
fn feed(&self, amount: u32) {}
}
```
[discrete]
=== `reorder_fields`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/reorder_fields.rs#L10[reorder_fields.rs]
2020-04-11 18:32:58 +00:00
Reorder the fields of record literals and record patterns in the same order as in
the definition.
.Before
2020-04-11 18:32:58 +00:00
```rust
struct Foo {foo: i32, bar: i32};
const test: Foo = ┃Foo {bar: 0, foo: 1}
```
2020-04-11 18:32:58 +00:00
.After
```rust
2020-04-11 18:32:58 +00:00
struct Foo {foo: i32, bar: i32};
const test: Foo = Foo {foo: 1, bar: 0}
```
[discrete]
=== `replace_if_let_with_match`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/replace_if_let_with_match.rs#L13[replace_if_let_with_match.rs]
2019-10-27 09:22:53 +00:00
Replaces `if let` with an else branch with a `match` expression.
.Before
2019-10-27 09:22:53 +00:00
```rust
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
┃if let Action::Move { distance } = action {
foo(distance)
} else {
bar()
}
}
```
2019-10-27 09:22:53 +00:00
.After
```rust
2019-10-27 09:22:53 +00:00
enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
Action::Move { distance } => foo(distance),
_ => bar(),
}
}
```
[discrete]
=== `replace_let_with_if_let`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/replace_let_with_if_let.rs#L14[replace_let_with_if_let.rs]
2020-03-27 11:12:17 +00:00
2020-03-27 12:10:40 +00:00
Replaces `let` with an `if-let`.
2020-03-27 11:12:17 +00:00
.Before
2020-03-27 11:12:17 +00:00
```rust
fn main(action: Action) {
┃let x = compute();
}
fn compute() -> Option<i32> { None }
```
2020-03-27 11:12:17 +00:00
.After
```rust
2020-03-27 11:12:17 +00:00
fn main(action: Action) {
if let Some(x) = compute() {
}
}
fn compute() -> Option<i32> { None }
```
[discrete]
=== `replace_qualified_name_with_use`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs#L6[replace_qualified_name_with_use.rs]
2020-02-07 21:35:34 +00:00
Adds a use statement for a given fully-qualified name.
.Before
2020-02-07 21:35:34 +00:00
```rust
fn process(map: std::collections::┃HashMap<String, String>) {}
```
2020-02-07 21:35:34 +00:00
.After
```rust
2020-02-07 21:35:34 +00:00
use std::collections::HashMap;
fn process(map: HashMap<String, String>) {}
```
[discrete]
=== `replace_unwrap_with_match`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs#L17[replace_unwrap_with_match.rs]
2020-03-26 09:16:10 +00:00
Replaces `unwrap` a `match` expression. Works for Result and Option.
.Before
2020-03-26 09:16:10 +00:00
```rust
enum Result<T, E> { Ok(T), Err(E) }
fn main() {
let x: Result<i32, i32> = Result::Ok(92);
let y = x.┃unwrap();
}
```
2020-03-26 09:16:10 +00:00
.After
```rust
2020-03-26 09:16:10 +00:00
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 22:01:08 +00:00
$0_ => unreachable!(),
2020-03-26 09:16:10 +00:00
};
}
```
[discrete]
=== `split_import`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/split_import.rs#L7[split_import.rs]
2019-10-27 09:22:53 +00:00
Wraps the tail of import into braces.
.Before
2019-10-27 09:22:53 +00:00
```rust
use std::┃collections::HashMap;
```
2019-10-27 09:22:53 +00:00
.After
```rust
2019-10-27 09:22:53 +00:00
use std::{collections::HashMap};
```
[discrete]
=== `unwrap_block`
2020-05-31 13:36:20 +00:00
**Source:** https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_assists/src/handlers/unwrap_block.rs#L9[unwrap_block.rs]
This assist removes if...else, for, while and loop control statements to just keep the body.
.Before
```rust
fn foo() {
if true {┃
println!("foo");
}
}
```
.After
```rust
fn foo() {
println!("foo");
}
```