about summary refs log tree commit diff
path: root/compiler/rustc_errors/src
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-03-01 22:38:45 +0100
committerGitHub <noreply@github.com>2024-03-01 22:38:45 +0100
commit3c89280684c07aafc2ce34e7d7c2df88184d896a (patch)
treeab2a78e519aef3c78ec319a5e496a99691ce06a4 /compiler/rustc_errors/src
parent8185c843f3fab1413beb15bd79d92c42d10740bd (diff)
parent367126d49af3237eee24889191dfbb02dfc9320c (diff)
downloadrust-3c89280684c07aafc2ce34e7d7c2df88184d896a.tar.gz
rust-3c89280684c07aafc2ce34e7d7c2df88184d896a.zip
Rollup merge of #120305 - clubby789:unused-import-line, r=estebank
Delete line if suggestion would replace it with an empty line

Fixes #120296
Diffstat (limited to 'compiler/rustc_errors/src')
-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);