2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2020-12-20 16:19:49 +00:00
|
|
|
use rustc_ast::ast::{Block, Expr, ExprKind, Stmt, StmtKind};
|
|
|
|
use rustc_ast::visit::{walk_expr, Visitor};
|
2021-12-04 15:09:15 +00:00
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
|
2020-12-20 16:19:49 +00:00
|
|
|
use rustc_middle::lint::in_external_macro;
|
2023-12-01 17:21:58 +00:00
|
|
|
use rustc_session::declare_lint_pass;
|
2020-12-20 16:19:49 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for `else` blocks that can be removed without changing semantics.
|
2020-12-20 16:19:49 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// The `else` block adds unnecessary indentation and verbosity.
|
2020-12-20 16:19:49 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Known problems
|
|
|
|
/// Some may prefer to keep the `else` block for clarity.
|
2020-12-20 16:19:49 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2023-11-02 16:35:56 +00:00
|
|
|
/// ```no_run
|
2020-12-20 16:19:49 +00:00
|
|
|
/// fn my_func(count: u32) {
|
|
|
|
/// if count == 0 {
|
|
|
|
/// print!("Nothing to do");
|
|
|
|
/// return;
|
|
|
|
/// } else {
|
|
|
|
/// print!("Moving on...");
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
2023-11-02 16:35:56 +00:00
|
|
|
/// ```no_run
|
2020-12-20 16:19:49 +00:00
|
|
|
/// fn my_func(count: u32) {
|
|
|
|
/// if count == 0 {
|
|
|
|
/// print!("Nothing to do");
|
|
|
|
/// return;
|
|
|
|
/// }
|
|
|
|
/// print!("Moving on...");
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "1.50.0"]
|
2020-12-20 16:19:49 +00:00
|
|
|
pub REDUNDANT_ELSE,
|
|
|
|
pedantic,
|
|
|
|
"`else` branch that can be removed without changing semantics"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(RedundantElse => [REDUNDANT_ELSE]);
|
|
|
|
|
|
|
|
impl EarlyLintPass for RedundantElse {
|
|
|
|
fn check_stmt(&mut self, cx: &EarlyContext<'_>, stmt: &Stmt) {
|
2021-12-04 15:09:15 +00:00
|
|
|
if in_external_macro(cx.sess(), stmt.span) {
|
2020-12-20 16:19:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Only look at expressions that are a whole statement
|
|
|
|
let expr: &Expr = match &stmt.kind {
|
|
|
|
StmtKind::Expr(expr) | StmtKind::Semi(expr) => expr,
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
// if else
|
|
|
|
let (mut then, mut els): (&Block, &Expr) = match &expr.kind {
|
|
|
|
ExprKind::If(_, then, Some(els)) => (then, els),
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
loop {
|
|
|
|
if !BreakVisitor::default().check_block(then) {
|
|
|
|
// then block does not always break
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
match &els.kind {
|
|
|
|
// else if else
|
|
|
|
ExprKind::If(_, next_then, Some(next_els)) => {
|
|
|
|
then = next_then;
|
|
|
|
els = next_els;
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
// else if without else
|
|
|
|
ExprKind::If(..) => return,
|
|
|
|
// done
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
REDUNDANT_ELSE,
|
|
|
|
els.span,
|
|
|
|
"redundant else block",
|
|
|
|
None,
|
|
|
|
"remove the `else` block and move the contents out",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Call `check` functions to check if an expression always breaks control flow
|
|
|
|
#[derive(Default)]
|
|
|
|
struct BreakVisitor {
|
|
|
|
is_break: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ast> Visitor<'ast> for BreakVisitor {
|
|
|
|
fn visit_block(&mut self, block: &'ast Block) {
|
|
|
|
self.is_break = match block.stmts.as_slice() {
|
|
|
|
[.., last] => self.check_stmt(last),
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_expr(&mut self, expr: &'ast Expr) {
|
|
|
|
self.is_break = match expr.kind {
|
|
|
|
ExprKind::Break(..) | ExprKind::Continue(..) | ExprKind::Ret(..) => true,
|
2024-02-17 17:43:54 +00:00
|
|
|
ExprKind::Match(_, ref arms, _) => arms.iter().all(|arm|
|
2023-11-27 02:15:56 +00:00
|
|
|
arm.body.is_none() || arm.body.as_deref().is_some_and(|body| self.check_expr(body))
|
|
|
|
),
|
2020-12-20 16:19:49 +00:00
|
|
|
ExprKind::If(_, ref then, Some(ref els)) => self.check_block(then) && self.check_expr(els),
|
|
|
|
ExprKind::If(_, _, None)
|
|
|
|
// ignore loops for simplicity
|
2023-12-08 22:51:50 +00:00
|
|
|
| ExprKind::While(..) | ExprKind::ForLoop { .. } | ExprKind::Loop(..) => false,
|
2020-12-20 16:19:49 +00:00
|
|
|
_ => {
|
|
|
|
walk_expr(self, expr);
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BreakVisitor {
|
|
|
|
fn check<T>(&mut self, item: T, visit: fn(&mut Self, T)) -> bool {
|
|
|
|
visit(self, item);
|
|
|
|
std::mem::replace(&mut self.is_break, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_block(&mut self, block: &Block) -> bool {
|
|
|
|
self.check(block, Self::visit_block)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, expr: &Expr) -> bool {
|
|
|
|
self.check(expr, Self::visit_expr)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_stmt(&mut self, stmt: &Stmt) -> bool {
|
|
|
|
self.check(stmt, Self::visit_stmt)
|
|
|
|
}
|
|
|
|
}
|