]> git.lizzy.rs Git - rust.git/commitdiff
Add an additional empty line between the suggested `use` and the next item
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Fri, 18 Aug 2017 10:46:28 +0000 (12:46 +0200)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Fri, 18 Aug 2017 10:46:28 +0000 (12:46 +0200)
src/librustc_errors/lib.rs
src/librustc_resolve/lib.rs

index 3459771459a021e1c63d67c7ae8c082444dc7b06..26dda2dc42d2bfb47d012631881218f81e8cfa9a 100644 (file)
@@ -217,8 +217,10 @@ fn push_trailing(buf: &mut String,
             if !buf.ends_with('\n') {
                 push_trailing(buf, prev_line.as_ref(), &prev_hi, None);
             }
-            // remove trailing newline
-            buf.pop();
+            // remove trailing newlines
+            while buf.ends_with('\n') {
+                buf.pop();
+            }
         }
         bufs
     }
index 500639dfde69962ff5d069b61d32c711f4b7fd42..a0d7ee44faf616d68bfbd7b2c9c33f1d49f660db 100644 (file)
@@ -584,6 +584,7 @@ fn index_mut(&mut self, ns: Namespace) -> &mut T {
 struct UsePlacementFinder {
     target_module: NodeId,
     span: Option<Span>,
+    found_use: bool,
 }
 
 impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
@@ -611,6 +612,7 @@ fn visit_mod(
                         let mut span = item.span;
                         span.hi = span.lo;
                         self.span = Some(span);
+                        self.found_use = true;
                         return;
                     }
                 },
@@ -3576,11 +3578,12 @@ fn report_with_use_injections(&mut self, krate: &Crate) {
             let mut finder = UsePlacementFinder {
                 target_module: node_id,
                 span: None,
+                found_use: false,
             };
             visit::walk_crate(&mut finder, krate);
             if !candidates.is_empty() {
                 let span = finder.span.expect("did not find module");
-                show_candidates(&mut err, span, &candidates, better);
+                show_candidates(&mut err, span, &candidates, better, finder.found_use);
             }
             err.emit();
         }
@@ -3776,7 +3779,8 @@ fn import_candidate_to_paths(suggestion: &ImportSuggestion) -> (Span, String, St
 fn show_candidates(err: &mut DiagnosticBuilder,
                    span: Span,
                    candidates: &[ImportSuggestion],
-                   better: bool) {
+                   better: bool,
+                   found_use: bool) {
 
     // we want consistent results across executions, but candidates are produced
     // by iterating through a hash map, so make sure they are ordered:
@@ -3792,7 +3796,14 @@ fn show_candidates(err: &mut DiagnosticBuilder,
     let msg = format!("possible {}candidate{} into scope", better, msg_diff);
 
     for candidate in &mut path_strings {
-        *candidate = format!("use {};\n", candidate);
+        // produce an additional newline to separate the new use statement
+        // from the directly following item.
+        let additional_newline = if found_use {
+            ""
+        } else {
+            "\n"
+        };
+        *candidate = format!("use {};\n{}", candidate, additional_newline);
     }
 
     err.span_suggestions(span, &msg, path_strings);