2023-12-28 12:33:07 -06:00
|
|
|
use clippy_utils::consts::{constant_with_source, Constant, ConstantSource};
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2022-01-13 06:18:19 -06:00
|
|
|
use clippy_utils::macros::{find_assert_args, root_macro_call_first_node, PanicExpn};
|
2023-12-28 12:33:07 -06:00
|
|
|
use rustc_hir::{Expr, Item, ItemKind, Node};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2023-12-01 11:21:58 -06:00
|
|
|
use rustc_session::declare_lint_pass;
|
2022-01-13 06:18:19 -06:00
|
|
|
use rustc_span::sym;
|
2018-12-25 16:29:03 -06:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for `assert!(true)` and `assert!(false)` calls.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Will be optimized out by the compiler or should probably be replaced by a
|
2020-03-17 20:50:39 -05:00
|
|
|
/// `panic!()` or `unreachable!()`
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-10 17:01:56 -05:00
|
|
|
/// ```rust,ignore
|
2019-01-30 19:15:29 -06:00
|
|
|
/// assert!(false)
|
|
|
|
/// assert!(true)
|
2019-03-05 10:50:33 -06:00
|
|
|
/// const B: bool = false;
|
2019-01-30 19:15:29 -06:00
|
|
|
/// assert!(B)
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.34.0"]
|
2019-01-09 04:38:38 -06:00
|
|
|
pub ASSERTIONS_ON_CONSTANTS,
|
|
|
|
style,
|
2019-03-10 12:19:47 -05:00
|
|
|
"`assert!(true)` / `assert!(false)` will be optimized out by the compiler, and should probably be replaced by a `panic!()` or `unreachable!()`"
|
2018-12-25 16:29:03 -06:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
|
2018-12-25 16:29:03 -06:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for AssertionsOnConstants {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
|
2023-07-17 03:19:29 -05:00
|
|
|
let Some(macro_call) = root_macro_call_first_node(cx, e) else {
|
|
|
|
return;
|
|
|
|
};
|
2022-01-13 06:18:19 -06:00
|
|
|
let is_debug = match cx.tcx.get_diagnostic_name(macro_call.def_id) {
|
|
|
|
Some(sym::debug_assert_macro) => true,
|
|
|
|
Some(sym::assert_macro) => false,
|
|
|
|
_ => return,
|
2019-10-07 15:08:00 -05:00
|
|
|
};
|
2023-07-17 03:19:29 -05:00
|
|
|
let Some((condition, panic_expn)) = find_assert_args(cx, e, macro_call.expn) else {
|
|
|
|
return;
|
|
|
|
};
|
2023-12-28 12:33:07 -06:00
|
|
|
let Some((Constant::Bool(val), source)) = constant_with_source(cx, cx.typeck_results(), condition) else {
|
2023-07-17 03:19:29 -05:00
|
|
|
return;
|
|
|
|
};
|
2023-12-28 12:33:07 -06:00
|
|
|
if let ConstantSource::Constant = source
|
|
|
|
&& let Some(node) = cx.tcx.hir().find_parent(e.hir_id)
|
|
|
|
&& let Node::Item(Item {
|
|
|
|
kind: ItemKind::Const(..),
|
|
|
|
..
|
|
|
|
}) = node
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-01-13 06:18:19 -06:00
|
|
|
if val {
|
2020-01-26 19:56:22 -06:00
|
|
|
span_lint_and_help(
|
2019-10-07 15:08:00 -05:00
|
|
|
cx,
|
|
|
|
ASSERTIONS_ON_CONSTANTS,
|
2022-01-13 06:18:19 -06:00
|
|
|
macro_call.span,
|
|
|
|
&format!(
|
|
|
|
"`{}!(true)` will be optimized out by the compiler",
|
|
|
|
cx.tcx.item_name(macro_call.def_id)
|
|
|
|
),
|
2020-04-18 05:28:29 -05:00
|
|
|
None,
|
2022-01-13 06:18:19 -06:00
|
|
|
"remove it",
|
2019-10-07 15:08:00 -05:00
|
|
|
);
|
2022-01-13 06:18:19 -06:00
|
|
|
} else if !is_debug {
|
|
|
|
let (assert_arg, panic_arg) = match panic_expn {
|
|
|
|
PanicExpn::Empty => ("", ""),
|
|
|
|
_ => (", ..", ".."),
|
|
|
|
};
|
2020-01-26 19:56:22 -06:00
|
|
|
span_lint_and_help(
|
2019-10-07 15:08:00 -05:00
|
|
|
cx,
|
|
|
|
ASSERTIONS_ON_CONSTANTS,
|
2022-01-13 06:18:19 -06:00
|
|
|
macro_call.span,
|
2022-10-06 02:44:38 -05:00
|
|
|
&format!("`assert!(false{assert_arg})` should probably be replaced"),
|
2020-04-18 05:28:29 -05:00
|
|
|
None,
|
2022-10-06 02:44:38 -05:00
|
|
|
&format!("use `panic!({panic_arg})` or `unreachable!({panic_arg})`"),
|
2021-06-03 01:41:37 -05:00
|
|
|
);
|
2018-12-25 16:29:03 -06:00
|
|
|
}
|
|
|
|
}
|
2019-10-06 13:10:30 -05:00
|
|
|
}
|