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

+10-1
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

-3
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

-6
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

-1
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

-1
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

-1
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

-4
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

-1
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

-4
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

-2
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

tests/ui/hygiene/no_implicit_prelude.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ LL | Vec::new();
1111
help: consider importing this struct
1212
|
1313
LL + use std::vec::Vec;
14-
LL | pub macro m() { Vec::<i32>::new(); ().clone() }
1514
|
1615

1716
error[E0599]: no method named `clone` found for unit type `()` in the current scope
@@ -28,7 +27,6 @@ LL | ().clone()
2827
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
2928
|
3029
LL + use std::clone::Clone;
31-
LL | pub macro m() { Vec::<i32>::new(); ().clone() }
3230
|
3331

3432
error: aborting due to 2 previous errors

tests/ui/hygiene/trait_items.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ LL | pub macro m() { ().f() }
1515
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
1616
|
1717
LL + use foo::T;
18-
LL | use foo::*;
1918
|
2019

2120
error: aborting due to previous error

tests/ui/impl-trait/no-method-suggested-traits.stderr

-12
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@ LL | 1u32.method();
88
help: the following traits are implemented but not in scope; perhaps add a `use` for one of them:
99
|
1010
LL + use foo::Bar;
11-
LL | extern crate no_method_suggested_traits;
1211
|
1312
LL + use no_method_suggested_traits::Reexported;
14-
LL | extern crate no_method_suggested_traits;
1513
|
1614
LL + use no_method_suggested_traits::foo::PubPub;
17-
LL | extern crate no_method_suggested_traits;
1815
|
1916
LL + use no_method_suggested_traits::qux::PrivPub;
20-
LL | extern crate no_method_suggested_traits;
2117
|
2218

2319
error[E0599]: no method named `method` found for struct `Rc<&mut Box<&u32>>` in the current scope
@@ -30,16 +26,12 @@ LL | std::rc::Rc::new(&mut Box::new(&1u32)).method();
3026
help: the following traits are implemented but not in scope; perhaps add a `use` for one of them:
3127
|
3228
LL + use foo::Bar;
33-
LL | extern crate no_method_suggested_traits;
3429
|
3530
LL + use no_method_suggested_traits::Reexported;
36-
LL | extern crate no_method_suggested_traits;
3731
|
3832
LL + use no_method_suggested_traits::foo::PubPub;
39-
LL | extern crate no_method_suggested_traits;
4033
|
4134
LL + use no_method_suggested_traits::qux::PrivPub;
42-
LL | extern crate no_method_suggested_traits;
4335
|
4436

4537
error[E0599]: no method named `method` found for type `char` in the current scope
@@ -55,7 +47,6 @@ LL | 'a'.method();
5547
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
5648
|
5749
LL + use foo::Bar;
58-
LL | extern crate no_method_suggested_traits;
5950
|
6051

6152
error[E0599]: no method named `method` found for struct `Rc<&mut Box<&char>>` in the current scope
@@ -68,7 +59,6 @@ LL | std::rc::Rc::new(&mut Box::new(&'a')).method();
6859
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
6960
|
7061
LL + use foo::Bar;
71-
LL | extern crate no_method_suggested_traits;
7262
|
7363

7464
error[E0599]: no method named `method` found for type `i32` in the current scope
@@ -86,7 +76,6 @@ LL | fn method(&self) {}
8676
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
8777
|
8878
LL + use no_method_suggested_traits::foo::PubPub;
89-
LL | extern crate no_method_suggested_traits;
9079
|
9180

9281
error[E0599]: no method named `method` found for struct `Rc<&mut Box<&i32>>` in the current scope
@@ -99,7 +88,6 @@ LL | std::rc::Rc::new(&mut Box::new(&1i32)).method();
9988
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
10089
|
10190
LL + use no_method_suggested_traits::foo::PubPub;
102-
LL | extern crate no_method_suggested_traits;
10391
|
10492

10593
error[E0599]: no method named `method` found for struct `Foo` in the current scope

tests/ui/impl-trait/universal_wrong_bounds.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | fn wants_debug(g: impl Debug) { }
77
help: consider importing this trait instead
88
|
99
LL + use std::fmt::Debug;
10-
LL | use std::fmt::Display;
1110
|
1211

