Skip to content

Commit e3dbcdf

Browse files
authored
Rollup merge of #148004 - Muscraft:only-single-line-item-attributes, r=estebank
fix: Only special case single line item attribute suggestions `rustc` currently special cases suggestions to add [`#[derive(_)]\n` and other attributes](https://github.com/rust-lang/rust/blob/dc1feabef242259d61bd930713de3250577c1c71/compiler/rustc_errors/src/emitter.rs#L2288C36-L2288C72), to add more context to the suggestions. > // 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 advising 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. This special case is a bit broad at the moment and applies to suggestions just to add an attribute, as well as suggestions that contain an attribute and other code, i.e. ```rust #[derive(Clone)] ``` and ```rust #[cfg(not(test))] impl Default for NewWithCfg { fn default() -> Self { Self::new() } } ``` In the latter case, adding a line for context after the suggestion doesn't provide much benefit. Example: ![temp](https://github.com/user-attachments/assets/6859b400-aa99-4c1b-9eb0-0cd67ae35bf9) This PR makes it so that this special case only applies to suggestions that just add an attribute and nothing else. This will also make `rustc`'s output match `annotate-snippets`.
2 parents 3de72f6 + 73515c7 commit e3dbcdf

File tree

2 files changed

+2
-3
lines changed

2 files changed

+2
-3
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,6 +2155,7 @@ impl HumanEmitter {
21552155

21562156
let line_start = sm.lookup_char_pos(parts[0].original_span.lo()).line;
21572157
let mut lines = complete.lines();
2158+
let lines_len = lines.clone().count();
21582159
if lines.clone().next().is_none() {
21592160
// Account for a suggestion to completely remove a line(s) with whitespace (#94192).
21602161
let line_end = sm.lookup_char_pos(parts[0].original_span.hi()).line;
@@ -2195,6 +2196,7 @@ impl HumanEmitter {
21952196
if highlight_parts.len() == 1
21962197
&& line.trim().starts_with("#[")
21972198
&& line.trim().ends_with(']')
2199+
&& lines_len == 1
21982200
{
21992201
is_item_attribute = true;
22002202
}

src/tools/clippy/tests/ui/new_without_default.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ LL + fn default() -> Self {
191191
LL + Self::new()
192192
LL + }
193193
LL + }
194-
LL | impl NewWithCfg {
195194
|
196195

197196
error: you should consider adding a `Default` implementation for `NewWith2Cfgs`
@@ -212,7 +211,6 @@ LL + fn default() -> Self {
212211
LL + Self::new()
213212
LL + }
214213
LL + }
215-
LL | impl NewWith2Cfgs {
216214
|
217215

218216
error: you should consider adding a `Default` implementation for `NewWithExtraneous`
@@ -250,7 +248,6 @@ LL + fn default() -> Self {
250248
LL + Self::new()
251249
LL + }
252250
LL + }
253-
LL | impl NewWithCfgAndExtraneous {
254251
|
255252

256253
error: aborting due to 13 previous errors

0 commit comments

Comments
 (0)