Add linting for empty blocks too

This commit is contained in:
HMPerson1
2017-11-30 19:38:29 -05:00
parent 5fca6eb89e
commit e2bc383383
2 changed files with 53 additions and 60 deletions

View File

@@ -1,7 +1,7 @@
use rustc::lint::*; use rustc::lint::*;
use syntax::ast::*; use syntax::ast::*;
use syntax::ext::quote::rt::Span; use syntax::ext::quote::rt::Span;
use utils::span_note_and_lint; use utils::{span_lint, span_note_and_lint};
/// **What it does:** Checks for /// **What it does:** Checks for
/// - () being assigned to a variable /// - () being assigned to a variable
@@ -24,6 +24,12 @@ declare_lint! {
"unintended assignment or use of a unit typed value" "unintended assignment or use of a unit typed value"
} }
#[derive(Copy, Clone)]
enum UnitCause {
SemiColon,
EmptyBlock,
}
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct UnitExpr; pub struct UnitExpr;
@@ -36,43 +42,16 @@ impl LintPass for UnitExpr {
impl EarlyLintPass for UnitExpr { impl EarlyLintPass for UnitExpr {
fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) { fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
if let ExprKind::Assign(ref _left, ref right) = expr.node { if let ExprKind::Assign(ref _left, ref right) = expr.node {
if let Some(span) = is_unit_expr(right) { check_for_unit(cx, right);
span_note_and_lint(
cx,
UNIT_EXPR,
expr.span,
"This expression evaluates to the Unit type ()",
span,
"Consider removing the trailing semicolon",
);
}
} }
if let ExprKind::MethodCall(ref _left, ref args) = expr.node { if let ExprKind::MethodCall(ref _left, ref args) = expr.node {
for arg in args { for arg in args {
if let Some(span) = is_unit_expr(arg) { check_for_unit(cx, arg);
span_note_and_lint(
cx,
UNIT_EXPR,
expr.span,
"This expression evaluates to the Unit type ()",
span,
"Consider removing the trailing semicolon",
);
}
} }
} }
if let ExprKind::Call(_, ref args) = expr.node { if let ExprKind::Call(_, ref args) = expr.node {
for arg in args { for arg in args {
if let Some(span) = is_unit_expr(arg) { check_for_unit(cx, arg);
span_note_and_lint(
cx,
UNIT_EXPR,
expr.span,
"This expression evaluates to the Unit type ()",
span,
"Consider removing the trailing semicolon",
);
}
} }
} }
} }
@@ -83,28 +62,41 @@ impl EarlyLintPass for UnitExpr {
return; return;
} }
if let Some(ref expr) = local.init { if let Some(ref expr) = local.init {
if let Some(span) = is_unit_expr(expr) { check_for_unit(cx, expr);
span_note_and_lint(
cx,
UNIT_EXPR,
expr.span,
"This expression evaluates to the Unit type ()",
span,
"Consider removing the trailing semicolon",
);
}
} }
} }
} }
} }
fn is_unit_expr(expr: &Expr) -> Option<Span> { 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)> {
match expr.node { match expr.node {
ExprKind::Block(ref block) => if check_last_stmt_in_block(block) { ExprKind::Block(ref block) => match check_last_stmt_in_block(block) {
Some(block.stmts[block.stmts.len() - 1].span) Some(UnitCause::SemiColon) =>
} else { Some((block.stmts[block.stmts.len() - 1].span, UnitCause::SemiColon)),
None Some(UnitCause::EmptyBlock) =>
}, Some((block.span, UnitCause::EmptyBlock)),
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_ {
@@ -113,16 +105,15 @@ fn is_unit_expr(expr: &Expr) -> Option<Span> {
return Some(*expr_else); return Some(*expr_else);
} }
} }
if check_then { match check_then {
Some(expr.span) Some(c) => Some((expr.span, c)),
} else { None => None,
None
} }
}, },
ExprKind::Match(ref _pattern, ref arms) => { ExprKind::Match(ref _pattern, ref arms) => {
for arm in arms { for arm in arms {
if let Some(expr) = is_unit_expr(&arm.body) { if let Some(r) = is_unit_expr(&arm.body) {
return Some(expr); return Some(r);
} }
} }
None None
@@ -131,19 +122,19 @@ fn is_unit_expr(expr: &Expr) -> Option<Span> {
} }
} }
fn check_last_stmt_in_block(block: &Block) -> bool { fn check_last_stmt_in_block(block: &Block) -> Option<UnitCause> {
if block.stmts.is_empty() { return false; } if block.stmts.is_empty() { return Some(UnitCause::EmptyBlock); }
let final_stmt = &block.stmts[block.stmts.len() - 1]; let final_stmt = &block.stmts[block.stmts.len() - 1];
// Made a choice here to risk false positives on divergent macro invocations // Made a choice here to risk false positives on divergent macro invocations
// like `panic!()` // like `panic!()`
match final_stmt.node { match final_stmt.node {
StmtKind::Expr(_) => false, StmtKind::Expr(_) => None,
StmtKind::Semi(ref expr) => match expr.node { StmtKind::Semi(ref expr) => match expr.node {
ExprKind::Break(_, _) | ExprKind::Continue(_) | ExprKind::Ret(_) => false, ExprKind::Break(_, _) | ExprKind::Continue(_) | ExprKind::Ret(_) => None,
_ => true, _ => Some(UnitCause::SemiColon),
}, },
_ => true, _ => Some(UnitCause::SemiColon), // not sure what's happening here
} }
} }

View File

@@ -73,5 +73,7 @@ pub fn foo() -> i32 {
} }
pub fn issue_2160() { pub fn issue_2160() {
let x = {}; let x1 = {};
let x2 = if true {} else {};
let x3 = match None { Some(_) => {}, None => {}, };
} }