1312
error[E0404]: expected trait, found derive macro `Debug`
@@ -19,7 +18,6 @@ LL | fn wants_display(g: impl Debug) { }
1918
help: consider importing this trait instead
2019
|
2120
LL + use std::fmt::Debug;
22-
LL | use std::fmt::Display;
2321
|
2422

2523
error: aborting due to 2 previous errors

tests/ui/imports/glob-resolve1.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ LL | import();
6161
help: consider importing this function
6262
|
6363
LL + use other::import;
64-
LL | use bar::*;
6564
|
6665

6766
error[E0412]: cannot find type `A` in this scope

tests/ui/imports/issue-38293.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ LL | baz();
1313
help: consider importing this function instead
1414
|
1515
LL + use bar::baz;
16-
LL | use foo::f::{self};
1716
|
1817

1918
error: aborting due to 2 previous errors

tests/ui/imports/issue-4366-2.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ LL | foo();
1919
help: consider importing this function instead
2020
|
2121
LL + use foo::foo;
22-
LL | use m1::*;
2322
|
2423

2524
error: aborting due to 2 previous errors

tests/ui/imports/issue-4366.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | fn sub() -> isize { foo(); 1 }
77
help: consider importing this function
88
|
99
LL + use foo::foo;
10-
LL | use a::b::*;
1110
|
1211

1312
error: aborting due to previous error

tests/ui/imports/overlapping_pub_trait.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ LL | pub trait Tr { fn method(&self); }
1313
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
1414
|
1515
LL + use overlapping_pub_trait_source::m::Tr;
16-
LL | extern crate overlapping_pub_trait_source;
1716
|
1817

1918
error: aborting due to previous error

tests/ui/imports/unnamed_pub_trait.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ LL | pub trait Tr { fn method(&self); }
1313
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
1414
|
1515
LL + use unnamed_pub_trait_source::prelude::*; // trait Tr
16-
LL | extern crate unnamed_pub_trait_source;
1716
|
1817

1918
error: aborting due to previous error

tests/ui/issues/issue-10465.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ LL | b.foo();
88
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
99
|
1010
LL + use a::A;
11-
LL | use b::B;
1211
|
1312

1413
error: aborting due to previous error

tests/ui/issues/issue-17546.stderr

-8
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,12 @@ LL | fn new() -> Result<foo::MyEnum, String> {
2525
help: consider importing one of these items instead
2626
|
2727
LL + use std::fmt::Result;
28-
LL | use foo::MyEnum::Result;
2928
|
3029
LL + use std::io::Result;
31-
LL | use foo::MyEnum::Result;
3230
|
3331
LL + use std::result::Result;
34-
LL | use foo::MyEnum::Result;
3532
|
3633
LL + use std::thread::Result;
37-
LL | use foo::MyEnum::Result;
3834
|
3935

4036
error[E0573]: expected type, found variant `Result`
@@ -46,16 +42,12 @@ LL | fn new() -> Result<foo::MyEnum, String> {
4642
help: consider importing one of these items instead
4743
|
4844
LL + use std::fmt::Result;
49-
LL | use foo::MyEnum::Result;
5045
|
5146
LL + use std::io::Result;
52-
LL | use foo::MyEnum::Result;
5347
|
5448
LL + use std::result::Result;
55-
LL | use foo::MyEnum::Result;
5649
|
5750
LL + use std::thread::Result;
58-
LL | use foo::MyEnum::Result;
5951
|
6052

6153
error[E0573]: expected type, found variant `NoResult`

tests/ui/issues/issue-35976.unimported.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ LL | arg.wait();
1010
help: another candidate was found in the following trait, perhaps add a `use` for it:
1111
|
1212
LL + use private::Future;
13-
LL | mod private {
1413
|
1514

1615
error: aborting due to previous error

tests/ui/issues/issue-37534.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | struct Foo<T: ?Hash> { }
77
help: consider importing this trait instead
88
|
99
LL + use std::hash::Hash;
10-
LL | struct Foo<T: ?Hash> { }
1110
|
1211

1312
warning: default bound relaxed for a type parameter, but this does nothing because the given bound is not a default; only `?Sized` is supported

tests/ui/issues/issue-39175.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ LL | Command::new("echo").arg("hello").exec();
88
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
99
|
1010
LL + use std::os::unix::process::CommandExt;
11-
LL | use std::process::Command;
1211
|
1312

1413
error: aborting due to previous error

0 commit comments

Comments
 (0)