From f56c61face0e6ca8eaef8d872413e984c717657b Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 19 Apr 2018 19:51:53 -0700 Subject: [PATCH] Add suggestion to lint --- src/librustc/lint/builtin.rs | 20 +++++++++++++++++++- src/librustc_resolve/lib.rs | 9 ++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index 30ae830ee464..109edffcde38 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -332,7 +332,8 @@ impl LintPass for HardwiredLints { #[derive(PartialEq, RustcEncodable, RustcDecodable, Debug)] pub enum BuiltinLintDiagnostics { Normal, - BareTraitObject(Span, /* is_global */ bool) + BareTraitObject(Span, /* is_global */ bool), + AbsPathWithModule(Span), } impl BuiltinLintDiagnostics { @@ -347,6 +348,23 @@ impl BuiltinLintDiagnostics { }; db.span_suggestion(span, "use `dyn`", sugg); } + BuiltinLintDiagnostics::AbsPathWithModule(span) => { + let sugg = match sess.codemap().span_to_snippet(span) { + Ok(ref s) => { + // FIXME(Manishearth) ideally the emitting code + // can tell us whether or not this is global + let opt_colon = if s.trim_left().starts_with("::") { + "" + } else { + "::" + }; + + format!("crate{}{}", opt_colon, s) + } + Err(_) => format!("crate::") + }; + db.span_suggestion(span, "use `crate`", sugg); + } } } } diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index c0bb6921b693..a41cd62ea7a8 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -3344,11 +3344,14 @@ impl<'a> Resolver<'a> { } if !is_crate { - self.session.buffer_lint( + let diag = lint::builtin::BuiltinLintDiagnostics + ::AbsPathWithModule(path_span); + self.session.buffer_lint_with_diagnostic( lint::builtin::ABSOLUTE_PATH_STARTING_WITH_MODULE, id, path_span, - "Fully-qualified paths must start with `self`, `super`, - `crate`, or an external crate name in the 2018 edition"); + "Absolute paths must start with `self`, `super`, \ + `crate`, or an external crate name in the 2018 edition", + diag); } } }