Skip to content

Commit e613c8b

Browse files
committed
Introduce multispan_sugg
1 parent 97f65b0 commit e613c8b

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extern crate quine_mc_cluskey;
3838

3939
extern crate rustc_serialize;
4040

41+
extern crate rustc_errors;
4142
extern crate rustc_plugin;
4243
extern crate rustc_const_eval;
4344
extern crate rustc_const_math;

clippy_lints/src/utils/mod.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ use rustc::traits::ProjectionMode;
99
use rustc::traits;
1010
use rustc::ty::subst::Subst;
1111
use rustc::ty;
12+
use rustc_errors;
1213
use std::borrow::Cow;
1314
use std::env;
1415
use std::mem;
1516
use std::str::FromStr;
1617
use syntax::ast::{self, LitKind};
17-
use syntax::codemap::{ExpnInfo, Span, ExpnFormat};
18+
use syntax::codemap::{ExpnFormat, ExpnInfo, MultiSpan, Span};
1819
use syntax::errors::DiagnosticBuilder;
1920
use syntax::ptr::P;
2021

@@ -490,6 +491,25 @@ pub fn span_lint_and_then<'a, T: LintContext, F>(cx: &'a T, lint: &'static Lint,
490491
}
491492
}
492493

494+
/// Create a suggestion made from several `span → replacement`.
495+
///
496+
/// Note: in the JSON format (used by `compiletest_rs`), the help message will appear once per
497+
/// replacement. In human-readable format though, it only appears once before the whole suggestion.
498+
pub fn multispan_sugg(db: &mut DiagnosticBuilder, help_msg: String, sugg: &[(Span, &str)]) {
499+
let sugg = rustc_errors::RenderSpan::Suggestion(rustc_errors::CodeSuggestion {
500+
msp: MultiSpan::from_spans(sugg.iter().map(|&(span, _)| span).collect()),
501+
substitutes: sugg.iter().map(|&(_, subs)| subs.to_owned()).collect(),
502+
});
503+
504+
let sub = rustc_errors::SubDiagnostic {
505+
level: rustc_errors::Level::Help,
506+
message: help_msg,
507+
span: MultiSpan::new(),
508+
render_span: Some(sugg),
509+
};
510+
db.children.push(sub);
511+
}
512+
493513
/// Return the base type for references and raw pointers.
494514
pub fn walk_ptrs_ty(ty: ty::Ty) -> ty::Ty {
495515
match ty.sty {

0 commit comments

Comments
 (0)