]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/assertions_on_constants.rs
Rollup merge of #82917 - cuviper:iter-zip, r=m-ou-se
[rust.git] / clippy_lints / src / assertions_on_constants.rs
index e2fe5f2400f41d72555f999839bde45e5dc5a612..16905781c56b6851b5ffcce9c89b1ccbdc381f0b 100644 (file)
@@ -1,9 +1,9 @@
 use crate::consts::{constant, Constant};
-use crate::utils::paths;
-use crate::utils::{is_direct_expn_of, is_expn_of, match_function_call, snippet_opt, span_lint_and_help};
+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_ast::ast::LitKind;
-use rustc_hir::{Expr, ExprKind, PatKind, UnOp};
+use rustc_hir::{Expr, ExprKind, UnOp};
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_session::{declare_lint_pass, declare_tool_lint};
 
@@ -29,8 +29,8 @@
 
 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<'_>) {
+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,
@@ -41,6 +41,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) {
                 } else {
                     "`assert!(true)` will be optimized out by the compiler"
                 },
+                None,
                 "remove it",
             );
         };
@@ -50,6 +51,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) {
                 ASSERTIONS_ON_CONSTANTS,
                 e.span,
                 "`assert!(false)` should probably be replaced",
+                None,
                 "use `panic!()` or `unreachable!()`",
             );
         };
@@ -59,6 +61,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) {
                 ASSERTIONS_ON_CONSTANTS,
                 e.span,
                 &format!("`assert!(false, {})` should probably be replaced", panic_message),
+                None,
                 &format!("use `panic!({})` or `unreachable!({})`", panic_message, panic_message),
             )
         };
@@ -69,7 +72,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) {
             }
             if_chain! {
                 if let ExprKind::Unary(_, ref lit) = e.kind;
-                if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, lit);
+                if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.typeck_results(), lit);
                 if is_true;
                 then {
                     lint_true(true);
@@ -100,37 +103,31 @@ enum AssertKind {
 /// Check if the expression matches
 ///
 /// ```rust,ignore
-/// match { let _t = !c; _t } {
-///     true => {
-///         {
-///             ::std::rt::begin_panic(message, _)
-///         }
-///     }
-///     _ => { }
-/// };
+/// if !c {
+///   {
+///     ::std::rt::begin_panic(message, _)
+///   }
+/// }
 /// ```
 ///
 /// where `message` is any expression and `c` is a constant bool.
-fn match_assert_with_message<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) -> Option<AssertKind> {
+fn match_assert_with_message<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<AssertKind> {
     if_chain! {
-        if let ExprKind::Match(ref expr, ref arms, _) = expr.kind;
-        // matches { let _t = expr; _t }
-        if let ExprKind::DropTemps(ref expr) = expr.kind;
-        if let ExprKind::Unary(UnOp::UnNot, ref expr) = expr.kind;
+        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.tables, expr);
-        // arm 1 pattern
-        if let PatKind::Lit(ref lit_expr) = arms[0].pat.kind;
-        if let ExprKind::Lit(ref lit) = lit_expr.kind;
-        if let LitKind::Bool(true) = lit.node;
-        // arm 1 block
-        if let ExprKind::Block(ref block, _) = arms[0].body.kind;
+        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;
-        if let ExprKind::Block(ref inner_block, _) = block_expr.kind;
-        if let Some(begin_panic_call) = &inner_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_function_call(cx, begin_panic_call, &paths::BEGIN_PANIC);
+        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);