Skip to content

Commit 9fadcc1

Browse files
committed
Special-case item attributes in the suggestion output
1 parent 5b40aa5 commit 9fadcc1

File tree

84 files changed

+10
-188
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+10
-188
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,7 @@ impl EmitterWriter {
18861886
}
18871887
let mut unhighlighted_lines = Vec::new();
18881888
let mut last_pos = 0;
1889+
let mut is_item_attribute = false;
18891890
for (line_pos, (line, highlight_parts)) in lines.by_ref().zip(highlights).enumerate() {
18901891
last_pos = line_pos;
18911892
debug!(%line_pos, %line, ?highlight_parts);
@@ -1895,6 +1896,12 @@ impl EmitterWriter {
18951896
unhighlighted_lines.push((line_pos, line));
18961897
continue;
18971898
}
1899+
if highlight_parts.len() == 1
1900+
&& line.trim().starts_with("#[")
1901+
&& line.trim().ends_with(']')
1902+
{
1903+
is_item_attribute = true;
1904+
}
18981905

18991906
match unhighlighted_lines.len() {
19001907
0 => (),
@@ -1971,11 +1978,13 @@ impl EmitterWriter {
19711978
is_multiline,
19721979
)
19731980
}
1974-
if let DisplaySuggestion::Add = show_code_change {
1981+
if let DisplaySuggestion::Add = show_code_change && is_item_attribute {
19751982
// The suggestion adds an entire line of code, ending on a newline, so we'll also
19761983
// print the *following* line, to provide context of what we're advicing people to
19771984
// do. Otherwise you would only see contextless code that can be confused for
19781985
// already existing code, despite the colors and UI elements.
1986+
// We special case `#[derive(_)]\n` and other attribute suggestions, because those
1987+
// are the ones where context is most useful.
19791988
let file_lines = sm
19801989
.span_to_lines(span.primary_span().unwrap().shrink_to_hi())
19811990
.expect("span_to_lines failed when emitting suggestion");

src/tools/clippy/tests/ui/crashes/ice-6252.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ LL | _n: PhantomData,
77
help: consider importing one of these items
88
|
99
LL + use core::marker::PhantomData;
10-
LL | trait TypeVal<T> {
1110
|
1211
LL + use serde::__private::PhantomData;
13-
LL | trait TypeVal<T> {
1412
|
1513
LL + use std::marker::PhantomData;
16-
LL | trait TypeVal<T> {
1714
|
1815

1916
error[E0412]: cannot find type `VAL` in this scope

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ LL + fn default() -> Self {
1414
LL + Self::new()
1515
LL + }
1616
LL + }
17-
LL | impl Foo {
1817
|
1918

2019
error: you should consider adding a `Default` implementation for `Bar`
@@ -32,7 +31,6 @@ LL + fn default() -> Self {
3231
LL + Self::new()
3332
LL + }
3433
LL + }
35-
LL | impl Bar {
3634
|
3735

3836
error: you should consider adding a `Default` implementation for `LtKo<'c>`
@@ -50,7 +48,6 @@ LL + fn default() -> Self {
5048
LL + Self::new()
5149
LL + }
5250
LL + }
53-
LL | impl<'c> LtKo<'c> {
5451
|
5552

5653
error: you should consider adding a `Default` implementation for `NewNotEqualToDerive`
@@ -68,7 +65,6 @@ LL + fn default() -> Self {
6865
LL + Self::new()
6966
LL + }
7067
LL + }
71-
LL | impl NewNotEqualToDerive {
7268
|
7369

7470
error: you should consider adding a `Default` implementation for `FooGenerics<T>`
@@ -86,7 +82,6 @@ LL + fn default() -> Self {
8682
LL + Self::new()
8783
LL + }
8884
LL + }
89-
LL | impl<T> FooGenerics<T> {
9085
|
9186

9287
error: you should consider adding a `Default` implementation for `BarGenerics<T>`
@@ -104,7 +99,6 @@ LL + fn default() -> Self {
10499
LL + Self::new()
105100
LL + }
106101
LL + }
107-
LL | impl<T: Copy> BarGenerics<T> {
108102
|
109103

110104
error: you should consider adding a `Default` implementation for `Foo<T>`

tests/ui/coherence/coherence_inherent.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ LL | s.the_fn();
88
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
99
|
1010
LL + use Lib::TheTrait;
11-
LL | use Lib::TheStruct;
1211
|
1312

1413
error: aborting due to previous error

tests/ui/coherence/coherence_inherent_cc.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ LL | s.the_fn();
88
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
99
|
1010
LL + use coherence_inherent_cc_lib::TheTrait;
11-
LL | use coherence_inherent_cc_lib::TheStruct;
1211
|
1312

1413
error: aborting due to previous error

tests/ui/const-generics/generic_const_exprs/issue-94287.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ help: consider enabling this feature
99
--> $DIR/issue-94287.rs:1:1
1010
|
1111
LL + #![feature(generic_const_exprs)]
12-
LL | #![feature(generic_const_exprs)]
1312
|
1413

1514
error: aborting due to previous error

tests/ui/const-generics/issues/issue-82956.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@ LL | let mut iter = IntoIter::new(self);
77
help: consider importing one of these items
88
|
99
LL + use std::array::IntoIter;
10-
LL | pub struct ConstCheck<const CHECK: bool>;
1110
|
1211
LL + use std::collections::binary_heap::IntoIter;
13-
LL | pub struct ConstCheck<const CHECK: bool>;
1412
|
1513
LL + use std::collections::btree_map::IntoIter;
16-
LL | pub struct ConstCheck<const CHECK: bool>;
1714
|
1815
LL + use std::collections::btree_set::IntoIter;
19-
LL | pub struct ConstCheck<const CHECK: bool>;
2016
|
2117
and 8 other candidates
2218

tests/ui/derived-errors/issue-31997-1.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | let mut map = HashMap::new();
77
help: consider importing this struct
88
|
99
LL + use std::collections::HashMap;
10-
LL | use std::io::prelude::*;
1110
|
1211

1312
error: aborting due to previous error

tests/ui/did_you_mean/issue-56028-there-is-an-enum-variant.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,12 @@ LL | fn setup() -> Set { Set }
2525
help: consider importing one of these items
2626
|
2727
LL + use AffixHeart::Set;
28-
LL | enum PutDown { Set }
2928
|
3029
LL + use CauseToBe::Set;
31-
LL | enum PutDown { Set }
3230
|
3331
LL + use Determine::Set;
34-
LL | enum PutDown { Set }
3532
|
3633
LL + use PutDown::Set;
37-
LL | enum PutDown { Set }
3834
|
3935
and 3 other candidates
4036

tests/ui/hygiene/globs.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ LL | g();
1414
help: consider importing this function
1515
|
1616
LL + use foo::f;
17-
LL | mod foo {
1817
|
1918

2019
error[E0425]: cannot find function `g` in this scope
@@ -41,7 +40,6 @@ LL | f();
4140
help: consider importing this function
4241
|
4342
LL + use bar::g;
44-
LL | mod foo {
4543
|
4644

4745
error[E0425]: cannot find function `f` in this scope

0 commit comments

Comments
 (0)