Skip to content

Use suggestion for redundant_closure #485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/eta_reduction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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);
}
});
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 9 additions & 3 deletions tests/compile-fail/eta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down