]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/large_stack_arrays.rs
Revert let_chains stabilization
[rust.git] / clippy_lints / src / large_stack_arrays.rs
index 28810696cc180f992e59fa69077f6a7ca229b016..0acbd81aec3438ab99989d2fda6c8960257045b6 100644 (file)
@@ -1,25 +1,24 @@
-use rustc::lint::{LateContext, LateLintPass};
-use rustc::mir::interpret::ConstValue;
-use rustc::ty::{self, ConstKind};
-use rustc_hir::*;
-use rustc_session::{declare_tool_lint, impl_lint_pass};
-
+use clippy_utils::diagnostics::span_lint_and_help;
+use clippy_utils::source::snippet;
 use if_chain::if_chain;
-
-use crate::rustc_target::abi::LayoutOf;
-use crate::utils::{snippet, span_help_and_lint};
+use rustc_hir::{Expr, ExprKind};
+use rustc_lint::{LateContext, LateLintPass};
+use rustc_middle::ty::layout::LayoutOf;
+use rustc_middle::ty::{self, ConstKind};
+use rustc_session::{declare_tool_lint, impl_lint_pass};
 
 declare_clippy_lint! {
-    /// **What it does:** Checks for local arrays that may be too large.
-    ///
-    /// **Why is this bad?** Large local arrays may cause stack overflow.
+    /// ### What it does
+    /// Checks for local arrays that may be too large.
     ///
-    /// **Known problems:** None.
+    /// ### Why is this bad?
+    /// Large local arrays may cause stack overflow.
     ///
-    /// **Example:**
+    /// ### Example
     /// ```rust,ignore
     /// let a = [0u32; 1_000_000];
     /// ```
+    #[clippy::version = "1.41.0"]
     pub LARGE_STACK_ARRAYS,
     pedantic,
     "allocating large arrays on stack may cause stack overflow"
@@ -38,18 +37,17 @@ pub fn new(maximum_allowed_size: u64) -> Self {
 
 impl_lint_pass!(LargeStackArrays => [LARGE_STACK_ARRAYS]);
 
-impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeStackArrays {
-    fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
+impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
+    fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
         if_chain! {
             if let ExprKind::Repeat(_, _) = expr.kind;
-            if let ty::Array(element_type, cst) = cx.tables.expr_ty(expr).kind;
-            if let ConstKind::Value(val) = cst.val;
-            if let ConstValue::Scalar(element_count) = val;
-            if let Ok(element_count) = element_count.to_machine_usize(&cx.tcx);
-            if let Ok(element_size) = cx.layout_of(element_type).map(|l| l.size.bytes());
+            if let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind();
+            if let ConstKind::Value(ty::ValTree::Leaf(element_count)) = cst.kind();
+            if let Ok(element_count) = element_count.try_to_machine_usize(cx.tcx);
+            if let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes());
             if self.maximum_allowed_size < element_count * element_size;
             then {
-                span_help_and_lint(
+                span_lint_and_help(
                     cx,
                     LARGE_STACK_ARRAYS,
                     expr.span,
@@ -57,6 +55,7 @@ fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
                         "allocating a local array larger than {} bytes",
                         self.maximum_allowed_size
                     ),
+                    None,
                     &format!(
                         "consider allocating on the heap with `vec!{}.into_boxed_slice()`",
                         snippet(cx, expr.span, "[...]")