From a490ba06fa635ecb34b5ce0b7205621eefaee603 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 27 Oct 2019 11:26:46 +0300 Subject: document some more assists --- docs/user/assists.md | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++ docs/user/features.md | 84 ---------------------------------------------- 2 files changed, 93 insertions(+), 84 deletions(-) (limited to 'docs') diff --git a/docs/user/assists.md b/docs/user/assists.md index ee1cfa142..34a95696c 100644 --- a/docs/user/assists.md +++ b/docs/user/assists.md @@ -265,3 +265,96 @@ fn main() { var_name * 4; } ``` + +## `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 U>(f: F, x: T) -> U { + f(x) +} + +// AFTER +fn apply(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() }, + _ => (), + } +} +``` diff --git a/docs/user/features.md b/docs/user/features.md index 39dab710d..2e213e34c 100644 --- a/docs/user/features.md +++ b/docs/user/features.md @@ -154,45 +154,6 @@ fn main() { } ``` -- Flip `,` - -```rust -// before: -fn foo(x: usize,<|> dim: (usize, usize)) {} -// after: -fn foo(dim: (usize, usize), x: usize) {} -``` - -- Introduce variable: - -```rust -// before: -fn foo() { - foo(<|>1 + 1<|>); -} - -// after: -fn foo() { - let var_name = 1 + 1; - foo(var_name); -} -``` - -- Inline local variable: - -```rust -// before: -fn foo() { - let a<|> = 1 + 1; - let b = a * 10; -} - -// after: -fn foo() { - let b = (1 + 1) * 10; -} -``` - - Remove `dbg!` ```rust @@ -245,41 +206,6 @@ use crate:<|>:db::{RootDatabase, FileSymbol}; use crate::{<|>db::{RootDatabase, FileSymbol}}; ``` -- Flip binary expression - -```rust -// before: -fn foo() { - if 1 <<|> 2 { - println!("Who would have thought?"); - } -} -// after: -fn foo() { - if 2 ><|> 1 { - println!("Who would have thought?"); - } -} -``` - -- Move guard expression to match arm body -```rust -// before: -fn f() { - match x { - <|>y @ 4 | y @ 5 if y > 5 => true, - _ => false - } -} -// after: -fn f() { - match x { - y @ 4 | y @ 5 => if y > 5 { <|>true }, - _ => false - } -} -``` - - Move if condition to match arm guard ```rust // before: @@ -309,16 +235,6 @@ fn f() { } ``` -- Move type bounds to where clause - -```rust -// before: -fn foo T>() {} - -// after: -fn foo() where T: u32, F: FnOnce(T) -> T {} -``` - - Make raw string unescaped ```rust -- cgit v1.2.3