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