Use suggestion for redundant_closure
This commit is contained in:
@@ -2,7 +2,7 @@ use rustc::lint::*;
|
|||||||
use rustc_front::hir::*;
|
use rustc_front::hir::*;
|
||||||
use rustc::middle::ty;
|
use rustc::middle::ty;
|
||||||
|
|
||||||
use utils::{snippet, span_lint, is_adjusted};
|
use utils::{snippet_opt, span_lint, is_adjusted};
|
||||||
|
|
||||||
|
|
||||||
#[allow(missing_copy_implementations)]
|
#[allow(missing_copy_implementations)]
|
||||||
@@ -75,9 +75,12 @@ fn check_closure(cx: &LateContext, expr: &Expr) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
span_lint(cx, REDUNDANT_CLOSURE, expr.span, &format!(
|
span_lint(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure found");
|
||||||
"redundant closure found. Consider using `{}` in its place",
|
if let Some(snippet) = snippet_opt(cx, caller.span) {
|
||||||
snippet(cx, caller.span, "..")));
|
cx.sess().span_suggestion(expr.span,
|
||||||
|
"remove closure as shown:",
|
||||||
|
snippet);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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))
|
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.
|
/// convert a span (from a block) to a code snippet if available, otherwise use default, e.g.
|
||||||
/// `snippet(cx, expr.span, "..")`
|
/// `snippet(cx, expr.span, "..")`
|
||||||
/// This trims the code of indentation, except for the first line
|
/// This trims the code of indentation, except for the first line
|
||||||
|
|||||||
@@ -5,11 +5,17 @@
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let a = Some(1u8).map(|a| foo(a));
|
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));
|
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));
|
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?
|
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
|
all(&[1, 2, 3], &&2, |x, y| below(x, y)); //is adjusted
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|||||||
Reference in New Issue
Block a user