Skip to content

Commit

Permalink
Auto merge of rust-lang#17970 - ChayimFriedman2:unwrap-unsafe-block, …
Browse files Browse the repository at this point in the history
…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 rust-lang#17964.
  • Loading branch information
bors committed Aug 27, 2024
2 parents f0bfd43 + 65e9f8b commit 6593688
Showing 1 changed file with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -824,6 +826,56 @@ fn main() {
let a = 1;
let x = foo;
}
"#,
);
}

#[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;
}
"#,
);
}
Expand Down

0 comments on commit 6593688

Please sign in to comment.