1
Fork 0

Don't introduce a block if a block exists

This commit is contained in:
Aman Arora 2021-04-01 21:08:04 -04:00
parent da86348707
commit a721957a3d
10 changed files with 189 additions and 63 deletions

View file

@ -495,10 +495,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let closure_body_span = self.tcx.hir().span(body_id.hir_id);
let (sugg, app) =
match self.tcx.sess.source_map().span_to_snippet(closure_body_span) {
Ok(s) => (
format!("{{ {}; {} }}", migration_string, s),
Applicability::MachineApplicable,
),
Ok(s) => {
let trimmed = s.trim_start();
// 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..])
} else {
format!("{{ {}; {} }}", migration_string, s)
};
(sugg, Applicability::MachineApplicable)
}
Err(_) => (migration_string.clone(), Applicability::HasPlaceholders),
};