-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Detect Expressions that evaluate to Unit and warn the user #2016
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
clippy_lints/src/is_unit_expr.rs
Outdated
} | ||
} | ||
} | ||
fn is_unit_expr(expr: &Expr)->Option<Span>{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably rustfmt the file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looking good so far. Probably should add a test.
clippy_lints/src/is_unit_expr.rs
Outdated
}, | ||
ExprKind::If(_, ref then, ref else_)=>{ | ||
let check_then = check_last_stmt_in_block(then); | ||
if let Some(ref else_) = *else_{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is no else block then it's always going to evaluate to ()
clippy_lints/src/is_unit_expr.rs
Outdated
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_.deref()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you shouldn't need to call deref directly. &else_
should work here.
clippy_lints/src/is_unit_expr.rs
Outdated
return Some(expr.span.clone()); | ||
|
||
} else{ | ||
return Some(expr.span.clone()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This else block shouldn't be there I think? return None
?
clippy_lints/src/is_unit_expr.rs
Outdated
if let Some(ref expr_else) = check_else{ | ||
return Some(expr_else.clone()); | ||
}else{ | ||
return Some(expr.span.clone()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return None
?
clippy_lints/src/is_unit_expr.rs
Outdated
else{ | ||
return true; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: newline at EOF
tests/ui/is_unit_expr.rs
Outdated
@@ -0,0 +1,14 @@ | |||
|
|||
#![feature(plugin)] | |||
#[plugin(clippy)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs an exclamation mark. The tests didn't run correctly (see the output)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looking good, needs more tests for the call case and also tests for false positives.
clippy_lints/src/is_unit_expr.rs
Outdated
cx, | ||
UNIT_EXPR, | ||
expr.span, | ||
"This expression assigns the Unit type ()", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"assigns" probably isn't the right word to use here for a call
clippy_lints/src/is_unit_expr.rs
Outdated
cx, | ||
UNIT_EXPR, | ||
expr.span, | ||
"This expression assigns the Unit type ()", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
Remove "assigns" from the lint
Add false positive tests
Needs rustfmt on the tests, otherwise it's good to go. It occurs to me that we have chances for false positives on if blocks that diverge via panics too. Perhaps we should only lint if both the if and else are semicolon'd? |
or, well, at worst it will warn about a needless semicolon after a diverging statement. That's fine. |
retriggering travis |
Does "That's fine." mean it's fine for now and that it's planned to be fixed? Or it's by design? I like putting a semi-colon after my |
We can detect |
|
Ah cool. I did only hit the false positive with |
Oh continue is an easy fix. |
Fixed in #2022 |
I meant that it's fine forever.
|
Work in progress to resolve #2011
Feedback requested.