diff options
| author | Mara Bos <m-ou.se@m-ou.se> | 2021-08-12 11:23:18 +0200 |
|---|---|---|
| committer | Mara Bos <m-ou.se@m-ou.se> | 2021-08-12 11:23:18 +0200 |
| commit | c3b4a1f9bf09b40dafb0cf096cd955a362e0d758 (patch) | |
| tree | 44aad8841ce0d04816365c0c12151a924b2fe829 | |
| parent | eb2226b1f174f3cc644275ef8663be6295a7f704 (diff) | |
| download | rust-c3b4a1f9bf09b40dafb0cf096cd955a362e0d758.tar.gz rust-c3b4a1f9bf09b40dafb0cf096cd955a362e0d758.zip | |
Improve formatting of closure capture migration suggestion.
| -rw-r--r-- | compiler/rustc_typeck/src/check/upvar.rs | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/compiler/rustc_typeck/src/check/upvar.rs b/compiler/rustc_typeck/src/check/upvar.rs index 013cb2a49b2..9cf741bc63f 100644 --- a/compiler/rustc_typeck/src/check/upvar.rs +++ b/compiler/rustc_typeck/src/check/upvar.rs @@ -649,11 +649,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { match self.tcx.sess.source_map().span_to_snippet(closure_body_span) { Ok(s) => { let trimmed = s.trim_start(); + let mut lines = trimmed.lines(); + let line1 = lines.next().unwrap_or_default(); // If the closure contains a block then replace the opening brace // with "{ let _ = (..); " - let sugg = if let Some('{') = trimmed.chars().next() { - format!("{{ {}; {}", migration_string, &trimmed[1..]) + let sugg = if line1.trim_end() == "{" { + // This is a multi-line closure with just a `{` on the first line, + // so we put the `let` on its own line. + // We take the indentation from the next non-empty line. + let line2 = lines.filter(|line| !line.is_empty()).next().unwrap_or_default(); + let indent = line2.split_once(|c: char| !c.is_whitespace()).unwrap_or_default().0; + format!("{{\n{}{};{}", indent, migration_string, &trimmed[line1.len()..]) + } else if line1.starts_with('{') { + format!("{{ {}; {}", migration_string, &trimmed[1..].trim_start()) } else { format!("{{ {}; {} }}", migration_string, s) }; |
