1 use crate::consts::{constant, Constant};
2 use crate::rustc::hir::{Expr, ExprKind};
3 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use crate::rustc::{declare_tool_lint, lint_array};
5 use crate::syntax::ast::LitKind;
6 use crate::utils::{is_direct_expn_of, span_help_and_lint};
7 use if_chain::if_chain;
9 /// **What it does:** Check to call assert!(true/false)
11 /// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a
12 /// panic!() or unreachable!()
14 /// **Known problems:** None
22 /// const B: bool = false;
25 declare_clippy_lint! {
26 pub ASSERTIONS_ON_CONSTANTS,
28 "assert!(true/false) will be optimized out by the compiler/should probably be replaced by a panic!() or unreachable!()"
31 pub struct AssertionsOnConstants;
33 impl LintPass for AssertionsOnConstants {
34 fn get_lints(&self) -> LintArray {
35 lint_array![ASSERTIONS_ON_CONSTANTS]
38 fn name(&self) -> &'static str {
39 "AssertionsOnConstants"
43 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants {
44 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
46 if is_direct_expn_of(e.span, "assert").is_some();
47 if let ExprKind::Unary(_, ref lit) = e.node;
49 if let ExprKind::Lit(ref inner) = lit.node {
51 LitKind::Bool(true) => {
52 span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
53 "assert!(true) will be optimized out by the compiler",
56 LitKind::Bool(false) => {
58 cx, ASSERTIONS_ON_CONSTANTS, e.span,
59 "assert!(false) should probably be replaced",
60 "use panic!() or unreachable!()");
64 } else if let Some(bool_const) = constant(cx, cx.tables, lit) {
66 Constant::Bool(true) => {
67 span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
68 "assert!(const: true) will be optimized out by the compiler",
71 Constant::Bool(false) => {
72 span_help_and_lint(cx, ASSERTIONS_ON_CONSTANTS, e.span,
73 "assert!(const: false) should probably be replaced",
74 "use panic!() or unreachable!()");