Improve E0777 help message

This commit is contained in:
Guillaume Gomez 2020-10-02 13:52:03 +02:00
parent 8b7aeefede
commit d1d94ba026
3 changed files with 27 additions and 5 deletions

View File

@ -6,6 +6,7 @@ use rustc_ast::tokenstream::{TokenStream, TokenTree};
use rustc_ast::{self as ast, *};
use rustc_data_structures::sync::Lrc;
use rustc_errors::{struct_span_err, Applicability, ErrorReported};
use rustc_lexer::is_ident;
use rustc_parse::nt_to_tokenstream;
use rustc_span::symbol::sym;
use rustc_span::{Span, DUMMY_SP};
@ -182,14 +183,22 @@ crate fn collect_derives(cx: &mut ExtCtxt<'_>, attrs: &mut Vec<ast::Attribute>)
.filter_map(|nmi| match nmi {
NestedMetaItem::Literal(lit) => {
error_reported_filter_map = true;
struct_span_err!(
let mut err = struct_span_err!(
cx.sess,
lit.span,
E0777,
"expected path to a trait, found literal",
)
.help("for example, write `#[derive(Debug)]` for `Debug`")
.emit();
);
let token = lit.token.to_string();
if token.starts_with('"')
&& token.len() > 2
&& is_ident(&token[1..token.len() - 1])
{
err.help(&format!("try using `#[derive({})]`", &token[1..token.len() - 1]));
} else {
err.help("for example, write `#[derive(Debug)]` for `Debug`");
}
err.emit();
None
}
NestedMetaItem::MetaItem(mi) => Some(mi),

View File

@ -1,4 +1,7 @@
#[derive("Clone")] //~ ERROR E0777
#[derive("Clone
")]
//~^^ ERROR E0777
struct Foo;
fn main() {}

View File

@ -4,8 +4,18 @@ error[E0777]: expected path to a trait, found literal
LL | #[derive("Clone")]
| ^^^^^^^
|
= help: try using `#[derive(Clone)]`
error[E0777]: expected path to a trait, found literal
--> $DIR/E0777.rs:2:10
|
LL | #[derive("Clone
| __________^
LL | | ")]
| |_^
|
= help: for example, write `#[derive(Debug)]` for `Debug`
error: aborting due to previous error
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0777`.