about summary refs log tree commit diff
path: root/compiler/rustc_errors/src
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2023-04-10 16:46:12 +0000
committerEsteban Küber <esteban@kuber.com.ar>2023-04-12 22:50:10 +0000
commit9fadcc143a49a124fa1d0d3261593defb986a987 (patch)
tree893e65c8134b89447a09716b0d0c34979a94cc22 /compiler/rustc_errors/src
parent5b40aa5eb484b31c9a82c1b1a0f414840477b9ea (diff)
downloadrust-9fadcc143a49a124fa1d0d3261593defb986a987.tar.gz
rust-9fadcc143a49a124fa1d0d3261593defb986a987.zip
Special-case item attributes in the suggestion output
Diffstat (limited to 'compiler/rustc_errors/src')
-rw-r--r--compiler/rustc_errors/src/emitter.rs11
1 files changed, 10 insertions, 1 deletions
diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs
index 7b6d65511c1..fe44799efdb 100644
--- a/compiler/rustc_errors/src/emitter.rs
+++ b/compiler/rustc_errors/src/emitter.rs
@@ -1886,6 +1886,7 @@ impl EmitterWriter {
             }
             let mut unhighlighted_lines = Vec::new();
             let mut last_pos = 0;
+            let mut is_item_attribute = false;
             for (line_pos, (line, highlight_parts)) in lines.by_ref().zip(highlights).enumerate() {
                 last_pos = line_pos;
                 debug!(%line_pos, %line, ?highlight_parts);
@@ -1895,6 +1896,12 @@ impl EmitterWriter {
                     unhighlighted_lines.push((line_pos, line));
                     continue;
                 }
+                if highlight_parts.len() == 1
+                    && line.trim().starts_with("#[")
+                    && line.trim().ends_with(']')
+                {
+                    is_item_attribute = true;
+                }
 
                 match unhighlighted_lines.len() {
                     0 => (),
@@ -1971,11 +1978,13 @@ impl EmitterWriter {
                     is_multiline,
                 )
             }
-            if let DisplaySuggestion::Add = show_code_change {
+            if let DisplaySuggestion::Add = show_code_change && is_item_attribute {
                 // The suggestion adds an entire line of code, ending on a newline, so we'll also
                 // print the *following* line, to provide context of what we're advicing people to
                 // do. Otherwise you would only see contextless code that can be confused for
                 // already existing code, despite the colors and UI elements.
+                // We special case `#[derive(_)]\n` and other attribute suggestions, because those
+                // are the ones where context is most useful.
                 let file_lines = sm
                     .span_to_lines(span.primary_span().unwrap().shrink_to_hi())
                     .expect("span_to_lines failed when emitting suggestion");