rust/compiler/rustc_errors/src
Matthias Krüger e3dbcdf753
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](dc1feabef2/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`.
2025-11-03 21:20:22 +01:00
..
json Extract Translator struct 2025-06-19 13:02:04 -05:00
markdown chore: Remove unneeded ansi escape resets from output 2025-10-20 12:13:26 -06:00
annotate_snippet_emitter_writer.rs chore: Update to the latest annotate-snippets 2025-10-24 12:50:19 -06:00
codes.rs Move IntoDiagArg earlier in the dependency chains 2025-08-20 15:01:13 -07:00
decorate_diag.rs Refactor lint buffering to avoid requiring a giant enum 2025-08-22 01:59:56 -07:00
diagnostic.rs Revert "Auto merge of #146121 - Muscraft:filter-suggestion-parts, r=petrochenkov" 2025-10-23 18:18:35 +00:00
diagnostic_impls.rs Move IntoDiagArg earlier in the dependency chains 2025-08-20 15:01:13 -07:00
emitter.rs Rollup merge of #148004 - Muscraft:only-single-line-item-attributes, r=estebank 2025-11-03 21:20:22 +01:00
error.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
json.rs refactor: Move to anstream + anstyle for styling 2025-10-20 12:13:25 -06:00
lib.rs Revert "Auto merge of #146121 - Muscraft:filter-suggestion-parts, r=petrochenkov" 2025-10-23 18:18:35 +00:00
lock.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
registry.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
snippet.rs Use abs_diff where applicable 2025-03-27 18:29:06 +00:00
styled_buffer.rs use let chains in ast, borrowck, codegen, const_eval 2025-07-28 06:08:48 +05:00
tests.rs Extract Translator struct 2025-06-19 13:02:04 -05:00
timings.rs Add codegen timing section 2025-06-23 08:50:17 +02:00
translation.rs Extract Translator struct 2025-06-19 13:02:04 -05:00