about summary refs log tree commit diff
path: root/compiler/rustc_errors/src/json.rs
diff options
context:
space:
mode:
authorclubby789 <jamie@hill-daniel.co.uk>2024-01-26 15:32:59 +0000
committerclubby789 <jamie@hill-daniel.co.uk>2024-03-01 13:48:20 +0000
commit367126d49af3237eee24889191dfbb02dfc9320c (patch)
treeb320fb996521d21141ae7b746df538df663a66e4 /compiler/rustc_errors/src/json.rs
parentb0696a5160711c068cb1f01b7437db7990d15750 (diff)
downloadrust-367126d49af3237eee24889191dfbb02dfc9320c.tar.gz
rust-367126d49af3237eee24889191dfbb02dfc9320c.zip
If suggestion would leave an empty line, delete it
Diffstat (limited to 'compiler/rustc_errors/src/json.rs')
-rw-r--r--compiler/rustc_errors/src/json.rs12
1 files changed, 11 insertions, 1 deletions
diff --git a/compiler/rustc_errors/src/json.rs b/compiler/rustc_errors/src/json.rs
index bc1822f83fc..af82d8092c2 100644
--- a/compiler/rustc_errors/src/json.rs
+++ b/compiler/rustc_errors/src/json.rs
@@ -428,7 +428,7 @@ impl DiagnosticSpan {
     }
 
     fn from_span_full(
-        span: Span,
+        mut span: Span,
         is_primary: bool,
         label: Option<String>,
         suggestion: Option<(&String, Applicability)>,
@@ -436,6 +436,16 @@ impl DiagnosticSpan {
         je: &JsonEmitter,
     ) -> DiagnosticSpan {
         let start = je.sm.lookup_char_pos(span.lo());
+        // If this goes from the start of a line to the end and the replacement
+        // is an empty string, increase the length to include the newline so we don't
+        // leave an empty line
+        if start.col.0 == 0
+            && suggestion.map_or(false, |(s, _)| s.is_empty())
+            && let Ok(after) = je.sm.span_to_next_source(span)
+            && after.starts_with('\n')
+        {
+            span = span.with_hi(span.hi() + rustc_span::BytePos(1));
+        }
         let end = je.sm.lookup_char_pos(span.hi());
         let backtrace_step = backtrace.next().map(|bt| {
             let call_site = Self::from_span_full(bt.call_site, false, None, None, backtrace, je);