2024-05-15 17:30:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-03-25 19:29:11 +01:00
|
|
|
use clippy_utils::source::snippet_block_with_applicability;
|
2024-05-15 17:30:11 +00:00
|
|
|
use clippy_utils::{higher, is_from_proc_macro};
|
2020-02-04 16:08:15 +01:00
|
|
|
use rustc_errors::Applicability;
|
2023-11-22 09:29:44 +08:00
|
|
|
use rustc_hir::{BlockCheckMode, Expr, ExprKind, MatchSource};
|
2020-01-12 15:08:41 +09:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
2020-03-30 11:02:14 +02:00
|
|
|
use rustc_middle::lint::in_external_macro;
|
2023-11-25 17:45:27 +00:00
|
|
|
use rustc_session::declare_lint_pass;
|
2015-11-20 00:22:52 -05:00
|
|
|
|
2018-03-28 15:24:26 +02:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### What it does
|
2024-02-15 11:05:25 +01:00
|
|
|
/// Checks for `if` and `match` conditions that use blocks containing an
|
2020-05-17 17:36:26 +02:00
|
|
|
/// expression, statements or conditions that use closures with blocks.
|
2019-03-05 11:50:33 -05:00
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Style, using blocks in the condition makes it hard to read.
|
2019-03-05 11:50:33 -05:00
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Examples
|
2023-10-23 13:49:18 +00:00
|
|
|
/// ```no_run
|
2022-06-04 13:34:07 +02:00
|
|
|
/// # fn somefunc() -> bool { true };
|
2019-03-05 17:23:50 -05:00
|
|
|
/// if { true } { /* ... */ }
|
2020-05-17 17:36:26 +02:00
|
|
|
///
|
2022-06-04 13:34:07 +02:00
|
|
|
/// if { let x = somefunc(); x } { /* ... */ }
|
2024-02-15 11:05:25 +01:00
|
|
|
///
|
2024-02-15 11:44:22 +01:00
|
|
|
/// match { let e = somefunc(); e } {
|
|
|
|
/// // ...
|
|
|
|
/// # _ => {}
|
|
|
|
/// }
|
2019-03-05 11:50:33 -05:00
|
|
|
/// ```
|
|
|
|
///
|
2022-06-04 13:34:07 +02:00
|
|
|
/// Use instead:
|
2023-10-23 13:49:18 +00:00
|
|
|
/// ```no_run
|
2020-05-17 17:36:26 +02:00
|
|
|
/// # fn somefunc() -> bool { true };
|
2022-06-04 13:34:07 +02:00
|
|
|
/// if true { /* ... */ }
|
2020-05-17 17:36:26 +02:00
|
|
|
///
|
|
|
|
/// let res = { let x = somefunc(); x };
|
|
|
|
/// if res { /* ... */ }
|
2024-02-15 11:05:25 +01:00
|
|
|
///
|
|
|
|
/// let res = { let e = somefunc(); e };
|
2024-02-15 11:44:22 +01:00
|
|
|
/// match res {
|
|
|
|
/// // ...
|
|
|
|
/// # _ => {}
|
|
|
|
/// }
|
2019-03-05 11:50:33 -05:00
|
|
|
/// ```
|
2021-12-06 12:33:31 +01:00
|
|
|
#[clippy::version = "1.45.0"]
|
2023-11-30 15:41:54 +08:00
|
|
|
pub BLOCKS_IN_CONDITIONS,
|
2018-03-28 15:24:26 +02:00
|
|
|
style,
|
2020-05-17 17:36:26 +02:00
|
|
|
"useless or complex blocks that can be eliminated in conditions"
|
2015-11-20 00:22:52 -05:00
|
|
|
}
|
|
|
|
|
2023-11-30 15:41:54 +08:00
|
|
|
declare_lint_pass!(BlocksInConditions => [BLOCKS_IN_CONDITIONS]);
|
2015-11-20 00:22:52 -05:00
|
|
|
|
2017-10-20 08:41:24 -04:00
|
|
|
const BRACED_EXPR_MESSAGE: &str = "omit braces around single expression condition";
|
2015-11-20 00:22:52 -05:00
|
|
|
|
2023-11-30 15:41:54 +08:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for BlocksInConditions {
|
2020-06-25 23:41:36 +03:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2019-08-27 09:43:03 +02:00
|
|
|
if in_external_macro(cx.sess(), expr.span) {
|
|
|
|
return;
|
|
|
|
}
|
2023-11-30 15:41:54 +08:00
|
|
|
|
|
|
|
let Some((cond, keyword, desc)) = higher::If::hir(expr)
|
|
|
|
.map(|hif| (hif.cond, "if", "an `if` condition"))
|
|
|
|
.or(if let ExprKind::Match(match_ex, _, MatchSource::Normal) = expr.kind {
|
|
|
|
Some((match_ex, "match", "a `match` scrutinee"))
|
2023-11-22 09:29:44 +08:00
|
|
|
} else {
|
|
|
|
None
|
2023-11-30 15:41:54 +08:00
|
|
|
})
|
|
|
|
else {
|
2023-11-22 09:29:44 +08:00
|
|
|
return;
|
|
|
|
};
|
2024-03-23 06:52:11 +01:00
|
|
|
let complex_block_message = format!(
|
2023-11-30 15:41:54 +08:00
|
|
|
"in {desc}, avoid complex blocks or closures with blocks; \
|
|
|
|
instead, move the block or closure higher and bind it with a `let`",
|
|
|
|
);
|
|
|
|
|
2023-11-22 09:29:44 +08:00
|
|
|
if let ExprKind::Block(block, _) = &cond.kind {
|
2024-01-19 01:06:08 +01:00
|
|
|
if !block.span.eq_ctxt(expr.span) {
|
|
|
|
// If the block comes from a macro, or as an argument to a macro,
|
|
|
|
// do not lint.
|
|
|
|
return;
|
|
|
|
}
|
2023-11-22 09:29:44 +08:00
|
|
|
if block.rules == BlockCheckMode::DefaultBlock {
|
|
|
|
if block.stmts.is_empty() {
|
|
|
|
if let Some(ex) = &block.expr {
|
|
|
|
// don't dig into the expression here, just suggest that they remove
|
|
|
|
// the block
|
|
|
|
if expr.span.from_expansion() || ex.span.from_expansion() {
|
2016-01-02 21:41:48 +05:30
|
|
|
return;
|
|
|
|
}
|
2020-02-04 16:08:15 +01:00
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
span_lint_and_sugg(
|
2017-08-09 09:30:56 +02:00
|
|
|
cx,
|
2023-11-30 15:41:54 +08:00
|
|
|
BLOCKS_IN_CONDITIONS,
|
2023-11-22 09:29:44 +08:00
|
|
|
cond.span,
|
|
|
|
BRACED_EXPR_MESSAGE,
|
2020-02-04 16:08:15 +01:00
|
|
|
"try",
|
2023-11-30 15:41:54 +08:00
|
|
|
snippet_block_with_applicability(cx, ex.span, "..", Some(expr.span), &mut applicability)
|
|
|
|
.to_string(),
|
2020-02-04 16:08:15 +01:00
|
|
|
applicability,
|
2017-08-09 09:30:56 +02:00
|
|
|
);
|
2015-11-20 00:22:52 -05:00
|
|
|
}
|
2023-11-22 09:29:44 +08:00
|
|
|
} else {
|
|
|
|
let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
|
2024-02-12 04:16:11 +08:00
|
|
|
if span.from_expansion() || expr.span.from_expansion() || is_from_proc_macro(cx, cond) {
|
2023-11-22 09:29:44 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// move block higher
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
2023-11-30 15:41:54 +08:00
|
|
|
BLOCKS_IN_CONDITIONS,
|
2023-11-22 09:29:44 +08:00
|
|
|
expr.span.with_hi(cond.span.hi()),
|
2023-11-30 15:41:54 +08:00
|
|
|
complex_block_message,
|
2023-11-22 09:29:44 +08:00
|
|
|
"try",
|
|
|
|
format!(
|
|
|
|
"let res = {}; {keyword} res",
|
|
|
|
snippet_block_with_applicability(cx, block.span, "..", Some(expr.span), &mut applicability),
|
|
|
|
),
|
|
|
|
applicability,
|
|
|
|
);
|
2015-11-20 00:22:52 -05:00
|
|
|
}
|
2023-11-22 09:29:44 +08:00
|
|
|
}
|
2015-11-20 00:22:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|