From e613c8b492495aeb5f75f94a0cdcf252573a345f Mon Sep 17 00:00:00 2001 From: mcarton Date: Fri, 1 Jul 2016 18:38:50 +0200 Subject: [PATCH] Introduce `multispan_sugg` --- clippy_lints/src/lib.rs | 1 + clippy_lints/src/utils/mod.rs | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 41a61feab53..6bdfbe98b5b 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -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; diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index df9d09ed010..3e0f0db6ac1 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -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 { -- 2.44.0