]> git.lizzy.rs Git - rust.git/commitdiff
Fix all the clippy lints
authorZaki Manian <zaki@manian.org>
Sun, 3 Sep 2017 20:39:49 +0000 (13:39 -0700)
committerZaki Manian <zaki@manian.org>
Sun, 3 Sep 2017 20:39:49 +0000 (13:39 -0700)
Add false positive tests

clippy_lints/src/is_unit_expr.rs
tests/ui/is_unit_expr.rs
tests/ui/is_unit_expr.stderr

index ad55d0b09731b8d176ca497b99d39cb5ba52373c..abaa1edf090e075117fd12867c6c691ebf10f7f4 100644 (file)
@@ -49,7 +49,7 @@ fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
             }
         }
         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 @@ fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
             }
         }
         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 @@ fn check_stmt(&mut self, cx: &EarlyContext, stmt: &Stmt) {
 fn is_unit_expr(expr: &Expr) -> Option<Span> {
     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,
     }
 }
index 63f3fcfa28c37c0c580180a4abdbb40697f2fa0e..8a986494eaf76cca02f91a4c9db368d9a5677ff8 100644 (file)
@@ -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;},
+    };
+
+
 }
index 73b1e04cba790ac87be7026cea1cedc59d3dddd6..dafebb1c82af3f84a2b868464a8fecd8dccbebfd 100644 (file)
@@ -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 = {
    |  _____________^
- | |         "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