2017-09-02 18:25:33 +00:00
|
|
|
use rustc::lint::*;
|
|
|
|
use syntax::ast::*;
|
2017-09-02 22:04:52 +00:00
|
|
|
use std::ops::Deref;
|
2017-09-03 03:36:24 +00:00
|
|
|
use syntax::ext::quote::rt::Span;
|
|
|
|
|
2017-09-02 18:25:33 +00:00
|
|
|
|
|
|
|
/// **What it does:** Checks for
|
|
|
|
/// - () being assigned to a variable
|
|
|
|
/// - () being passed to a function
|
|
|
|
///
|
|
|
|
/// **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.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// * `let x = {"foo" ;}` when the user almost certainly intended `let x ={"foo"}`
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub UNIT_EXPR,
|
|
|
|
Warn,
|
|
|
|
"unintended assignment or use of a unit typed value"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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-09-03 03:36:24 +00:00
|
|
|
if let Some(span) = is_unit_expr(right){
|
|
|
|
cx.span_lint(UNIT_EXPR, span, "Consider removing the trailing semicolon");
|
2017-09-02 21:09:41 +00:00
|
|
|
}
|
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-02 21:19:45 +00:00
|
|
|
for ref arg in args{
|
2017-09-03 03:36:24 +00:00
|
|
|
if let Some(span) = is_unit_expr(arg){
|
|
|
|
cx.span_lint(UNIT_EXPR, span, "Consider removing the trailing semicolon");
|
2017-09-02 21:20:22 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 21:19:45 +00:00
|
|
|
}
|
|
|
|
if let ExprKind::Call( _, ref args) = expr.node{
|
|
|
|
for ref arg in args{
|
2017-09-03 03:36:24 +00:00
|
|
|
if let Some(span) = is_unit_expr(arg){
|
|
|
|
cx.span_lint(UNIT_EXPR, span, "Consider removing the trailing semicolon");
|
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-02 21:09:41 +00:00
|
|
|
if let StmtKind::Local(ref local) = stmt.node{
|
|
|
|
if local.pat.node == PatKind::Wild {return;}
|
|
|
|
if let Some(ref expr) = local.init{
|
2017-09-03 03:36:24 +00:00
|
|
|
if let Some(span) = is_unit_expr(expr){
|
|
|
|
cx.span_lint(UNIT_EXPR, span, "Consider removing the trailing semicolon");
|
2017-09-02 21:09:41 +00:00
|
|
|
}
|
2017-09-02 19:20:43 +00:00
|
|
|
}
|
2017-09-02 18:25:33 +00:00
|
|
|
}
|
2017-09-03 03:36:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn is_unit_expr(expr: &Expr)->Option<Span>{
|
2017-09-02 21:09:41 +00:00
|
|
|
match expr.node{
|
2017-09-02 22:04:52 +00:00
|
|
|
ExprKind::Block(ref block) => {
|
2017-09-03 03:36:24 +00:00
|
|
|
if check_last_stmt_in_block(block){
|
|
|
|
return Some(block.stmts[block.stmts.len()-1].span.clone());
|
|
|
|
} else{
|
|
|
|
return None;
|
|
|
|
}
|
2017-09-02 22:04:52 +00:00
|
|
|
},
|
|
|
|
ExprKind::If(_, ref then, ref else_)=>{
|
|
|
|
let check_then = check_last_stmt_in_block(then);
|
|
|
|
if let Some(ref else_) = *else_{
|
2017-09-03 03:45:40 +00:00
|
|
|
let check_else = is_unit_expr(else_.deref());
|
2017-09-03 03:36:24 +00:00
|
|
|
if let Some(ref expr_else) = check_else{
|
|
|
|
return Some(expr_else.clone());
|
|
|
|
}else{
|
|
|
|
return Some(expr.span.clone());
|
|
|
|
}
|
2017-09-02 22:04:52 +00:00
|
|
|
}
|
2017-09-03 03:36:24 +00:00
|
|
|
if check_then {
|
|
|
|
return Some(expr.span.clone());
|
|
|
|
|
|
|
|
} else{
|
|
|
|
return Some(expr.span.clone());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ExprKind::Match(ref _pattern, ref arms ) =>{
|
|
|
|
for ref arm in arms{
|
|
|
|
if let Some(expr) = is_unit_expr(&arm.body){
|
|
|
|
return Some(expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return None;
|
2017-09-02 22:04:52 +00:00
|
|
|
}
|
2017-09-03 03:36:24 +00:00
|
|
|
_ => return None,
|
2017-09-02 22:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_last_stmt_in_block(block: &Block)->bool{
|
|
|
|
let ref final_stmt = &block.stmts[block.stmts.len()-1];
|
|
|
|
if let StmtKind::Expr(_) = final_stmt.node{
|
2017-09-02 21:09:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else{
|
2017-09-03 03:36:24 +00:00
|
|
|
return true;
|
2017-09-02 21:09:41 +00:00
|
|
|
}
|
|
|
|
}
|