2 use rustc::lint::{Context, LintPass, LintArray, Lint, Level};
3 use syntax::codemap::{Span, Spanned};
4 use syntax::print::pprust::expr_to_string;
9 #[allow(missing_copy_implementations)]
13 declare_lint!(pub REDUNDANT_CLOSURE, Warn,
14 "Warn on usage of redundant closures, i.e. `|a| foo(a)`");
16 impl LintPass for EtaPass {
17 fn get_lints(&self) -> LintArray {
18 lint_array!(REDUNDANT_CLOSURE)
21 fn check_expr(&mut self, cx: &Context, expr: &Expr) {
22 if let ExprClosure(_, ref decl, ref blk) = expr.node {
23 if !blk.stmts.is_empty() {
24 // || {foo(); bar()}; can't be reduced here
27 if let Some(ref ex) = blk.expr {
28 if let ExprCall(ref caller, ref args) = ex.node {
29 if args.len() != decl.inputs.len() {
30 // Not the same number of arguments, there
31 // is no way the closure is the same as the function
34 for (ref a1, ref a2) in decl.inputs.iter().zip(args) {
35 if let PatIdent(_, ident, _) = a1.pat.node {
36 // XXXManishearth Should I be checking the binding mode here?
37 if let ExprPath(None, ref p) = a2.node {
38 if p.segments.len() != 1 {
39 // If it's a proper path, it can't be a local variable
42 if p.segments[0].identifier != ident.node {
43 // The two idents should be the same
53 span_lint(cx, REDUNDANT_CLOSURE, expr.span,
54 &format!("redundant closure found. Consider using `{}` in its place.",
55 expr_to_string(caller))[..])