Skip to content

Commit 8f56322

Browse files
committed
Add an additional empty line between the suggested use and the next item
1 parent e0ba29c commit 8f56322

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/librustc_errors/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,10 @@ impl CodeSuggestion {
217217
if !buf.ends_with('\n') {
218218
push_trailing(buf, prev_line.as_ref(), &prev_hi, None);
219219
}
220-
// remove trailing newline
221-
buf.pop();
220+
// remove trailing newlines
221+
while buf.ends_with('\n') {
222+
buf.pop();
223+
}
222224
}
223225
bufs
224226
}

src/librustc_resolve/lib.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
584584
struct UsePlacementFinder {
585585
target_module: NodeId,
586586
span: Option<Span>,
587+
found_use: bool,
587588
}
588589

589590
impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
@@ -611,6 +612,7 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
611612
let mut span = item.span;
612613
span.hi = span.lo;
613614
self.span = Some(span);
615+
self.found_use = true;
614616
return;
615617
}
616618
},
@@ -3576,11 +3578,12 @@ impl<'a> Resolver<'a> {
35763578
let mut finder = UsePlacementFinder {
35773579
target_module: node_id,
35783580
span: None,
3581+
found_use: false,
35793582
};
35803583
visit::walk_crate(&mut finder, krate);
35813584
if !candidates.is_empty() {
35823585
let span = finder.span.expect("did not find module");
3583-
show_candidates(&mut err, span, &candidates, better);
3586+
show_candidates(&mut err, span, &candidates, better, finder.found_use);
35843587
}
35853588
err.emit();
35863589
}
@@ -3776,7 +3779,8 @@ fn import_candidate_to_paths(suggestion: &ImportSuggestion) -> (Span, String, St
37763779
fn show_candidates(err: &mut DiagnosticBuilder,
37773780
span: Span,
37783781
candidates: &[ImportSuggestion],
3779-
better: bool) {
3782+
better: bool,
3783+
found_use: bool) {
37803784

37813785
// we want consistent results across executions, but candidates are produced
37823786
// by iterating through a hash map, so make sure they are ordered:
@@ -3792,7 +3796,14 @@ fn show_candidates(err: &mut DiagnosticBuilder,
37923796
let msg = format!("possible {}candidate{} into scope", better, msg_diff);
37933797

37943798
for candidate in &mut path_strings {
3795-
*candidate = format!("use {};\n", candidate);
3799+
// produce an additional newline to separate the new use statement
3800+
// from the directly following item.
3801+
let additional_newline = if found_use {
3802+
""
3803+
} else {
3804+
"\n"
3805+
};
3806+
*candidate = format!("use {};\n{}", candidate, additional_newline);
37963807
}
37973808

37983809
err.span_suggestions(span, &msg, path_strings);

0 commit comments

Comments
 (0)