Merge pull request #485 from sanxiyn/suggestion
Use suggestion for redundant_closure
This commit is contained in:
commit
2fbe762a99
@ -2,7 +2,7 @@ use rustc::lint::*;
|
||||
use rustc_front::hir::*;
|
||||
use rustc::middle::ty;
|
||||
|
||||
use utils::{snippet, span_lint, is_adjusted};
|
||||
use utils::{snippet_opt, span_lint_and_then, is_adjusted};
|
||||
|
||||
|
||||
#[allow(missing_copy_implementations)]
|
||||
@ -75,9 +75,15 @@ fn check_closure(cx: &LateContext, expr: &Expr) {
|
||||
return
|
||||
}
|
||||
}
|
||||
span_lint(cx, REDUNDANT_CLOSURE, expr.span, &format!(
|
||||
"redundant closure found. Consider using `{}` in its place",
|
||||
snippet(cx, caller.span, "..")));
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
16
src/utils.rs
16
src/utils.rs
@ -195,6 +195,11 @@ pub fn snippet<'a, T: LintContext>(cx: &T, span: Span, default: &'a str) -> Cow<
|
||||
cx.sess().codemap().span_to_snippet(span).map(From::from).unwrap_or(Cow::Borrowed(default))
|
||||
}
|
||||
|
||||
/// Converts a span to a code snippet. Returns None if not available.
|
||||
pub fn snippet_opt<T: LintContext>(cx: &T, span: Span) -> Option<String> {
|
||||
cx.sess().codemap().span_to_snippet(span).ok()
|
||||
}
|
||||
|
||||
/// convert a span (from a block) to a code snippet if available, otherwise use default, e.g.
|
||||
/// `snippet(cx, expr.span, "..")`
|
||||
/// This trims the code of indentation, except for the first line
|
||||
@ -320,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 {
|
||||
|
@ -5,11 +5,17 @@
|
||||
|
||||
fn main() {
|
||||
let a = Some(1u8).map(|a| foo(a));
|
||||
//~^ ERROR redundant closure found. Consider using `foo` in its place
|
||||
//~^ ERROR redundant closure found
|
||||
//~| HELP remove closure as shown
|
||||
//~| SUGGESTION let a = Some(1u8).map(foo);
|
||||
meta(|a| foo(a));
|
||||
//~^ ERROR redundant closure found. Consider using `foo` in its place
|
||||
//~^ ERROR redundant closure found
|
||||
//~| HELP remove closure as shown
|
||||
//~| SUGGESTION meta(foo);
|
||||
let c = Some(1u8).map(|a| {1+2; foo}(a));
|
||||
//~^ ERROR redundant closure found. Consider using `{1+2; foo}` in its place
|
||||
//~^ ERROR redundant closure found
|
||||
//~| HELP remove closure as shown
|
||||
//~| SUGGESTION let c = Some(1u8).map({1+2; foo});
|
||||
let d = Some(1u8).map(|a| foo((|b| foo2(b))(a))); //is adjusted?
|
||||
all(&[1, 2, 3], &&2, |x, y| below(x, y)); //is adjusted
|
||||
unsafe {
|
||||
|
Loading…
x
Reference in New Issue
Block a user