]> git.lizzy.rs Git - rust.git/commitdiff
Introduce `multispan_sugg`
authormcarton <cartonmartin+git@gmail.com>
Fri, 1 Jul 2016 16:38:50 +0000 (18:38 +0200)
committermcarton <cartonmartin+git@gmail.com>
Fri, 1 Jul 2016 16:43:42 +0000 (18:43 +0200)
clippy_lints/src/lib.rs
clippy_lints/src/utils/mod.rs

index 41a61feab5390446710ae4b610a2117c786520ed..6bdfbe98b5bc639a0d6ab4a53c83c0462267dc7a 100644 (file)
@@ -38,6 +38,7 @@
 
 extern crate rustc_serialize;
 
+extern crate rustc_errors;
 extern crate rustc_plugin;
 extern crate rustc_const_eval;
 extern crate rustc_const_math;
index df9d09ed0106ef024317218bfdeb683f38194d74..3e0f0db6ac170d7c98b58b914add2d9b092890dc 100644 (file)
@@ -9,12 +9,13 @@
 use rustc::traits;
 use rustc::ty::subst::Subst;
 use rustc::ty;
+use rustc_errors;
 use std::borrow::Cow;
 use std::env;
 use std::mem;
 use std::str::FromStr;
 use syntax::ast::{self, LitKind};
-use syntax::codemap::{ExpnInfo, Span, ExpnFormat};
+use syntax::codemap::{ExpnFormat, ExpnInfo, MultiSpan, Span};
 use syntax::errors::DiagnosticBuilder;
 use syntax::ptr::P;
 
@@ -490,6 +491,25 @@ pub fn span_lint_and_then<'a, T: LintContext, F>(cx: &'a T, lint: &'static Lint,
     }
 }
 
+/// Create a suggestion made from several `span → replacement`.
+///
+/// Note: in the JSON format (used by `compiletest_rs`), the help message will appear once per
+/// replacement. In human-readable format though, it only appears once before the whole suggestion.
+pub fn multispan_sugg(db: &mut DiagnosticBuilder, help_msg: String, sugg: &[(Span, &str)]) {
+    let sugg = rustc_errors::RenderSpan::Suggestion(rustc_errors::CodeSuggestion {
+        msp: MultiSpan::from_spans(sugg.iter().map(|&(span, _)| span).collect()),
+        substitutes: sugg.iter().map(|&(_, subs)| subs.to_owned()).collect(),
+    });
+
+    let sub = rustc_errors::SubDiagnostic {
+        level: rustc_errors::Level::Help,
+        message: help_msg,
+        span: MultiSpan::new(),
+        render_span: Some(sugg),
+    };
+    db.children.push(sub);
+}
+
 /// Return the base type for references and raw pointers.
 pub fn walk_ptrs_ty(ty: ty::Ty) -> ty::Ty {
     match ty.sty {