Skip to content

Commit

Permalink
Special-case item attributes in the suggestion output
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Apr 12, 2023
1 parent 5b40aa5 commit 9fadcc1
Show file tree
Hide file tree
Showing 84 changed files with 10 additions and 188 deletions.
11 changes: 10 additions & 1 deletion compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 => (),
Expand Down Expand Up @@ -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");
Expand Down
3 changes: 0 additions & 3 deletions src/tools/clippy/tests/ui/crashes/ice-6252.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ LL | _n: PhantomData,
help: consider importing one of these items
|
LL + use core::marker::PhantomData;
LL | trait TypeVal<T> {
|
LL + use serde::__private::PhantomData;
LL | trait TypeVal<T> {
|
LL + use std::marker::PhantomData;
LL | trait TypeVal<T> {
|

error[E0412]: cannot find type `VAL` in this scope
Expand Down
6 changes: 0 additions & 6 deletions src/tools/clippy/tests/ui/new_without_default.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ LL + fn default() -> Self {
LL + Self::new()
LL + }
LL + }
LL | impl Foo {
|

error: you should consider adding a `Default` implementation for `Bar`
Expand All @@ -32,7 +31,6 @@ LL + fn default() -> Self {
LL + Self::new()
LL + }
LL + }
LL | impl Bar {
|

error: you should consider adding a `Default` implementation for `LtKo<'c>`
Expand All @@ -50,7 +48,6 @@ LL + fn default() -> Self {
LL + Self::new()
LL + }
LL + }
LL | impl<'c> LtKo<'c> {
|

error: you should consider adding a `Default` implementation for `NewNotEqualToDerive`
Expand All @@ -68,7 +65,6 @@ LL + fn default() -> Self {
LL + Self::new()
LL + }
LL + }
LL | impl NewNotEqualToDerive {
|

error: you should consider adding a `Default` implementation for `FooGenerics<T>`
Expand All @@ -86,7 +82,6 @@ LL + fn default() -> Self {
LL + Self::new()
LL + }
LL + }
LL | impl<T> FooGenerics<T> {
|

error: you should consider adding a `Default` implementation for `BarGenerics<T>`
Expand All @@ -104,7 +99,6 @@ LL + fn default() -> Self {
LL + Self::new()
LL + }
LL + }
LL | impl<T: Copy> BarGenerics<T> {
|

error: you should consider adding a `Default` implementation for `Foo<T>`
Expand Down
1 change: 0 additions & 1 deletion tests/ui/coherence/coherence_inherent.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ LL | s.the_fn();
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
LL + use Lib::TheTrait;
LL | use Lib::TheStruct;
|

error: aborting due to previous error
Expand Down
1 change: 0 additions & 1 deletion tests/ui/coherence/coherence_inherent_cc.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ LL | s.the_fn();
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
LL + use coherence_inherent_cc_lib::TheTrait;
LL | use coherence_inherent_cc_lib::TheStruct;
|

error: aborting due to previous error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ help: consider enabling this feature
--> $DIR/issue-94287.rs:1:1
|
LL + #![feature(generic_const_exprs)]
LL | #![feature(generic_const_exprs)]
|

error: aborting due to previous error
Expand Down
4 changes: 0 additions & 4 deletions tests/ui/const-generics/issues/issue-82956.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ LL | let mut iter = IntoIter::new(self);
help: consider importing one of these items
|
LL + use std::array::IntoIter;
LL | pub struct ConstCheck<const CHECK: bool>;
|
LL + use std::collections::binary_heap::IntoIter;
LL | pub struct ConstCheck<const CHECK: bool>;
|
LL + use std::collections::btree_map::IntoIter;
LL | pub struct ConstCheck<const CHECK: bool>;
|
LL + use std::collections::btree_set::IntoIter;
LL | pub struct ConstCheck<const CHECK: bool>;
|
and 8 other candidates

Expand Down
1 change: 0 additions & 1 deletion tests/ui/derived-errors/issue-31997-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ LL | let mut map = HashMap::new();
help: consider importing this struct
|
LL + use std::collections::HashMap;
LL | use std::io::prelude::*;
|

error: aborting due to previous error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@ LL | fn setup() -> Set { Set }
help: consider importing one of these items
|
LL + use AffixHeart::Set;
LL | enum PutDown { Set }
|
LL + use CauseToBe::Set;
LL | enum PutDown { Set }
|
LL + use Determine::Set;
LL | enum PutDown { Set }
|
LL + use PutDown::Set;
LL | enum PutDown { Set }
|
and 3 other candidates

Expand Down
2 changes: 0 additions & 2 deletions tests/ui/hygiene/globs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ LL | g();
help: consider importing this function
|
LL + use foo::f;
LL | mod foo {
|

error[E0425]: cannot find function `g` in this scope
Expand All @@ -41,7 +40,6 @@ LL | f();
help: consider importing this function
|
LL + use bar::g;
LL | mod foo {
|

error[E0425]: cannot find function `f` in this scope
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/hygiene/no_implicit_prelude.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ LL | Vec::new();
help: consider importing this struct
|
LL + use std::vec::Vec;
LL | pub macro m() { Vec::<i32>::new(); ().clone() }
|

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

error: aborting due to 2 previous errors
Expand Down
1 change: 0 additions & 1 deletion tests/ui/hygiene/trait_items.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ LL | pub macro m() { ().f() }
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
LL + use foo::T;
LL | use foo::*;
|

error: aborting due to previous error
Expand Down
12 changes: 0 additions & 12 deletions tests/ui/impl-trait/no-method-suggested-traits.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ LL | 1u32.method();
help: the following traits are implemented but not in scope; perhaps add a `use` for one of them:
|
LL + use foo::Bar;
LL | extern crate no_method_suggested_traits;
|
LL + use no_method_suggested_traits::Reexported;
LL | extern crate no_method_suggested_traits;
|
LL + use no_method_suggested_traits::foo::PubPub;
LL | extern crate no_method_suggested_traits;
|
LL + use no_method_suggested_traits::qux::PrivPub;
LL | extern crate no_method_suggested_traits;
|

error[E0599]: no method named `method` found for struct `Rc<&mut Box<&u32>>` in the current scope
Expand All @@ -30,16 +26,12 @@ LL | std::rc::Rc::new(&mut Box::new(&1u32)).method();
help: the following traits are implemented but not in scope; perhaps add a `use` for one of them:
|
LL + use foo::Bar;
LL | extern crate no_method_suggested_traits;
|
LL + use no_method_suggested_traits::Reexported;
LL | extern crate no_method_suggested_traits;
|
LL + use no_method_suggested_traits::foo::PubPub;
LL | extern crate no_method_suggested_traits;
|
LL + use no_method_suggested_traits::qux::PrivPub;
LL | extern crate no_method_suggested_traits;
|

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

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

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

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

error[E0599]: no method named `method` found for struct `Foo` in the current scope
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/impl-trait/universal_wrong_bounds.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ LL | fn wants_debug(g: impl Debug) { }
help: consider importing this trait instead
|
LL + use std::fmt::Debug;
LL | use std::fmt::Display;
|

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

error: aborting due to 2 previous errors
Expand Down
1 change: 0 additions & 1 deletion tests/ui/imports/glob-resolve1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ LL | import();
help: consider importing this function
|
LL + use other::import;
LL | use bar::*;
|

error[E0412]: cannot find type `A` in this scope
Expand Down
1 change: 0 additions & 1 deletion tests/ui/imports/issue-38293.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ LL | baz();
help: consider importing this function instead
|
LL + use bar::baz;
LL | use foo::f::{self};
|

error: aborting due to 2 previous errors
Expand Down
1 change: 0 additions & 1 deletion tests/ui/imports/issue-4366-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ LL | foo();
help: consider importing this function instead
|
LL + use foo::foo;
LL | use m1::*;
|

error: aborting due to 2 previous errors
Expand Down
1 change: 0 additions & 1 deletion tests/ui/imports/issue-4366.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ LL | fn sub() -> isize { foo(); 1 }
help: consider importing this function
|
LL + use foo::foo;
LL | use a::b::*;
|

error: aborting due to previous error
Expand Down
1 change: 0 additions & 1 deletion tests/ui/imports/overlapping_pub_trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ LL | pub trait Tr { fn method(&self); }
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
LL + use overlapping_pub_trait_source::m::Tr;
LL | extern crate overlapping_pub_trait_source;
|

error: aborting due to previous error
Expand Down
1 change: 0 additions & 1 deletion tests/ui/imports/unnamed_pub_trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ LL | pub trait Tr { fn method(&self); }
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
LL + use unnamed_pub_trait_source::prelude::*; // trait Tr
LL | extern crate unnamed_pub_trait_source;
|

error: aborting due to previous error
Expand Down
1 change: 0 additions & 1 deletion tests/ui/issues/issue-10465.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ LL | b.foo();
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
LL + use a::A;
LL | use b::B;
|

error: aborting due to previous error
Expand Down
8 changes: 0 additions & 8 deletions tests/ui/issues/issue-17546.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@ LL | fn new() -> Result<foo::MyEnum, String> {
help: consider importing one of these items instead
|
LL + use std::fmt::Result;
LL | use foo::MyEnum::Result;
|
LL + use std::io::Result;
LL | use foo::MyEnum::Result;
|
LL + use std::result::Result;
LL | use foo::MyEnum::Result;
|
LL + use std::thread::Result;
LL | use foo::MyEnum::Result;
|

error[E0573]: expected type, found variant `Result`
Expand All @@ -46,16 +42,12 @@ LL | fn new() -> Result<foo::MyEnum, String> {
help: consider importing one of these items instead
|
LL + use std::fmt::Result;
LL | use foo::MyEnum::Result;
|
LL + use std::io::Result;
LL | use foo::MyEnum::Result;
|
LL + use std::result::Result;
LL | use foo::MyEnum::Result;
|
LL + use std::thread::Result;
LL | use foo::MyEnum::Result;
|

error[E0573]: expected type, found variant `NoResult`
Expand Down
1 change: 0 additions & 1 deletion tests/ui/issues/issue-35976.unimported.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ LL | arg.wait();
help: another candidate was found in the following trait, perhaps add a `use` for it:
|
LL + use private::Future;
LL | mod private {
|

error: aborting due to previous error
Expand Down
1 change: 0 additions & 1 deletion tests/ui/issues/issue-37534.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ LL | struct Foo<T: ?Hash> { }
help: consider importing this trait instead
|
LL + use std::hash::Hash;
LL | struct Foo<T: ?Hash> { }
|

warning: default bound relaxed for a type parameter, but this does nothing because the given bound is not a default; only `?Sized` is supported
Expand Down
1 change: 0 additions & 1 deletion tests/ui/issues/issue-39175.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ LL | Command::new("echo").arg("hello").exec();
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
LL + use std::os::unix::process::CommandExt;
LL | use std::process::Command;
|

error: aborting due to previous error
Expand Down
Loading

0 comments on commit 9fadcc1

Please sign in to comment.