2015-05-29 09:07:34 -05:00
|
|
|
//! 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::*;
|
2015-09-03 09:42:17 -05:00
|
|
|
use rustc_front::hir::*;
|
2015-09-06 03:53:55 -05:00
|
|
|
use syntax::codemap::Spanned;
|
2015-08-16 01:54:43 -05:00
|
|
|
|
2015-08-12 06:14:14 -05:00
|
|
|
use utils::{in_macro, span_help_and_lint, snippet, snippet_block};
|
2015-05-29 09:07:34 -05:00
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub COLLAPSIBLE_IF,
|
|
|
|
Warn,
|
2015-08-13 03:32:35 -05:00
|
|
|
"two nested `if`-expressions can be collapsed into one, e.g. `if x { if y { foo() } }` \
|
|
|
|
can be written as `if x && y { foo() }`"
|
2015-05-29 09:07:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy,Clone)]
|
|
|
|
pub struct CollapsibleIf;
|
|
|
|
|
|
|
|
impl LintPass for CollapsibleIf {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(COLLAPSIBLE_IF)
|
|
|
|
}
|
2015-09-18 21:53:04 -05:00
|
|
|
}
|
2015-08-11 13:22:20 -05:00
|
|
|
|
2015-09-18 21:53:04 -05:00
|
|
|
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)
|
|
|
|
}
|
2015-08-11 13:22:20 -05:00
|
|
|
}
|
2015-06-01 08:09:17 -05:00
|
|
|
}
|
|
|
|
|
2015-09-18 21:53:04 -05:00
|
|
|
fn check_if(cx: &LateContext, e: &Expr) {
|
2015-08-11 13:22:20 -05:00
|
|
|
if let ExprIf(ref check, ref then, None) = e.node {
|
2015-08-13 09:41:51 -05:00
|
|
|
if let Some(&Expr{ node: ExprIf(ref check_inner, ref content, None), span: sp, ..}) =
|
2015-08-11 13:22:20 -05:00
|
|
|
single_stmt_of_block(then) {
|
2015-08-13 09:41:51 -05:00
|
|
|
if e.span.expn_id != sp.expn_id {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-12 04:00:08 -05:00
|
|
|
span_help_and_lint(cx, COLLAPSIBLE_IF, e.span,
|
|
|
|
"this if statement can be collapsed",
|
|
|
|
&format!("try\nif {} && {} {}",
|
2015-08-12 04:49:57 -05:00
|
|
|
check_to_string(cx, check), check_to_string(cx, check_inner),
|
2015-08-12 06:14:14 -05:00
|
|
|
snippet_block(cx, content.span, "..")));
|
2015-08-11 13:22:20 -05:00
|
|
|
}
|
|
|
|
}
|
2015-05-29 09:07:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn requires_brackets(e: &Expr) -> bool {
|
|
|
|
match e.node {
|
|
|
|
ExprBinary(Spanned {node: n, ..}, _, _) if n == BiEq => false,
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 21:53:04 -05:00
|
|
|
fn check_to_string(cx: &LateContext, e: &Expr) -> String {
|
2015-05-29 09:07:34 -05:00
|
|
|
if requires_brackets(e) {
|
2015-08-12 04:49:57 -05:00
|
|
|
format!("({})", snippet(cx, e.span, ".."))
|
2015-05-29 09:07:34 -05:00
|
|
|
} else {
|
2015-08-12 04:49:57 -05:00
|
|
|
format!("{}", snippet(cx, e.span, ".."))
|
2015-05-29 09:07:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 08:09:17 -05:00
|
|
|
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)
|
|
|
|
} else { None }
|
|
|
|
} else {
|
|
|
|
if block.stmts.is_empty() {
|
2015-08-25 07:41:35 -05:00
|
|
|
if let Some(ref p) = block.expr { Some(p) } else { None }
|
2015-06-01 08:09:17 -05:00
|
|
|
} else { None }
|
2015-05-29 09:07:34 -05:00
|
|
|
}
|
2015-06-01 08:09:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn single_stmt_of_expr(expr: &Expr) -> Option<&Expr> {
|
|
|
|
if let ExprBlock(ref block) = expr.node {
|
|
|
|
single_stmt_of_block(block)
|
|
|
|
} else { Some(expr) }
|
2015-05-29 09:07:34 -05:00
|
|
|
}
|