Add span_lint_and_then and use it

This commit is contained in:
Seo Sanghyeon 2015-12-09 01:28:35 +09:00
parent 35b5c3efdd
commit 213c15cd66
2 changed files with 21 additions and 7 deletions

View File

@ -2,7 +2,7 @@ use rustc::lint::*;
use rustc_front::hir::*;
use rustc::middle::ty;
use utils::{snippet_opt, span_lint, is_adjusted};
use utils::{snippet_opt, span_lint_and_then, is_adjusted};
#[allow(missing_copy_implementations)]
@ -75,12 +75,15 @@ fn check_closure(cx: &LateContext, expr: &Expr) {
return
}
}
span_lint(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure found");
if let Some(snippet) = snippet_opt(cx, caller.span) {
cx.sess().span_suggestion(expr.span,
"remove closure as shown:",
snippet);
}
span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span,
"redundant closure found",
|| {
if let Some(snippet) = snippet_opt(cx, caller.span) {
cx.sess().span_suggestion(expr.span,
"remove closure as shown:",
snippet);
}
});
}
}
}

View File

@ -325,6 +325,17 @@ pub fn span_note_and_lint<T: LintContext>(cx: &T, lint: &'static Lint, span: Spa
}
}
pub fn span_lint_and_then<T: LintContext, F>(cx: &T, lint: &'static Lint, sp: Span,
msg: &str, f: F) where F: Fn() {
cx.span_lint(lint, sp, msg);
if cx.current_level(lint) != Level::Allow {
f();
cx.sess().fileline_help(sp, &format!("for further information visit \
https://github.com/Manishearth/rust-clippy/wiki#{}",
lint.name_lower()))
}
}
/// return the base type for references and raw pointers
pub fn walk_ptrs_ty(ty: ty::Ty) -> ty::Ty {
match ty.sty {