diff --git a/clippy_lints/src/is_unit_expr.rs b/clippy_lints/src/is_unit_expr.rs index ad55d0b09..abaa1edf0 100644 --- a/clippy_lints/src/is_unit_expr.rs +++ b/clippy_lints/src/is_unit_expr.rs @@ -49,7 +49,7 @@ impl EarlyLintPass for UnitExpr { } } if let ExprKind::MethodCall(ref _left, ref args) = expr.node { - for ref arg in args { + for arg in args { if let Some(span) = is_unit_expr(arg) { span_note_and_lint( cx, @@ -63,7 +63,7 @@ impl EarlyLintPass for UnitExpr { } } if let ExprKind::Call(_, ref args) = expr.node { - for ref arg in args { + for arg in args { if let Some(span) = is_unit_expr(arg) { span_note_and_lint( cx, @@ -101,46 +101,45 @@ impl EarlyLintPass for UnitExpr { fn is_unit_expr(expr: &Expr) -> Option { match expr.node { ExprKind::Block(ref block) => if check_last_stmt_in_block(block) { - return Some(block.stmts[block.stmts.len() - 1].span.clone()); + Some(block.stmts[block.stmts.len() - 1].span) } else { - return None; + None }, ExprKind::If(_, ref then, ref else_) => { let check_then = check_last_stmt_in_block(then); if let Some(ref else_) = *else_ { - let check_else = is_unit_expr(&else_); + let check_else = is_unit_expr(else_); if let Some(ref expr_else) = check_else { - return Some(expr_else.clone()); + return Some(*expr_else); } } if check_then { - return Some(expr.span.clone()); + Some(expr.span) } else { - return None; + None } }, ExprKind::Match(ref _pattern, ref arms) => { - for ref arm in arms { + for arm in arms { if let Some(expr) = is_unit_expr(&arm.body) { return Some(expr); } } - return None; + None }, - _ => return None, + _ => None, } } fn check_last_stmt_in_block(block: &Block) -> bool { - let ref final_stmt = &block.stmts[block.stmts.len() - 1]; + let final_stmt = &block.stmts[block.stmts.len() - 1]; match final_stmt.node { - StmtKind::Expr(_) => return false, + StmtKind::Expr(_) => false, StmtKind::Semi(ref expr) => match expr.node { - ExprKind::Break(_, _) => return false, - ExprKind::Ret(_) => return false, - _ => return true, + ExprKind::Break(_, _) | ExprKind::Ret(_) => false, + _ => true, }, - _ => return true, + _ => true, } } diff --git a/tests/ui/is_unit_expr.rs b/tests/ui/is_unit_expr.rs index 63f3fcfa2..8a986494e 100644 --- a/tests/ui/is_unit_expr.rs +++ b/tests/ui/is_unit_expr.rs @@ -4,8 +4,42 @@ #[allow(unused_variables)] fn main() { + + //lint should note removing the semicolon from "baz" let x = { "foo"; "baz"; }; + + + //lint should ignore false positive. + let y = if true{ + "foo" + } else{ + return; + }; + + //lint should note removing semicolon from "bar" + let z = if true{ + "foo"; + } else{ + "bar"; + }; + + + let a1 = Some(5); + + //lint should ignore false positive + let a2 = match a1 { + Some(x) => x, + _ => {return;}, + }; + + //lint should note removing the semicolon after `x;` + let a3 = match a1 { + Some(x) => {x;}, + _ => {0;}, + }; + + } diff --git a/tests/ui/is_unit_expr.stderr b/tests/ui/is_unit_expr.stderr index 73b1e04cb..dafebb1c8 100644 --- a/tests/ui/is_unit_expr.stderr +++ b/tests/ui/is_unit_expr.stderr @@ -1,19 +1,52 @@ -error: This expression assigns the Unit type () - --> $DIR/is_unit_expr.rs:8:13 +error: This expression evaluates to the Unit type () + --> $DIR/is_unit_expr.rs:9:13 | -8 | let x = { +9 | let x = { | _____________^ -9 | | "foo"; -10 | | "baz"; -11 | | }; +10 | | "foo"; +11 | | "baz"; +12 | | }; | |_____^ | = note: `-D unit-expr` implied by `-D warnings` note: Consider removing the trailing semicolon - --> $DIR/is_unit_expr.rs:10:9 + --> $DIR/is_unit_expr.rs:11:9 | -10 | "baz"; +11 | "baz"; | ^^^^^^ -error: aborting due to previous error +error: This expression evaluates to the Unit type () + --> $DIR/is_unit_expr.rs:23:13 + | +23 | let z = if true{ + | _____________^ +24 | | "foo"; +25 | | } else{ +26 | | "bar"; +27 | | }; + | |_____^ + | +note: Consider removing the trailing semicolon + --> $DIR/is_unit_expr.rs:26:9 + | +26 | "bar"; + | ^^^^^^ + +error: This expression evaluates to the Unit type () + --> $DIR/is_unit_expr.rs:39:14 + | +39 | let a3 = match a1 { + | ______________^ +40 | | Some(x) => {x;}, +41 | | _ => {0;}, +42 | | }; + | |_____^ + | +note: Consider removing the trailing semicolon + --> $DIR/is_unit_expr.rs:40:21 + | +40 | Some(x) => {x;}, + | ^^ + +error: aborting due to 3 previous errors