diff options
| author | bors <bors@rust-lang.org> | 2024-08-27 09:17:10 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-08-27 09:17:10 +0000 |
| commit | 65936887fffff8bf36aa63ec9560ab44bb18ade8 (patch) | |
| tree | f641b24a6217d804684c2ff12b9705541321ec87 | |
| parent | f0bfd43b323b2def975379616f34ecfe57048dfd (diff) | |
| parent | 65e9f8ba7f53fa0697b5d1456aefef6f5560335d (diff) | |
| download | rust-65936887fffff8bf36aa63ec9560ab44bb18ade8.tar.gz rust-65936887fffff8bf36aa63ec9560ab44bb18ade8.zip | |
Auto merge of #17970 - ChayimFriedman2:unwrap-unsafe-block, r=Veykril
fix: Fix "Unwrap block" assist with block modifiers
The assist just assumes the `{` will be the first character, which led to strange outputs such as `nsafe {`.
Fixes #17964.
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-assists/src/handlers/unwrap_block.rs | 58 |
1 files changed, 55 insertions, 3 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/unwrap_block.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/unwrap_block.rs index de801279a0e..f3e7f5f4167 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/unwrap_block.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/unwrap_block.rs @@ -128,10 +128,12 @@ fn update_expr_string_without_newline(expr_string: String) -> String { } fn update_expr_string_with_pat(expr_str: String, whitespace_pat: &[char]) -> String { - // Remove leading whitespace, index [1..] to remove the leading '{', + // Remove leading whitespace, index to remove the leading '{', // then continue to remove leading whitespace. - let expr_str = - expr_str.trim_start_matches(whitespace_pat)[1..].trim_start_matches(whitespace_pat); + // We cannot assume the `{` is the first character because there are block modifiers + // (`unsafe`, `async` etc.). + let after_open_brace_index = expr_str.find('{').map_or(0, |it| it + 1); + let expr_str = expr_str[after_open_brace_index..].trim_start_matches(whitespace_pat); // Remove trailing whitespace, index [..expr_str.len() - 1] to remove the trailing '}', // then continue to remove trailing whitespace. @@ -827,4 +829,54 @@ fn main() { "#, ); } + + #[test] + fn unwrap_block_with_modifiers() { + // https://github.com/rust-lang/rust-analyzer/issues/17964 + check_assist( + unwrap_block, + r#" +fn main() { + unsafe $0{ + bar; + } +} +"#, + r#" +fn main() { + bar; +} +"#, + ); + check_assist( + unwrap_block, + r#" +fn main() { + async move $0{ + bar; + } +} +"#, + r#" +fn main() { + bar; +} +"#, + ); + check_assist( + unwrap_block, + r#" +fn main() { + try $0{ + bar; + } +} +"#, + r#" +fn main() { + bar; +} +"#, + ); + } } |
