X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Fassertions_on_constants.rs;h=16905781c56b6851b5ffcce9c89b1ccbdc381f0b;hb=b094bb1bd7f1cc702823c91ca509f338fedee24a;hp=22dbd3ecc9c13f4ac1a96bbb9dd2e216bb28fd57;hpb=439284b58400be98036117d053815eb24472fee7;p=rust.git diff --git a/clippy_lints/src/assertions_on_constants.rs b/clippy_lints/src/assertions_on_constants.rs index 22dbd3ecc9c..16905781c56 100644 --- a/clippy_lints/src/assertions_on_constants.rs +++ b/clippy_lints/src/assertions_on_constants.rs @@ -1,26 +1,24 @@ -use if_chain::if_chain; -use rustc::hir::{Expr, ExprKind}; -use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; -use rustc::{declare_lint_pass, declare_tool_lint}; -use syntax_pos::Span; - use crate::consts::{constant, Constant}; -use crate::utils::{in_macro_or_desugar, is_direct_expn_of, span_help_and_lint}; +use clippy_utils::diagnostics::span_lint_and_help; +use clippy_utils::source::snippet_opt; +use clippy_utils::{is_direct_expn_of, is_expn_of, match_panic_call}; +use if_chain::if_chain; +use rustc_hir::{Expr, ExprKind, UnOp}; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_session::{declare_lint_pass, declare_tool_lint}; declare_clippy_lint! { /// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls. /// /// **Why is this bad?** Will be optimized out by the compiler or should probably be replaced by a - /// panic!() or unreachable!() + /// `panic!()` or `unreachable!()` /// /// **Known problems:** None /// /// **Example:** /// ```rust,ignore /// assert!(false) - /// // or /// assert!(true) - /// // or /// const B: bool = false; /// assert!(B) /// ``` @@ -31,42 +29,118 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]); -impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { - fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) { - let mut is_debug_assert = false; - if let Some(assert_span) = is_direct_expn_of(e.span, "assert") { - if in_macro_or_desugar(assert_span) { +impl<'tcx> LateLintPass<'tcx> for AssertionsOnConstants { + fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) { + let lint_true = |is_debug: bool| { + span_lint_and_help( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + if is_debug { + "`debug_assert!(true)` will be optimized out by the compiler" + } else { + "`assert!(true)` will be optimized out by the compiler" + }, + None, + "remove it", + ); + }; + let lint_false_without_message = || { + span_lint_and_help( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + "`assert!(false)` should probably be replaced", + None, + "use `panic!()` or `unreachable!()`", + ); + }; + let lint_false_with_message = |panic_message: String| { + span_lint_and_help( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + &format!("`assert!(false, {})` should probably be replaced", panic_message), + None, + &format!("use `panic!({})` or `unreachable!({})`", panic_message, panic_message), + ) + }; + + if let Some(debug_assert_span) = is_expn_of(e.span, "debug_assert") { + if debug_assert_span.from_expansion() { return; } - if let Some(debug_assert_span) = is_direct_expn_of(assert_span, "debug_assert") { - if in_macro_or_desugar(debug_assert_span) { - return; + if_chain! { + if let ExprKind::Unary(_, ref lit) = e.kind; + if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.typeck_results(), lit); + if is_true; + then { + lint_true(true); } - is_debug_assert = true; + }; + } else if let Some(assert_span) = is_direct_expn_of(e.span, "assert") { + if assert_span.from_expansion() { + return; } - if let ExprKind::Unary(_, ref lit) = e.node { - if let Some((bool_const, _)) = constant(cx, cx.tables, lit) { - if let Constant::Bool(is_true) bool_const { - if is_true { - span_help_and_lint( - cx, - ASSERTIONS_ON_CONSTANTS, - e.span, - "`assert!(true)` will be optimized out by the compiler", - "remove it" - ); - } else if !is_debug_assert { - span_help_and_lint( - cx, - ASSERTIONS_ON_CONSTANTS, - e.span, - "`assert!(false)` should probably be replaced", - "use `panic!()` or `unreachable!()`" - ); - } - } - } + if let Some(assert_match) = match_assert_with_message(&cx, e) { + match assert_match { + // matched assert but not message + AssertKind::WithoutMessage(false) => lint_false_without_message(), + AssertKind::WithoutMessage(true) | AssertKind::WithMessage(_, true) => lint_true(false), + AssertKind::WithMessage(panic_message, false) => lint_false_with_message(panic_message), + }; } } } } + +/// Result of calling `match_assert_with_message`. +enum AssertKind { + WithMessage(String, bool), + WithoutMessage(bool), +} + +/// Check if the expression matches +/// +/// ```rust,ignore +/// if !c { +/// { +/// ::std::rt::begin_panic(message, _) +/// } +/// } +/// ``` +/// +/// where `message` is any expression and `c` is a constant bool. +fn match_assert_with_message<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option { + if_chain! { + if let ExprKind::If(ref cond, ref then, _) = expr.kind; + if let ExprKind::Unary(UnOp::Not, ref expr) = cond.kind; + // bind the first argument of the `assert!` macro + if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.typeck_results(), expr); + // block + if let ExprKind::Block(ref block, _) = then.kind; + if block.stmts.is_empty(); + if let Some(block_expr) = &block.expr; + // inner block is optional. unwrap it if it exists, or use the expression as is otherwise. + if let Some(begin_panic_call) = match block_expr.kind { + ExprKind::Block(ref inner_block, _) => &inner_block.expr, + _ => &block.expr, + }; + // function call + if let Some(args) = match_panic_call(cx, begin_panic_call); + if args.len() == 1; + // bind the second argument of the `assert!` macro if it exists + if let panic_message = snippet_opt(cx, args[0].span); + // second argument of begin_panic is irrelevant + // as is the second match arm + then { + // an empty message occurs when it was generated by the macro + // (and not passed by the user) + return panic_message + .filter(|msg| !msg.is_empty()) + .map(|msg| AssertKind::WithMessage(msg, is_true)) + .or(Some(AssertKind::WithoutMessage(is_true))); + } + } + None +}