Skip to content

Commit c3b4a1f

Browse files
committed
Improve formatting of closure capture migration suggestion.
1 parent eb2226b commit c3b4a1f

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

compiler/rustc_typeck/src/check/upvar.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
649649
match self.tcx.sess.source_map().span_to_snippet(closure_body_span) {
650650
Ok(s) => {
651651
let trimmed = s.trim_start();
652+
let mut lines = trimmed.lines();
653+
let line1 = lines.next().unwrap_or_default();
652654

653655
// If the closure contains a block then replace the opening brace
654656
// with "{ let _ = (..); "
655-
let sugg = if let Some('{') = trimmed.chars().next() {
656-
format!("{{ {}; {}", migration_string, &trimmed[1..])
657+
let sugg = if line1.trim_end() == "{" {
658+
// This is a multi-line closure with just a `{` on the first line,
659+
// so we put the `let` on its own line.
660+
// We take the indentation from the next non-empty line.
661+
let line2 = lines.filter(|line| !line.is_empty()).next().unwrap_or_default();
662+
let indent = line2.split_once(|c: char| !c.is_whitespace()).unwrap_or_default().0;
663+
format!("{{\n{}{};{}", indent, migration_string, &trimmed[line1.len()..])
664+
} else if line1.starts_with('{') {
665+
format!("{{ {}; {}", migration_string, &trimmed[1..].trim_start())
657666
} else {
658667
format!("{{ {}; {} }}", migration_string, s)
659668
};

0 commit comments

Comments
 (0)