2017-09-02 18:25:33 +00:00
|
|
|
use rustc::lint::*;
|
|
|
|
use syntax::ast::*;
|
2017-09-03 03:36:24 +00:00
|
|
|
use syntax::ext::quote::rt::Span;
|
2017-12-01 00:38:29 +00:00
|
|
|
use utils::{span_lint, span_note_and_lint};
|
2017-09-02 18:25:33 +00:00
|
|
|
|
2017-09-03 04:33:26 +00:00
|
|
|
/// **What it does:** Checks for
|
2017-09-02 18:25:33 +00:00
|
|
|
/// - () being assigned to a variable
|
|
|
|
/// - () being passed to a function
|
|
|
|
///
|
2017-09-03 04:33:26 +00:00
|
|
|
/// **Why is this bad?** It is extremely unlikely that a user intended to
|
|
|
|
/// assign '()' to valiable. Instead,
|
|
|
|
/// Unit is what a block evaluates to when it returns nothing. This is
|
|
|
|
/// typically caused by a trailing
|
|
|
|
/// unintended semicolon.
|
2017-09-02 18:25:33 +00:00
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2017-09-03 04:33:26 +00:00
|
|
|
/// * `let x = {"foo" ;}` when the user almost certainly intended `let x
|
|
|
|
/// ={"foo"}`
|
2017-09-02 18:25:33 +00:00
|
|
|
declare_lint! {
|
|
|
|
pub UNIT_EXPR,
|
|
|
|
Warn,
|
|
|
|
"unintended assignment or use of a unit typed value"
|
|
|
|
}
|
|
|
|
|
2017-12-01 00:38:29 +00:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum UnitCause {
|
|
|
|
SemiColon,
|
|
|
|
EmptyBlock,
|
|
|
|
}
|
|
|
|
|
2017-09-02 18:25:33 +00:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct UnitExpr;
|
|
|
|
|
|
|
|
impl LintPass for UnitExpr {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNIT_EXPR)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for UnitExpr {
|
|
|
|
fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
|
2017-09-02 22:04:52 +00:00
|
|
|
if let ExprKind::Assign(ref _left, ref right) = expr.node {
|
2017-12-01 00:38:29 +00:00
|
|
|
check_for_unit(cx, right);
|
2017-09-02 18:25:33 +00:00
|
|
|
}
|
2017-09-02 22:04:52 +00:00
|
|
|
if let ExprKind::MethodCall(ref _left, ref args) = expr.node {
|
2017-09-03 20:39:49 +00:00
|
|
|
for arg in args {
|
2017-12-01 00:38:29 +00:00
|
|
|
check_for_unit(cx, arg);
|
2017-09-02 21:20:22 +00:00
|
|
|
}
|
2017-09-02 21:19:45 +00:00
|
|
|
}
|
2017-09-03 04:33:26 +00:00
|
|
|
if let ExprKind::Call(_, ref args) = expr.node {
|
2017-09-03 20:39:49 +00:00
|
|
|
for arg in args {
|
2017-12-01 00:38:29 +00:00
|
|
|
check_for_unit(cx, arg);
|
2017-09-03 04:33:26 +00:00
|
|
|
}
|
2017-09-02 21:20:22 +00:00
|
|
|
}
|
2017-09-02 18:25:33 +00:00
|
|
|
}
|
2017-09-02 19:20:43 +00:00
|
|
|
|
|
|
|
fn check_stmt(&mut self, cx: &EarlyContext, stmt: &Stmt) {
|
2017-09-03 04:33:26 +00:00
|
|
|
if let StmtKind::Local(ref local) = stmt.node {
|
|
|
|
if local.pat.node == PatKind::Wild {
|
|
|
|
return;
|
2017-09-02 21:09:41 +00:00
|
|
|
}
|
2017-09-03 04:33:26 +00:00
|
|
|
if let Some(ref expr) = local.init {
|
2017-12-01 00:38:29 +00:00
|
|
|
check_for_unit(cx, expr);
|
2017-09-03 04:33:26 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 19:20:43 +00:00
|
|
|
}
|
2017-09-02 18:25:33 +00:00
|
|
|
}
|
2017-09-05 09:25:20 +00:00
|
|
|
|
2017-12-01 00:38:29 +00:00
|
|
|
fn check_for_unit(cx: &EarlyContext, expr: &Expr) {
|
|
|
|
match is_unit_expr(expr) {
|
|
|
|
Some((span, UnitCause::SemiColon)) => span_note_and_lint(
|
|
|
|
cx,
|
|
|
|
UNIT_EXPR,
|
|
|
|
expr.span,
|
|
|
|
"This expression evaluates to the Unit type ()",
|
|
|
|
span,
|
|
|
|
"Consider removing the trailing semicolon",
|
|
|
|
),
|
|
|
|
Some((_span, UnitCause::EmptyBlock)) => span_lint(
|
|
|
|
cx,
|
|
|
|
UNIT_EXPR,
|
|
|
|
expr.span,
|
|
|
|
"This expression evaluates to the Unit type ()",
|
|
|
|
),
|
|
|
|
None => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_unit_expr(expr: &Expr) -> Option<(Span, UnitCause)> {
|
2017-09-03 04:33:26 +00:00
|
|
|
match expr.node {
|
2017-12-01 00:38:29 +00:00
|
|
|
ExprKind::Block(ref block) => match check_last_stmt_in_block(block) {
|
|
|
|
Some(UnitCause::SemiColon) =>
|
|
|
|
Some((block.stmts[block.stmts.len() - 1].span, UnitCause::SemiColon)),
|
|
|
|
Some(UnitCause::EmptyBlock) =>
|
|
|
|
Some((block.span, UnitCause::EmptyBlock)),
|
|
|
|
None => None
|
|
|
|
}
|
2017-09-03 04:33:26 +00:00
|
|
|
ExprKind::If(_, ref then, ref else_) => {
|
2017-09-02 22:04:52 +00:00
|
|
|
let check_then = check_last_stmt_in_block(then);
|
2017-09-03 04:33:26 +00:00
|
|
|
if let Some(ref else_) = *else_ {
|
2017-09-03 20:39:49 +00:00
|
|
|
let check_else = is_unit_expr(else_);
|
2017-09-03 04:33:26 +00:00
|
|
|
if let Some(ref expr_else) = check_else {
|
2017-09-03 20:39:49 +00:00
|
|
|
return Some(*expr_else);
|
2017-09-03 18:19:59 +00:00
|
|
|
}
|
2017-09-03 04:33:26 +00:00
|
|
|
}
|
2017-12-01 00:38:29 +00:00
|
|
|
match check_then {
|
|
|
|
Some(c) => Some((expr.span, c)),
|
|
|
|
None => None,
|
2017-09-05 09:33:04 +00:00
|
|
|
}
|
2017-09-03 03:36:24 +00:00
|
|
|
},
|
2017-09-03 04:33:26 +00:00
|
|
|
ExprKind::Match(ref _pattern, ref arms) => {
|
2017-09-03 20:39:49 +00:00
|
|
|
for arm in arms {
|
2017-12-01 00:38:29 +00:00
|
|
|
if let Some(r) = is_unit_expr(&arm.body) {
|
|
|
|
return Some(r);
|
2017-09-03 03:36:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-03 20:39:49 +00:00
|
|
|
None
|
2017-09-03 04:33:26 +00:00
|
|
|
},
|
2017-09-03 20:39:49 +00:00
|
|
|
_ => None,
|
2017-09-02 22:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 00:38:29 +00:00
|
|
|
fn check_last_stmt_in_block(block: &Block) -> Option<UnitCause> {
|
|
|
|
if block.stmts.is_empty() { return Some(UnitCause::EmptyBlock); }
|
2017-09-03 20:39:49 +00:00
|
|
|
let final_stmt = &block.stmts[block.stmts.len() - 1];
|
2017-09-03 18:19:59 +00:00
|
|
|
|
2017-09-03 21:01:29 +00:00
|
|
|
|
2017-09-03 21:15:15 +00:00
|
|
|
// Made a choice here to risk false positives on divergent macro invocations
|
|
|
|
// like `panic!()`
|
2017-09-03 18:19:59 +00:00
|
|
|
match final_stmt.node {
|
2017-12-01 00:38:29 +00:00
|
|
|
StmtKind::Expr(_) => None,
|
2017-11-04 19:55:56 +00:00
|
|
|
StmtKind::Semi(ref expr) => match expr.node {
|
2017-12-01 00:38:29 +00:00
|
|
|
ExprKind::Break(_, _) | ExprKind::Continue(_) | ExprKind::Ret(_) => None,
|
|
|
|
_ => Some(UnitCause::SemiColon),
|
2017-09-03 18:17:20 +00:00
|
|
|
},
|
2017-12-01 00:38:29 +00:00
|
|
|
_ => Some(UnitCause::SemiColon), // not sure what's happening here
|
2017-09-03 04:33:26 +00:00
|
|
|
}
|
|
|
|
}
|