Skip to content

Commit 66b9b56

Browse files
committed
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.
2 parents 0f7f68d + 104c297 commit 66b9b56

File tree

1 file changed

+55
-3
lines changed

1 file changed

+55
-3
lines changed

Diff for: crates/ide-assists/src/handlers/unwrap_block.rs

+55-3
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,12 @@ fn update_expr_string_without_newline(expr_string: String) -> String {
128128
}
129129

130130
fn update_expr_string_with_pat(expr_str: String, whitespace_pat: &[char]) -> String {
131-
// Remove leading whitespace, index [1..] to remove the leading '{',
131+
// Remove leading whitespace, index to remove the leading '{',
132132
// then continue to remove leading whitespace.
133-
let expr_str =
134-
expr_str.trim_start_matches(whitespace_pat)[1..].trim_start_matches(whitespace_pat);
133+
// We cannot assume the `{` is the first character because there are block modifiers
134+
// (`unsafe`, `async` etc.).
135+
let after_open_brace_index = expr_str.find('{').map_or(0, |it| it + 1);
136+
let expr_str = expr_str[after_open_brace_index..].trim_start_matches(whitespace_pat);
135137

136138
// Remove trailing whitespace, index [..expr_str.len() - 1] to remove the trailing '}',
137139
// then continue to remove trailing whitespace.
@@ -824,6 +826,56 @@ fn main() {
824826
let a = 1;
825827
let x = foo;
826828
}
829+
"#,
830+
);
831+
}
832+
833+
#[test]
834+
fn unwrap_block_with_modifiers() {
835+
// https://github.com/rust-lang/rust-analyzer/issues/17964
836+
check_assist(
837+
unwrap_block,
838+
r#"
839+
fn main() {
840+
unsafe $0{
841+
bar;
842+
}
843+
}
844+
"#,
845+
r#"
846+
fn main() {
847+
bar;
848+
}
849+
"#,
850+
);
851+
check_assist(
852+
unwrap_block,
853+
r#"
854+
fn main() {
855+
async move $0{
856+
bar;
857+
}
858+
}
859+
"#,
860+
r#"
861+
fn main() {
862+
bar;
863+
}
864+
"#,
865+
);
866+
check_assist(
867+
unwrap_block,
868+
r#"
869+
fn main() {
870+
try $0{
871+
bar;
872+
}
873+
}
874+
"#,
875+
r#"
876+
fn main() {
877+
bar;
878+
}
827879
"#,
828880
);
829881
}

0 commit comments

Comments
 (0)