From 2a97aadacf2907b82bd57d0dafe8aa4f7e904153 Mon Sep 17 00:00:00 2001 From: Zaki Manian Date: Sat, 2 Sep 2017 14:09:41 -0700 Subject: [PATCH] More initial work --- clippy_lints/src/is_unit_expr.rs | 56 ++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/clippy_lints/src/is_unit_expr.rs b/clippy_lints/src/is_unit_expr.rs index b8c85813d3f..07991082872 100644 --- a/clippy_lints/src/is_unit_expr.rs +++ b/clippy_lints/src/is_unit_expr.rs @@ -35,19 +35,55 @@ impl LintPass for UnitExpr { impl EarlyLintPass for UnitExpr { fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) { if let ExprKind::Assign(ref left, ref right) = expr.node { - unimplemented!(); - } - if let ExprKind::MethodCall(ref path, ref args) = expr.node { - unimplemented!(); - } - if let ExprKind::Call(ref path, ref args) = expr.node{ - unimplemented!(); + if is_unit_expr(right){ + span_lint_and_sugg( + cx, + UNIT_EXPR, + right.span, + "trailing semicolons can be tricky", + "remove the last semicolon", + "TODO".to_owned() + ) + } } + // if let ExprKind::MethodCall(ref path, ref args) = expr.node { + // unimplemented!(); + // } + // if let ExprKind::Call(ref path, ref args) = expr.node{ + // unimplemented!(); + // } } fn check_stmt(&mut self, cx: &EarlyContext, stmt: &Stmt) { - if let StmtKind::Local(ref data) = stmt.node{ - unimplemented!(); - } + if let StmtKind::Local(ref local) = stmt.node{ + if local.pat.node == PatKind::Wild {return;} + if let Some(ref expr) = local.init{ + if is_unit_expr(expr){ + span_lint_and_sugg( + cx, + UNIT_EXPR, + local.span, + "trailing semicolons can be tricky", + "remove the last semicolon", + "TODO".to_owned() + ) + } + } + } } } + +fn is_unit_expr(expr: &Expr)->bool{ + match expr.node{ + ExprKind::Block(ref next) => { + let ref final_stmt = &next.stmts[next.stmts.len()-1]; + if let StmtKind::Expr(_) = final_stmt.node{ + return false; + } + else{ + return true; + } + }, + _ => return false, + } +} \ No newline at end of file