diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-12-16 13:26:16 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-16 13:26:16 +0000 |
| commit | f79f3db7b71b192dd2e387d2d7face7e27b3b3bd (patch) | |
| tree | 4b5a50adc20566bfbed300a8d4d4656d5159da1e | |
| parent | 098b1f248682f87a69f6f00ff0683ca724521b5d (diff) | |
| parent | ee079561b1d65d40f67a9d6c3df3e8022b2e3d0a (diff) | |
| download | rust-f79f3db7b71b192dd2e387d2d7face7e27b3b3bd.tar.gz rust-f79f3db7b71b192dd2e387d2d7face7e27b3b3bd.zip | |
Merge #11030
11030: Add comma for "move if to guard" r=Veykril a=weirane
As I mentioned in #11017, there is a little issue in the implementation for if branch. This code
```rust
let y = match 92 {
x => {
if x == 0 {$0
false
}
}
_ => true,
};
```
will be transformed to
```rust
let y = match 92 {
x if x == 0 => false
_ => true,
};
```
a comma is missing after the false. I moved the fix from the code handling else branch to above.
Co-authored-by: Wang Ruochen <wrc@ruo-chen.wang>
| -rw-r--r-- | crates/ide_assists/src/handlers/move_guard.rs | 45 |
1 files changed, 37 insertions, 8 deletions
diff --git a/crates/ide_assists/src/handlers/move_guard.rs b/crates/ide_assists/src/handlers/move_guard.rs index 2f44ca40ab3..8a2c51d33b8 100644 --- a/crates/ide_assists/src/handlers/move_guard.rs +++ b/crates/ide_assists/src/handlers/move_guard.rs @@ -135,7 +135,15 @@ pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContex match &then_block.tail_expr() { Some(then_expr) if then_only_expr => { - edit.replace(replace_node.text_range(), then_expr.syntax().text()) + edit.replace(replace_node.text_range(), then_expr.syntax().text()); + // Insert comma for expression if there isn't one + match match_arm.syntax().last_child_or_token() { + Some(NodeOrToken::Token(t)) if t.kind() == COMMA => {} + _ => { + cov_mark::hit!(move_guard_if_add_comma); + edit.insert(match_arm.syntax().text_range().end(), ","); + } + } } _ if replace_node != *if_expr.syntax() => { // Dedent because if_expr is in a BlockExpr @@ -150,13 +158,6 @@ pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContex // If with only an else branch if let Some(ElseBranch::Block(else_block)) = if_expr.else_branch() { let then_arm_end = match_arm.syntax().text_range().end(); - if then_block.tail_expr().is_some() && then_only_expr { - // Insert comma for expression if there isn't one - match match_arm.syntax().last_child_or_token() { - Some(NodeOrToken::Token(t)) if t.kind() == COMMA => {} - _ => edit.insert(then_arm_end, ","), - } - } let else_only_expr = else_block.statements().next().is_none(); let indent_level = match_arm.indent_level(); let spaces = " ".repeat(indent_level.0 as _); @@ -319,6 +320,34 @@ fn main() { } #[test] + fn move_arm_cond_in_block_to_match_guard_add_comma_works() { + cov_mark::check!(move_guard_if_add_comma); + check_assist( + move_arm_cond_to_match_guard, + r#" +fn main() { + match 92 { + x => { + $0if x > 10 { + false + } + } + _ => true + } +} +"#, + r#" +fn main() { + match 92 { + x if x > 10 => false, + _ => true + } +} +"#, + ); + } + + #[test] fn move_arm_cond_to_match_guard_if_let_not_works() { check_assist_not_applicable( move_arm_cond_to_match_guard, |
