rust/src/collapsible_if.rs

133 lines
4.1 KiB
Rust
Raw Normal View History

//! Checks for if expressions that contain only an if expression.
//!
//! For example, the lint would catch:
//!
//! ```
//! if x {
//! if y {
//! println!("Hello world");
//! }
//! }
//! ```
//!
//! This lint is **warn** by default
use rustc::lint::*;
use rustc_front::hir::*;
2015-09-06 03:53:55 -05:00
use syntax::codemap::Spanned;
use utils::{in_macro, snippet, snippet_block, span_lint_and_then};
/// **What it does:** This lint checks for nested `if`-statements which can be collapsed by
/// `&&`-combining their conditions and for `else { if .. }` expressions that can be collapsed to
/// `else if ..`. It is `Warn` by default.
///
/// **Why is this bad?** Each `if`-statement adds one level of nesting, which makes code look more complex than it really is.
///
/// **Known problems:** None
///
/// **Example:** `if x { if y { .. } }`
declare_lint! {
pub COLLAPSIBLE_IF,
Warn,
"two nested `if`-expressions can be collapsed into one, e.g. `if x { if y { foo() } }` \
can be written as `if x && y { foo() }` and an `else { if .. } expression can be collapsed to \
`else if`"
}
#[derive(Copy,Clone)]
pub struct CollapsibleIf;
impl LintPass for CollapsibleIf {
fn get_lints(&self) -> LintArray {
lint_array!(COLLAPSIBLE_IF)
}
}
impl LateLintPass for CollapsibleIf {
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
2015-09-17 00:27:18 -05:00
if !in_macro(cx, expr.span) {
check_if(cx, expr)
}
}
}
fn check_if(cx: &LateContext, e: &Expr) {
if let ExprIf(ref check, ref then, ref else_) = e.node {
2016-02-01 04:28:39 -06:00
if let Some(ref else_) = *else_ {
if_let_chain! {[
let ExprBlock(ref block) = else_.node,
block.stmts.is_empty(),
block.rules == BlockCheckMode::DefaultBlock,
let Some(ref else_) = block.expr,
let ExprIf(_, _, _) = else_.node
], {
span_lint_and_then(cx,
COLLAPSIBLE_IF,
block.span,
"this `else { if .. }` block can be collapsed", |db| {
db.span_suggestion(block.span, "try",
format!("else {}",
snippet_block(cx, else_.span, "..")));
});
}}
} else if let Some(&Expr{ node: ExprIf(ref check_inner, ref content, None), span: sp, ..}) =
single_stmt_of_block(then) {
2016-02-01 04:28:39 -06:00
if e.span.expn_id != sp.expn_id {
return;
}
2016-02-01 04:28:39 -06:00
span_lint_and_then(cx,
COLLAPSIBLE_IF,
e.span,
"this if statement can be collapsed", |db| {
db.span_suggestion(e.span, "try",
format!("if {} && {} {}",
check_to_string(cx, check),
check_to_string(cx, check_inner),
snippet_block(cx, content.span, "..")));
});
2016-01-03 22:26:12 -06:00
}
}
}
fn requires_brackets(e: &Expr) -> bool {
match e.node {
ExprBinary(Spanned {node: n, ..}, _, _) if n == BiEq => false,
2016-01-03 22:26:12 -06:00
_ => true,
}
}
fn check_to_string(cx: &LateContext, e: &Expr) -> String {
if requires_brackets(e) {
2015-08-12 04:49:57 -05:00
format!("({})", snippet(cx, e.span, ".."))
} else {
2015-08-12 04:49:57 -05:00
format!("{}", snippet(cx, e.span, ".."))
}
}
fn single_stmt_of_block(block: &Block) -> Option<&Expr> {
if block.stmts.len() == 1 && block.expr.is_none() {
if let StmtExpr(ref expr, _) = block.stmts[0].node {
single_stmt_of_expr(expr)
2016-01-03 22:26:12 -06:00
} else {
None
}
} else if block.stmts.is_empty() {
if let Some(ref p) = block.expr {
Some(p)
2016-01-03 22:26:12 -06:00
} else {
None
}
} else {
None
}
}
fn single_stmt_of_expr(expr: &Expr) -> Option<&Expr> {
if let ExprBlock(ref block) = expr.node {
single_stmt_of_block(block)
2016-01-03 22:26:12 -06:00
} else {
Some(expr)
}
}