diff options
Diffstat (limited to 'crates/ra_assists/src/assists')
-rw-r--r-- | crates/ra_assists/src/assists/early_return.rs | 43 |
1 files changed, 20 insertions, 23 deletions
diff --git a/crates/ra_assists/src/assists/early_return.rs b/crates/ra_assists/src/assists/early_return.rs index f7d7e12e7..b3d025340 100644 --- a/crates/ra_assists/src/assists/early_return.rs +++ b/crates/ra_assists/src/assists/early_return.rs | |||
@@ -1,26 +1,3 @@ | |||
1 | //! Assist: `convert_to_guarded_return` | ||
2 | //! | ||
3 | //! Replace a large conditional with a guarded return. | ||
4 | //! | ||
5 | //! ```text | ||
6 | //! fn <|>main() { | ||
7 | //! if cond { | ||
8 | //! foo(); | ||
9 | //! bar(); | ||
10 | //! } | ||
11 | //! } | ||
12 | //! ``` | ||
13 | //! -> | ||
14 | //! ```text | ||
15 | //! fn main() { | ||
16 | //! if !cond { | ||
17 | //! return; | ||
18 | //! } | ||
19 | //! foo(); | ||
20 | //! bar(); | ||
21 | //! } | ||
22 | //! ``` | ||
23 | |||
24 | use std::ops::RangeInclusive; | 1 | use std::ops::RangeInclusive; |
25 | 2 | ||
26 | use hir::db::HirDatabase; | 3 | use hir::db::HirDatabase; |
@@ -36,6 +13,26 @@ use crate::{ | |||
36 | AssistId, | 13 | AssistId, |
37 | }; | 14 | }; |
38 | 15 | ||
16 | // Assist: convert_to_guarded_return | ||
17 | // Replace a large conditional with a guarded return. | ||
18 | // ``` | ||
19 | // fn main() { | ||
20 | // <|>if cond { | ||
21 | // foo(); | ||
22 | // bar(); | ||
23 | // } | ||
24 | // } | ||
25 | // ``` | ||
26 | // -> | ||
27 | // ``` | ||
28 | // fn main() { | ||
29 | // if !cond { | ||
30 | // return; | ||
31 | // } | ||
32 | // foo(); | ||
33 | // bar(); | ||
34 | // } | ||
35 | // ``` | ||
39 | pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 36 | pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
40 | let if_expr: ast::IfExpr = ctx.node_at_offset()?; | 37 | let if_expr: ast::IfExpr = ctx.node_at_offset()?; |
41 | let expr = if_expr.condition()?.expr()?; | 38 | let expr = if_expr.condition()?.expr()?; |