Fix all the clippy lints

Add false positive tests
This commit is contained in:
Zaki Manian 2017-09-03 13:39:49 -07:00
parent 6657d4e7ff
commit 8b53f2238b
3 changed files with 92 additions and 26 deletions

View file

@ -49,7 +49,7 @@ impl EarlyLintPass for UnitExpr {
} }
} }
if let ExprKind::MethodCall(ref _left, ref args) = expr.node { 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) { if let Some(span) = is_unit_expr(arg) {
span_note_and_lint( span_note_and_lint(
cx, cx,
@ -63,7 +63,7 @@ impl EarlyLintPass for UnitExpr {
} }
} }
if let ExprKind::Call(_, ref args) = expr.node { 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) { if let Some(span) = is_unit_expr(arg) {
span_note_and_lint( span_note_and_lint(
cx, cx,
@ -101,46 +101,45 @@ impl EarlyLintPass for UnitExpr {
fn is_unit_expr(expr: &Expr) -> Option<Span> { fn is_unit_expr(expr: &Expr) -> Option<Span> {
match expr.node { match expr.node {
ExprKind::Block(ref block) => if check_last_stmt_in_block(block) { 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 { } else {
return None; None
}, },
ExprKind::If(_, ref then, ref else_) => { ExprKind::If(_, ref then, ref else_) => {
let check_then = check_last_stmt_in_block(then); let check_then = check_last_stmt_in_block(then);
if let Some(ref else_) = *else_ { 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 { if let Some(ref expr_else) = check_else {
return Some(expr_else.clone()); return Some(*expr_else);
} }
} }
if check_then { if check_then {
return Some(expr.span.clone()); Some(expr.span)
} else { } else {
return None; None
} }
}, },
ExprKind::Match(ref _pattern, ref arms) => { ExprKind::Match(ref _pattern, ref arms) => {
for ref arm in arms { for arm in arms {
if let Some(expr) = is_unit_expr(&arm.body) { if let Some(expr) = is_unit_expr(&arm.body) {
return Some(expr); return Some(expr);
} }
} }
return None; None
}, },
_ => return None, _ => None,
} }
} }
fn check_last_stmt_in_block(block: &Block) -> bool { 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 { match final_stmt.node {
StmtKind::Expr(_) => return false, StmtKind::Expr(_) => false,
StmtKind::Semi(ref expr) => match expr.node { StmtKind::Semi(ref expr) => match expr.node {
ExprKind::Break(_, _) => return false, ExprKind::Break(_, _) | ExprKind::Ret(_) => false,
ExprKind::Ret(_) => return false, _ => true,
_ => return true,
}, },
_ => return true, _ => true,
} }
} }

View file

@ -4,8 +4,42 @@
#[allow(unused_variables)] #[allow(unused_variables)]
fn main() { fn main() {
//lint should note removing the semicolon from "baz"
let x = { let x = {
"foo"; "foo";
"baz"; "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;},
};
} }

View file

@ -1,19 +1,52 @@
error: This expression assigns the Unit type () error: This expression evaluates to the Unit type ()
--> $DIR/is_unit_expr.rs:8:13 --> $DIR/is_unit_expr.rs:9:13
| |
8 | let x = { 9 | let x = {
| _____________^ | _____________^
9 | | "foo"; 10 | | "foo";
10 | | "baz"; 11 | | "baz";
11 | | }; 12 | | };
| |_____^ | |_____^
| |
= note: `-D unit-expr` implied by `-D warnings` = note: `-D unit-expr` implied by `-D warnings`
note: Consider removing the trailing semicolon 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